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

Working around a weird quirk of getopt #179

Merged
merged 1 commit into from
Nov 20, 2020
Merged

Conversation

amete
Copy link
Collaborator

@amete amete commented Nov 20, 2020

While testing out #178, I noticed a "feature" of getopt that confused me for a while. When the user provides a wrong set of arguments, e.g.:

prmon -i 1 foo -- sleep 5

The forked process executes foo instead of sleep. This is because getopt seems to re-order argv and puts all non-option arguments at the end, i.e.:

argv : prmon -i 1 foo -- sleep 5
getopt_long
argv : prmon -i 1 -- foo sleep 5

As a result, we not only silently omit the wrong input but also end up calling the wrong command later on.

With an additional - at the beginning of the optstring it's possible to catch such issues and piggy-back on the default switch to print a message that suggests using the help.

Copy link
Member

@graeme-a-stewart graeme-a-stewart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good that you caught this @amete! That's quite non-obvious behaviour IMO.

@graeme-a-stewart graeme-a-stewart merged commit f392e54 into master Nov 20, 2020
@amete amete mentioned this pull request Feb 5, 2021
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

Successfully merging this pull request may close these issues.

2 participants