Skip to content

Instantly share code, notes, and snippets.

@fossler
Forked from cosimo/parse-options.sh
Created May 10, 2016 08:23
Show Gist options
  • Select an option

  • Save fossler/2b42513cc02eae11c863014860d86449 to your computer and use it in GitHub Desktop.

Select an option

Save fossler/2b42513cc02eae11c863014860d86449 to your computer and use it in GitHub Desktop.

Revisions

  1. @cosimo cosimo created this gist Sep 21, 2012.
    32 changes: 32 additions & 0 deletions parse-options.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/bin/bash
    #
    # Example of how to parse short/long options with 'getopt'
    #

    OPTS=`getopt -o vhns: --long verbose,dry-run,help,stack-size: -n 'parse-options' -- "$@"`

    if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi

    echo "$OPTS"
    eval set -- "$OPTS"

    VERBOSE=false
    HELP=false
    DRY_RUN=false
    STACK_SIZE=0

    while true; do
    case "$1" in
    -v | --verbose ) VERBOSE=true; shift ;;
    -h | --help ) HELP=true; shift ;;
    -n | --dry-run ) DRY_RUN=true; shift ;;
    -s | --stack-size ) STACK_SIZE="$2"; shift; shift ;;
    -- ) shift; break ;;
    * ) break ;;
    esac
    done

    echo VERBOSE=$VERBOSE
    echo HELP=$HELP
    echo DRY_RUN=$DRY_RUN
    echo STACK_SIZE=$STACK_SIZE