Add flags and options
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.
It occurs to me that we could use clap to construct recipe argument parsers.
@casey Will flags be supported ? It's a quite common use case , Thanks !
flag shorthand can also be supported:
foo --bar(-b):
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>'
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.
Any hope for this feature?
I'm not currently working on it. It's pretty complex, but it is a good feature.
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
I guess one concern with this is distinguishing what arguments/flags are for
justand 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.
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:
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.
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.
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
Yeah, that would be awesome. This tool is so epic, I love it.
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 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 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.
any blocker for that to be implemented? Anything the community can do to help getting there?
any blocker for that to be implemented?
👍 It's long awaited
Would love to see this