argc icon indicating copy to clipboard operation
argc copied to clipboard

Some options to help with inline use

Open dpc opened this issue 1 month ago • 2 comments

I'm doing quite a bit of creative gluing things together including argc.

I'm defining a alias command for Jujutsu:

parent-add = ["util", "exec", "--", "bash", "-c", '''
set -euo pipefail

source <(
nix run nixpkgs#argc -- --argc-eval /dev/stdin "$@" << EOF
# @option -r=@     Commit to modify
# @option -d       Parent to add
# @arg parent      Parent to add (positional version)
EOF
)

commit=${argc_r}
new_parent=${argc_d:-${argc_parent:-}}

if [ -z ${commit:-} ]; then
  >&2 echo "Must provide a commit to rebase"
  exit 1
fi

if [ -z ${new_parent:-} ]; then
  >&2 echo "Must provide a new parent"
  exit 1
fi

jj rebase -s "${commit}" -d "${commit}-" -d "${new_parent}"
''', ""]

which is like an shell script wrapped in a toml config file. Since the alias is executed as bash -c <...> it does not have an usable $0. So I'm using a heredoc passed via /dev/stdin 🤷 .

This works:

> jj parent-remove -h
USAGE: stdin [OPTIONS] [PARENT]

ARGS:
  [PARENT]  Parent to remove (positional version)

OPTIONS:
  -r <R>         Commit to modify [default: @]
  -d <D>         Parent to remove
  -h,  -help     Print help
  -V,  -version  Print version

though that stdin is a bit of nit, and gluing it together was needlessly complicated.

If I may dare for making such rare usecases nicer&easier I wish, there was --argc-eval-stdin or --argc-eval-string, so one could pass the thing to evaluate directly without /dev/stdin (which probably doesn't work on non-Linux/Unix systems, which I don't care about but are a thing), and possibly some way to override the USAGE: stdin [OPTIONS] [PARENT] inferred from the script path.

But it's fine with me if this is not going to get implemented. Maybe the way I glued it here will come handy to someone googling/LLMing for something similar. Thanks for argc anyway!

dpc avatar Nov 10 '25 23:11 dpc

I don't maintain this project, but I've been a long-time user of it. Take this as you wish.

You might have better luck not doing it inline. If you save a normal argc script named jj-parent-add alongside the config.toml, you could use this alias instead. This would give you a better $0 and less jiggery-pokery to get argc to work.

[aliases]
parent-add = ["util", "exec", "--", "sh", "-c", 'exec "$(dirname "$(jj config path --user)")/jj-parent-add" "$@"', ""]

CGamesPlay avatar Nov 18 '25 11:11 CGamesPlay

Of course doing with a separate script would have been easier. But where's the fun in that? And it's harder to share with people and copy&paste into your own config. And "locality of behavior", and some other arguments for doing it inline that I can't think of right now.

dpc avatar Nov 18 '25 16:11 dpc