getopts: not setting OPTARG when argument is both long form and declared optional #684
-
I am trying to use getopts in my script, but I would like to use both long forms of arguments as well as making some arguments have optional values. here is a script that works as expected (% is my shell prompt), the option has short and long versions but it is mandatory
However, when I change the script to make the argument optional (note the inserted ? character), OPTARG does not get set when the long form of the argument is used
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Forgot the mention, version is |
Beta Was this translation helpful? Give feedback.
-
and then it should work, no? |
Beta Was this translation helpful? Give feedback.
-
Yes, that's it. Strictly, in the GNU long option syntax, you need the There are also programs that don't require the But that is incompatible with the argument being optional. If it is optional, there is no way to distinguish between IMHO it would have been better if ksh never supported the |
Beta Was this translation helpful? Give feedback.
-
Yes, adding the |
Beta Was this translation helpful? Give feedback.
-
Since this is not a bug, I've converted the issue to a discussion thread. That way, it can stay visible for future reference, instead of simply closing the issue. Yes, I would absolutely be interested in contributions of examples. Please feel free to edit the wiki. I may use some of it for future documentation in the distro. Thanks :) |
Beta Was this translation helpful? Give feedback.
Yes, that's it. Strictly, in the GNU long option syntax, you need the
=
to add an option-argument. The whole long option plus its argument needs to be one word.There are also programs that don't require the
=
and take the next separate word as the option-argument instead, and it looks like the AT&T developers decided to support that syntax as well.But that is incompatible with the argument being optional. If it is optional, there is no way to distinguish between
--max 10
where10
is an option-argument tomax
, and--max 10
where--max
does not have an option-argument and10
is simply a separate, non-option argument. So that is why the=
is necessary in that case.IMHO it would have been …