Sitelet https://github.com/casey/just/issues/2379
Skip to content

Coalesce-like string function #2379

Description

@faultymajority

In a Justfile I'm building, I have use for "optional arguments". It would be helpful to have something like

target a *b:
  echo {{ coalesce(b, a) }}

where the return value of coalesce is b is passed (i.e., its value has length > 0), and a otherwise. In that way, b would be an optional argument, with a as default.

Would a PR for this be welcome? The function name would probably need bike shedding, as SQL coalesce operates based on the first argument being NULL, not "empty".

It would (for my use case) be even nicer if there were syntax

target a *b=a:
  echo {{ b }}

which currently fails with "error: Variable a not defined". Could this ever work? As the pieces seem there:

x := "abc"
target a *b=x:
  echo {{ b }}

UDFs in general
Generally - for "user-defined functions" I'm currently using the following pattern

coalesce a *b:
  @echo {{ a }}
target a *b:
  echo $(just coalesce {{ b }} {{ a }})

Is this the intended way to have UDFs?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions