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

Double hyphen not handled correctly in some situations #15

Closed
dodecatheon opened this issue Apr 4, 2023 · 2 comments · Fixed by #20
Closed

Double hyphen not handled correctly in some situations #15

dodecatheon opened this issue Apr 4, 2023 · 2 comments · Fixed by #20

Comments

@dodecatheon
Copy link

Try

_usage () { echo "usage" 1>&2; exit ${1:-0}; }
_die () { printf "\n$1\n" 1>&2; _usage ${2:-1}; }
while read args ; do
  OPTIND=1
  while getopts_long ":hvab: alpha bravo: charlie" OPT  $args ; do
    case "$OPT" in
      h|help   ) _usage          ;; # E.g., help option
      v|verbose) ((++verbose))   ;; # E.g., an incrementing option
      a|alpha  ) alpha=1         ;; # E.g., non-arg-requiring option
      b|bravo  ) bravo="$OPTARG" ;; # E.g., argument-requiring option
      charlie  ) charlie=1       ;; # E.g., long-option-only
      ':') _die "OPTION '${OPTARG}': MISSING ARGUMENT" 1;;
      '?') _die "UNKNOWN OPTION: '${OPTARG}'"          2;;
    esac
    echo "OPT=$OPT, OPTIND=$OPTIND, OPTARG=$OPTARG"
  done
done < <( cat <<EOF
-a-- param
EOF
)

and compare with the behavior of getopts. getopts is correctly able to stop parsing when the double-hyphen is grouped with the non-argument-requiring option, whether or not it is space separated, and whether or not '-:' is included in the shortopt spec.

@AndrewDDavis
Copy link

This is a strange edge case where it's not so clear what the correct behaviour is, IMO, but it does make sense to do the same as getopts.

@UrsaDK
Copy link
Owner

UrsaDK commented Dec 10, 2024

Thanks for reporting this guys! It took me a while, but I finally had a chance to look into it, and it turns out the issue isn’t just with "--" alone.

The root cause is that Bash’s built-in getopts supports adjacent variable-value syntax, like what you often see when compiling C: gcc -Llib -o example example.c -lmylib. Unfortunately, getopts_long can’t handle that properly.

The good news is the fix is fairly straightforward – #f263a9c – and will be available in getopts_long v1.2.3. 🙂

UrsaDK added a commit that referenced this issue Dec 10, 2024
* Refactored internal variable names used in tests
* Fixed github #15
* Replaced regex with globing to improve performance
UrsaDK added a commit that referenced this issue Dec 13, 2024
- Reverted adjacent option-value syntax for long options
- Test helper improvements:
  - Added the ability to pre-process test output using multiple sed expressions.
  - Added the capability to match the context of a single output line against a regex.
UrsaDK added a commit that referenced this issue Dec 13, 2024
UrsaDK added a commit that referenced this issue Dec 14, 2024
- Fixed double hyphen handling in short options
- Added more tests to cover #15 
- Updated documentation
- Test suit improvements
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 a pull request may close this issue.

3 participants