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

feat: allow command line flags to be used in any order #41

Closed
wants to merge 1 commit into from
Closed

feat: allow command line flags to be used in any order #41

wants to merge 1 commit into from

Conversation

zanona
Copy link

@zanona zanona commented Nov 11, 2020

This PR allows using option arguments in any position by depending on getopt

resolve #1

@sdushantha
Copy link
Owner

Thank you! I really appreciate you attempting to fix this!

It looks like there is something wrong. tmpmail fails to show the help message when the --help flag is used
image

@zanona
Copy link
Author

zanona commented Nov 11, 2020

Hi!
That's strange. I've notice you are invoking tmpmail from your PATH there, which may be aliased to a different version of the program.

Do you get the same when running bash ./tmpmail --help from the project directly?

you can also do from the project directory:

alias tmpmail=./tmpmail
bash tmpmail --help

@sdushantha
Copy link
Owner

Non of the solutions that you have suggested fixes the issue
Screenshot 2020-11-11 at 17 08 56

@zanona
Copy link
Author

zanona commented Nov 11, 2020

Ah, I believe we have a compatibility issue here, where getopt OSX implementation doesn't support long names.

https://stackoverflow.com/questions/12152077

BSD UNIX implementation of standalone getopt command (which is what MacOS uses). This does not support long options either.

Apologies for having missed this previously as it works well on Linux.
I am going to close this now so if someone would like to implement this use getopts with a hack for long names instead, that should be cross compatible.

Just in case someone's interested, below is a sample of how to workaround getopts limitation. IMO that defeats the purpose of using a program for parsing options, as it needs to be almost completely tweaked from its original behaviour:

source: https://stackoverflow.com/a/28466267/165750

die() { echo "$*" >&2; exit 2; }  # complain to STDERR and exit with error
needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; }

while getopts ab:c:-: OPT; do
  # support long options: https://stackoverflow.com/a/28466267/519360
  if [ "$OPT" = "-" ]; then   # long option: reformulate OPT and OPTARG
    OPT="${OPTARG%%=*}"       # extract long option name
    OPTARG="${OPTARG#$OPT}"   # extract long option argument (may be empty)
    OPTARG="${OPTARG#=}"      # if long option argument, remove assigning `=`
  fi
  case "$OPT" in
    a | alpha )    alpha=true ;;
    b | bravo )    needs_arg; bravo="$OPTARG" ;;
    c | charlie )  needs_arg; charlie="$OPTARG" ;;
    ??* )          die "Illegal option --$OPT" ;;  # bad long option
    ? )            exit 2 ;;  # bad short option (error reported via getopts)
  esac
done
shift $((OPTIND-1)) # remove parsed options and args from $@ list

@zanona zanona closed this Nov 11, 2020
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.

Allow command line flags to be used in any order
2 participants