Skip to content

getopts: not setting OPTARG when argument is both long form and declared optional #684

Answered by McDutchie
rosswmerrifield asked this question in Q&A
Discussion options

You must be logged in to vote

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 where 10 is an option-argument to max, and --max 10 where --max does not have an option-argument and 10 is simply a separate, non-option argument. So that is why the = is necessary in that case.

IMHO it would have been …

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by McDutchie
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #683 on September 22, 2023 16:19.