matejak / argbash

Bash argument parsing code generator
Other
1.39k stars 63 forks source link

Multiple Positional Arguments with Defaults #99

Open EdwardOst opened 5 years ago

EdwardOst commented 5 years ago

Feature Request: Argbash allows defaults for positional arguments, but in practice it can only be applied to the first positional argument. Many scripts have multiple parameters which have defaults. All such positional optional parameters are necessarily at the end of the signature. Intelligent defaults is a good practice in general. Could this be supported in a future version of Argbash?

Example

# ARG_POSITIONAL_SINGLE([bucket], [s3 bucket to publish the AWS CLI layer], [default_bucket])
# ARG_POSITIONAL_SINGLE([s3_folder], [s3 folder key to publish AWS CLI layer], [awscli])

could support

myscript myscript mybucket myscript mybucket my/folder/key

matejak commented 5 years ago

This is a tricky feature request. Imagine that the script accepts two arguments, both of them have default. You supply one. The question is: Which one has been supplied? The first one, or the last one?

EdwardOst commented 5 years ago

since they are positional it would be the first one. I realize that I can do this with ARG_POSITIONAL_INF but the parameters are not named.

matejak commented 5 years ago

Your answer sounds reasonable. I will think of this - the feature is not so complicated to implement, but it will open a lot of possibilities, and the code has to be tested thoroughly, so it won't break in the future.

sebastiancarlos commented 1 year ago

I second this feature request.

In my particular case I want a command similar to man, which has the usage man <options go here> [section] name

So you can pass either one positional argument (which is interpreted as name) or two (which are interpreted as section first and then name).

I was thinking about implementing this with an empty default for section, indicating that it is optional:

# ARG_POSITIONAL_SINGLE([section],[],[""])
# ARG_POSITIONAL_SINGLE([name])

But as @EdwardOst points out, you can't have positional arguments with defaults at the beginning.

Now, it works fine the other way around, but I would prefer to have it this way to share muscle memory with related commands.