Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for "external subcommands" #251

Closed
CGamesPlay opened this issue Oct 1, 2023 · 1 comment
Closed

Better support for "external subcommands" #251

CGamesPlay opened this issue Oct 1, 2023 · 1 comment

Comments

@CGamesPlay
Copy link
Contributor

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
Copy link
Owner

sigoden commented Oct 1, 2023

use @arg args~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants