-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Support for positional arguments #59
Comments
It should be able to be written as follows parser_definition() {
setup ARGS
param FOO --foo
}
eval "$(getoptions parser_definition __parse) exit 1"
__parse "$@"
eval "set -- $ARGS" # 👈
echo "$FOO"
FIRST="$1" SECOND="$2"
echo $FIRST # outputs "first_arg"
echo $SECOND # outputs "second_arg" It could be simplified a bit more and written as follows parser_definition() {
setup ARGS
param FOO --foo
}
eval "$(getoptions parser_definition) exit 1"
echo "$FOO"
FIRST="$1" SECOND="$2"
echo $FIRST # outputs "first_arg"
echo $SECOND # outputs "second_arg" Perhaps documentation needs to be improved. |
Right. I'm saying that it would be nicer if the positional args could be defined in the same parser definition function (and have the positional args loaded into the right vars), rather than doing extra stuff after calling __parse |
@cweagans The author can correct me if I'm wrong, but because |
Understood. I'm asserting that the args vs options vs flags vs params argument is a distinction without a difference. Ultimately, all flags/params/options/args are going to be in |
As @kkerce says, the thought that the treatment of (non-option) arguments is out of scope is one reason. But, I do not think it is that far out of scope. However, In my opinion, Although getoptions is designed to be embedded in shell scripts as a library, some people are not comfortable with the inclusion of long option parsing code. So I prefer to keep my implementation of getoptions as small as possible. About Terms: I follow the POSIX terminology (in principle) and consider flags and options to be the same thing and flags to be historical usage.
However, the term “operand” does not seem to be common. Therefore, I use “argument” instead of “operand” and the meaning of “argument” has become ambiguous. “argument” may be used to mean all arguments including options, or it may mean ”operand". |
I have a command that can be invoked like so:
(first_arg and second_arg are arbitrary strings)
My parser definition looks like this:
That all works great, but then I need to do something like this for the remaining positional arguments:
Do you think it makes sense to add a new kind of argument to the parser_definition where I can define the positional arguments too? Maybe something along these lines:
parser_definition() { setup ARGS param FOO --foo arg MY_ARG arg ANOTHER_ARG }
I imagine that this could filter $@ to elements that do not begin with a
-
or--
, and then $MY_ARG is assigned the value of the first arg, $ANOTHER_ARG is assigned the value of the second arg, and so forth.There are probably other considerations for this library, but as a starting point, does this idea make sense and/or would you be interested in a PR to add something like that?
The text was updated successfully, but these errors were encountered: