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

Fix broken getopts() usage, allows multiple flags #286

Merged
merged 1 commit into from
Apr 3, 2021

Conversation

alerque
Copy link
Collaborator

@alerque alerque commented Apr 3, 2021

The current usage of getopts is a kind of dirty hack: using it to iterate over passable arguments but using a hand rolled flag matching syntax to actually parse them. It appears this was done as a workaround to catch a long form ‘--debug’ argument, but I believe it introduces more problems than it solves. The long form argument does sort of work, but only after getpots throws an error about invalid argument syntax and only on a fluke of ‘-d’ being a substring of ‘--debug’ and getpots having assigned ‘ebug’ as $OPTARG.

In one sense this is a regression because it removes the long form option parsing, but it was not documented in help anyway and I believe it is unlikely anybody is using this particular flag in a script that will break.

On the other hand with proper iteration of the getopts assigned flags variable, we can now properly handle multiple flags. Previously things like -d -v or -v -c foo would attempt to do crazy broken things such as:

GIT_DIR=/home/caleb/.config/vcsh/repo.d/-v.git

Now multiple flag handling just works as expected.

The current usage of getopts is a kind of dirty hack: using it to
iterate over passable arguments but using a hand rolled flag matching
syntax. It appears this was done as a workaround to catch a long form
‘--debug’ argument, but I believe it introduces more problems than it
solves. The long form argument does sort of work, but only after getpots
throws an error about invalid argument syntax and only on a fluke of
‘-d’ being a substring of ‘--debug’ and getpots having assigned ‘ebug’
as $OPTARG.

In one sense this is a regression because it removes the long form
option parsing, but it was not documented in help anyway and I believe
it is unlikely anybody is using this particular flag in a script that
will break.

On the other hand with proper iteration of the getopts assigned flags
variable, we can now properly handle multiple flags. Previously things
like `-d -v` or `-v -c foo` would attempt to do crazy broken things such
as:

    GIT_DIR=/home/caleb/.config/vcsh/repo.d/-v.git

Now multiple flag handling just works as expected.
Copy link
Owner

@RichiH RichiH left a comment

Choose a reason for hiding this comment

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

Nice!

@RichiH RichiH merged commit 1c15d33 into RichiH:main Apr 3, 2021
@alerque alerque deleted the fix-getopts branch April 3, 2021 13:45
@alerque alerque added this to the v2.0.0 milestone Jun 23, 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