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

Use getopt to process options #10

Open
bbros-dev opened this issue Mar 29, 2020 · 0 comments
Open

Use getopt to process options #10

bbros-dev opened this issue Mar 29, 2020 · 0 comments
Assignees

Comments

@bbros-dev
Copy link

bbros-dev commented Mar 29, 2020

Feeding back a more radical change than the prior suggestions in PR #8 and issue #9.

What I now have in my template.sh:

From memory, I iterated from this blog source and this gist.

function parse_params() {
  # -allow a command to fail with !’s side effect on errexit
  # -use return value from ${PIPESTATUS[0]}, because ! hosed $?
  ! getopt --test > /dev/null 
  if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
      script_exit 'I’m sorry, `getopt --test` failed in this environment.' 2
  fi

  OPTIONS=chno:v
  LONGOPTS=cron,help,no-color,output:,verbose

  # -regarding ! and PIPESTATUS see above
  # -temporarily store output to be able to check for errors
  # -activate quoting/enhanced mode (e.g. by writing out “--options”)
  # -pass arguments only via   -- "$@"   to separate them correctly
  ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
  if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
      # e.g. return value is 1
      #  then getopt has complained about wrong arguments to stdout
      script_exit "Argument error" 2
  fi
  # read getopt’s output this way to handle the quoting right:
  eval set -- "$PARSED"

  local param
...
@ralish ralish self-assigned this Nov 8, 2020
@ralish ralish changed the title Idea: Use getopt to process options Use getopt to process options Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants