just icon indicating copy to clipboard operation
just copied to clipboard

Add flags and options

Open casey opened this issue 6 years ago • 20 comments

Recipes should be able to define options:

foo --bar BAZ:
  echo {{BAZ}}

Maybe flags too:

foo --BAZ:
  echo {{BAZ}} # is it "true" or "false"? Or "" or BAZ?

Large changes to the parsing of recipe parameters like this should be considered holistically, along with other possible changes, like $ to support exporting parameters as environment variables, or changing all just variables to being environment variables.

casey avatar Sep 24 '19 02:09 casey

It occurs to me that we could use clap to construct recipe argument parsers.

casey avatar Nov 11 '19 01:11 casey

@casey Will flags be supported ? It's a quite common use case , Thanks !

hustcer avatar Sep 16 '21 00:09 hustcer

flag shorthand can also be supported: foo --bar(-b):

hustcer avatar Sep 16 '21 00:09 hustcer

Just my two cents: I would already be super happy if I could explicitly refer to the argument's name:

foo bar=something:
	echo {{bar}}
$ just foo bar=stuff
stuff

Of course fully featured flags and options would be lovely but for my personal use-case (and I imagine other's as well) this would already be very helpful, because then I can do things like this:

watch folders='lib,test' cmd='just test':
  find {{ '{' }}{{folders}}{{ '}' }} -name '*.ex*' | entr -dc {{cmd}}
$ just watch cmd='<some other command>'

sascha-wolf avatar Aug 31 '23 09:08 sascha-wolf

I've just started using Just and my org loves it!

One thing that would improve our usage is opts with enums. We use just to coordinate all of our environment scripts and so we have done things like standardize accounts or environments to a certain list. Right now, they're just strings that we then call a pre-recipe on to check that they are one of a list. It would be awesome if we had this functionality AND incorporaitng an enum for one of the opts. I know https://github.com/casey/just/issues/1990 is also similar.

colemannerd avatar Jun 06 '24 04:06 colemannerd

Any hope for this feature?

chosroes avatar Oct 07 '24 11:10 chosroes

I'm not currently working on it. It's pretty complex, but it is a good feature.

casey avatar Oct 08 '24 00:10 casey

Would definitely love this capability. Right now you can use env vars as named parameters

env profile=release os=linux arch=amd64 just build

But it would be great if the arguments could be first class citizens of just (with default values if omitted) because they are discoverable with just --list and can be added to bash/zsh/fish/etc autocomplete.

For example:

just build --profile release --os linux --arch amd64

I guess one concern with this is distinguishing what arguments/flags are for just and what arguments can be passed through to an underlying command

For instance:

just run --profile release --os linux --arch amd64 --foo --bar 42

Perhaps explicitly named arguments can be excluded from *ARGS (or maybe a new ...ARGS)

run --profile="release" --os="{{ os() }}" --arch="{{ arch() }}" ...ARGS:
  cargo build --profile {{ profile }} --target {{ os }}-{{ arch }}
  ./target/{{ profile }}/main {{ ARGS }}

Which executes:

cargo build --profile release --target macos-amd64
./target/release/main --foo --bar 42

alshdavid avatar Nov 05 '24 01:11 alshdavid

I guess one concern with this is distinguishing what arguments/flags are for just and what arguments can be passed through to an underlying command

One way I've seen that handled in some other tools is to use -- as a separator. Any args before that are for the tool, and any after get passed to the next program.

So for your example, it would look something like

just run --profile release --os linux --arch amd64 -- --foo --bar 42

Not sure how hard that would be to implement or if it would work here, just thought I'd mention it as some prior art.

hisaac avatar Nov 05 '24 21:11 hisaac

Would love this feature as well. I'll put in my vote for this syntax, it feels pretty natural to me:

run --profile="release" --os="{{ os() }}" --arch="{{ arch() }}" ...ARGS:

iloveitaly avatar Dec 17 '24 17:12 iloveitaly

I would also very much like to have this feature. I was surprised to see it wasn't included in the list of basic features.

candsastle avatar Jan 18 '25 19:01 candsastle

Another 👍 for this feature.

If you are using bash, one alternative is to use argc in your recipe:

# argc example
[positional-arguments]
my_command *_:
        #!/usr/bin/env bash
        # @flag -F --foo        Flag param
        # @option --bar         Option param
        # @option --baz*        Option param (multi-occurs)
        # @option -q --qux=123  Option with default value
        # @arg val*             Positional param
        set -eu
        eval "$(argc --argc-eval "$0" "$@")"
        echo foo: ${argc_foo:-UNSET}
        echo bar: ${argc_bar:-UNSET}
        echo baz: ${argc_baz[@]}
        echo qux: $argc_qux
        echo val: ${argc_val[@]}
> just my_command --help
USAGE: my_command [OPTIONS] [VAL]...

ARGS:
  [VAL]...  Positional param

OPTIONS:
  -F, --foo           Flag param
      --bar <BAR>     Option param
      --baz [BAZ]...  Option param (multi-occurs)
  -q, --qux <QUX>     Option with default value [default: 123]
  -h, --help          Print help
  -V, --version       Print version

See the argc documentation for more details.

jyoung15 avatar Mar 04 '25 15:03 jyoung15

Would be cool if there where boolean flags too

build --prod="false":
  cargo build {{ if prod == "true" { "--profile release" } else { "" } }}

Where just can detect the default type as "true" | "false" allowing for

just build --prod
just build --prod true

alshdavid avatar Mar 26 '25 23:03 alshdavid

Yeah, that would be awesome. This tool is so epic, I love it.

iloveitaly avatar Mar 27 '25 17:03 iloveitaly

I implemented support flags and options in #2806. I took a couple of design decisions, see PR for details. Most notably, I used this syntax:

recipe --flag --option="foo" positional:
  echo body

chmp avatar Jul 09 '25 19:07 chmp

@chmp I briefly glanced at your PR am I correct in thinking that will support the following use case:

just mycommand -- --some-flag

Where --some-flag would be appended to the end of the internal definition in the justfile?

mdstaff avatar Aug 15 '25 22:08 mdstaff

@mdstaff anything after -- is treated as a positional argument. Eg, with a var-args argument as in *args it would be added to args.

TBH, I am somewhat hesitant to discuss the details of my impl on this issue. My suggestion would be to do so on the PR itself.

chmp avatar Sep 06 '25 08:09 chmp

any blocker for that to be implemented? Anything the community can do to help getting there?

gsouf avatar Sep 24 '25 10:09 gsouf

any blocker for that to be implemented?

👍 It's long awaited

chosroes avatar Sep 24 '25 10:09 chosroes

Would love to see this

Ksauder avatar Oct 28 '25 14:10 Ksauder