casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
20.66k stars 454 forks source link

How to shift positional arguments or pass part of positional arguments? #1883

Open contrun opened 8 months ago

contrun commented 8 months ago

I am trying to pass arguments from the main justfile to two separated justfiles (since module are not stable yet).

_run component *args:
    @echo "Running commands for {{component}}"
    {{just_executable()}} -f "{{component}}/mod.justfile" "$@"

frontend *args='': (_run "frontend" args)

backend *args='': (_run "backend" args)

When I ran just frontend echo, I got the output

Running commands for frontend
/nix/store/fv9livr2vv8nr9mgr6g2lfyblh6pkwzj-just-1.16.0/bin/just -f "frontend/mod.justfile" "$@"
error: Justfile does not contain recipe `frontend`.
error: Recipe `_run` failed on line 6 with exit code 1

I can see from execsnoop that /nix/store/fv9livr2vv8nr9mgr6g2lfyblh6pkwzj-just-1.16.0/bin/just -f frontend/mod.justfile frontend echo is what actually executed. This seems to be because "$@" is frontend echo instead of the desired echo.

I still very much prefer things like

test *args='':
    bash -c 'while (( "$#" )); do echo - $1; shift; done' -- "{{args}}"

as in https://github.com/casey/just/issues/1054 . I think we need to be careful to distinguish am empty set of positional arguments and a empty string argument. It is my understanding that we don't have a empty set literal. If there is one, we can quite easily add things like expanding an array to arguments and passing them to a command.

casey commented 8 months ago

I think you can do something like this to avoid passing frontend: "${@:2}"