Better support for "external subcommands"
CGamesPlay opened this issue ยท 1 comments
CGamesPlay commented
I do a lot of wrapping commands with argc. Generally, I include an escape hatch to pass arguments directly to the wrapped program, and this works well (post #232 ๐) using argc my-command -- --foo-bar
.
A useful addition to argc might be an "external subcommand" feature. Basically, when there is an external subcommand in play, as soon as an unsupported argument is found, all remaining arguments get treated as positional, even if argc would otherwise recognize them. There's a few ways this would work:
- Instead of
@arg args+
we use@remaining_args args+
or something (@rest
,@remainder
, ...). Must be after all@arg
, and after this starts to match, it consumes the rest of the input, ignoring further flags and options. - Instead of
@arg args+
we use@arg args...
or some other sigil. Same idea. - We add some
@meta external_subcommand
or something which applies this rule to the last@arg
.
Here's a quick example:
#!/usr/bin/env bash
# @describe Example Argcfile
set -eu
# @cmd Wrapper that works today (wraps terraform)
#
# Example: run with auto-approval
# argc terraform apply -- -auto-approve
# @arg subcommand Subcommand for terraform
# @arg args* Additional arguments for terraform
# @option -e --environment=production Name of the environment to operate in
terraform() {
echo terraform -chdir=terraform "${argc_subcommand:?}" -var-file="${argc_environment:?}.tfvars" ${args_args+"${argc_args[@]}"}
}
# @cmd Wrapper that would be nice (1st idea)
#
# Example: run with auto-approval
# argc terraform2 apply -auto-approve
# @arg subcommand Subcommand for terraform
# @remaining_args args Additional arguments for terraform
# @option -e --environment=production Name of the environment to operate in
terraform2() {
echo terraform -chdir=terraform "${argc_subcommand:?}" -var-file="${argc_environment:?}.tfvars" ${args_args+"${argc_args[@]}"}
}
# @cmd Wrapper that would be nice (2nd idea)
#
# Example: run with auto-approval
# argc terraform3 apply -auto-approve
# @arg subcommand Subcommand for terraform
# @arg args... Additional arguments for terraform
# @option -e --environment=production Name of the environment to operate in
terraform3() {
echo terraform -chdir=terraform "${argc_subcommand:?}" -var-file="${argc_environment:?}.tfvars" ${args_args+"${argc_args[@]}"}
}
# @cmd Wrapper that would be nice (3rd idea)
#
# Example: run with auto-approval
# argc terraform4 apply -auto-approve
# @arg subcommand Subcommand for terraform
# @arg args* Additional arguments for terraform
# @option -e --environment=production Name of the environment to operate in
# @meta external_subcommand
terraform4() {
echo terraform -chdir=terraform "${argc_subcommand:?}" -var-file="${argc_environment:?}.tfvars" ${args_args+"${argc_args[@]}"}
}
if ! command -v argc >/dev/null; then
echo "This command requires argc. Install from https://github.com/sigoden/argc" >&2
exit 100
fi
eval "$(argc --argc-eval "$0" "$@")"
sigoden commented
use @arg args~