Differences between revisions 5 and 6
Revision 5 as of 2016-03-07 10:20:18
Size: 8858
Editor: JarnoSuni
Comment: If the parenthesis are there, only the first argument of the option is stored in OPTARG (thought it does not matter in this example).
Revision 6 as of 2016-03-07 15:06:21
Size: 9331
Editor: JarnoSuni
Comment: Revert the last change. Add an option that takes two arguments. Improve usage and put it in a function. Add general syntax error checking.
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
optspec=":h-:"

while getopts "$optspec" optchar
do
    case "${optchar}" in
        -)
            case "${OPTARG}" in
              loglevel)
                  eval val="\${$OPTIND}"; OPTIND=$(( OPTIND + 1 ))
                  echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2
                  ;;
              loglevel=*)
                  val=${OPTARG#*=}
                  opt=${OPTARG%=$val}
                  echo "Parsing option: '--${opt}', value: '${val}'" >&2
                  ;;
            esac
            ;;
        h)
            echo "usage: $0 [--loglevel[=]<value>]" >&2
            exit 2
            ;;
    esac
done

# End of file
}}}
In practice, this example is so obfuscated that it may be preferable to add concatenated option support (like `-vf filename`) to a manual parsing loop instead, if that was the only reason for using `getopts`.

Here's an improved and more generalized version of above attempt to add support for long options when using `getopts`:

{{{#!highlight bash
#!/bin/bash
# Uses bash extensions. Not portable as written.
usage() {
    echo "Usage:"
    echo " $0 [ --loglevel=<value> | --loglevel <value> | -l <value> ] [ --range <beginning> <end> ] [--] [non-option-argument]..."
    echo " $0 [ --help | -h ]"
    echo
    echo "Default loglevel is 0. Default range is 0 to 0"
}

# set defaults
loglevel=0
r1=0 # beginning of range
r2=0 # end of range

i=$(($# + 1)) # index of the first non-existing argument
Line 44: Line 23:
longoptspec=( [loglevel]=1 ) #use associative array to declare how many arguments a long option expects, in this case we declare that loglevel expects/has one argument, long options that aren't listed in this way will have zero arguments by default
optspec=":h-:"
# Use associative array to declare how many arguments a long option
# expects. In this case we declare that loglevel expects/has one
# argument and range has two. Long options that aren't listed in this
# way will have zero arguments by default.
longoptspec=( [loglevel]=1 [range]=2 )
optspec=":l:h-:"
Line 50: Line 33:
            if [[ "${OPTARG}" =~ .*=.* ]] #with this --key=value format only one argument is possible             if [[ ${OPTARG} =~ .*=.* ]] # with this --key=value format only one argument is possible
Line 52: Line 35:
Line 53: Line 37:
                ((${#opt} <= 1)) && {
                    echo "Syntax error: Invalid long option '$opt'" >&2
                    exit 2
                }
                if (($((longoptspec[$opt])) != 1))
                then
                    echo "Syntax error: Option '$opt' does not support this syntax." >&2
                    exit 2
                fi
Line 54: Line 47:
                ((OPTIND--))
Line 57: Line 49:
                OPTARG=${@:OPTIND:$((longoptspec[$opt]))}                 ((${#opt} <= 1)) && {
                    echo "Syntax error: Invalid long option '$opt'" >&2
                    exit 2
                }
                OPTARG=(${@:OPTIND:$((longoptspec[$opt]))})
                ((OPTIND+=longoptspec[$opt]))
                echo $OPTIND
                ((OPTIND > i)) && {
                    echo "Syntax error: Not all required arguments for option '$opt' are given." >&2
                    exit 3
                }
Line 59: Line 61:
            ((OPTIND+=longoptspec[$opt]))
            continue #now that opt/OPTARG are set we can process them as if getopts would've given us long options
            ;;
        loglevel)
          loglevel=$OPTARG

            continue #now that opt/OPTARG are set we can process them as
            # if getopts would've given us long options
            ;;
        l|loglevel)
            loglevel=$OPTARG
            ;;
        range)
            r1=${OPTARG[0]}
            r2=${OPTARG[1]}
Line 66: Line 73:
            echo "usage: $0 [--loglevel[=]<value>]" >&2             usage
            exit 0
            ;;
        ?)
            echo "Syntax error: Unknown short option '$OPTARG'" >&2
            exit 2
            ;;
        *)
            echo "Syntax error: Unknown long option '$opt'" >&2
Line 73: Line 88:
echo "Loglevel: $loglevel"
echo "Range: $r1 to $r2"
echo "First non-option-argument (if exists): ${!OPTIND-}"
Line 75: Line 94:
With this version you can have long and short options side by side and you shouldn't need to modify the code from line 10 to 22. This solution can also handle multiple arguments for long options, just use ${OPTARG} or ${OPTARG[0]} for the first argument, ${OPTARG[1]} for the second argument, ${OPTARG[2]} for the third argument and so on. It has the same disadvantage of its predecessor in not being portable and specific to bash. Additionally, it breaks arguments with whitespace (line 18) for long options, when given using the syntax that does not include equal sign (=). With this version you can have long and short options side by side and you shouldn't need to modify the code from line 26 to 60. This solution can also handle multiple arguments for long options, just use ${OPTARG} or ${OPTARG[0]} for the first argument, ${OPTARG[1]} for the second argument, ${OPTARG[2]} for the third argument and so on. It has the same disadvantage of its predecessor in not being portable and specific to bash. Additionally, it breaks arguments with whitespace (line 49) for long options, when given using the syntax that does not include equal sign (=).

getopts long option trickery

Here is an example which claims to parse long options with getopts. The basic idea is quite simple: just put "-:" into the optstring. This trick requires a shell which permits the option-argument (i.e. the filename in "-f filename") to be concatenated to the option (as in "-ffilename") as POSIX requires. The POSIX standard says you should pass arguments to options as separate options (to avoid problems with empty arguments) but requires getopts to handle the case where the argument is stuck to the option in the same argument. However, POSIX leaves the behaviour unspecified when an option is not alpha-numerical, so POSIX does not guarantee this to work. However, it should work in all of bash, dash, yash, zsh and all implementations of ksh, the only exception reported so far being posh.

   1 #!/bin/bash
   2 # Uses bash extensions.  Not portable as written.
   3 
   4 usage() {
   5     echo "Usage:"
   6     echo " $0 [ --loglevel=<value> | --loglevel <value> | -l <value> ] [ --range <beginning> <end> ] [--] [non-option-argument]..."
   7     echo " $0 [ --help | -h ]"
   8     echo
   9     echo "Default loglevel is 0. Default range is 0 to 0"
  10 }
  11 
  12 # set defaults
  13 loglevel=0
  14 r1=0 # beginning of range
  15 r2=0 # end of range
  16 
  17 i=$(($# + 1)) # index of the first non-existing argument
  18 declare -A longoptspec
  19 # Use associative array to declare how many arguments a long option
  20 # expects. In this case we declare that loglevel expects/has one
  21 # argument and range has two. Long options that aren't listed in this
  22 # way will have zero arguments by default.
  23 longoptspec=( [loglevel]=1 [range]=2 )
  24 optspec=":l:h-:"
  25 while getopts "$optspec" opt; do
  26 while true; do
  27     case "${opt}" in
  28         -) #OPTARG is name-of-long-option or name-of-long-option=value
  29             if [[ ${OPTARG} =~ .*=.* ]] # with this --key=value format only one argument is possible
  30             then
  31 
  32                 opt=${OPTARG/=*/}
  33                 ((${#opt} <= 1)) && {
  34                     echo "Syntax error: Invalid long option '$opt'" >&2
  35                     exit 2
  36                 }
  37                 if (($((longoptspec[$opt])) != 1))
  38                 then
  39                     echo "Syntax error: Option '$opt' does not support this syntax." >&2
  40                     exit 2
  41                 fi
  42                 OPTARG=${OPTARG#*=}
  43             else #with this --key value1 value2 format multiple arguments are possible
  44                 opt="$OPTARG"
  45                 ((${#opt} <= 1)) && {
  46                     echo "Syntax error: Invalid long option '$opt'" >&2
  47                     exit 2
  48                 }
  49                 OPTARG=(${@:OPTIND:$((longoptspec[$opt]))})
  50                 ((OPTIND+=longoptspec[$opt]))
  51                 echo $OPTIND
  52                 ((OPTIND > i)) && {
  53                     echo "Syntax error: Not all required arguments for option '$opt' are given." >&2
  54                     exit 3
  55                 }
  56             fi
  57 
  58             continue #now that opt/OPTARG are set we can process them as
  59             # if getopts would've given us long options
  60             ;;
  61         l|loglevel)
  62             loglevel=$OPTARG
  63             ;;
  64         range)
  65             r1=${OPTARG[0]}
  66             r2=${OPTARG[1]}
  67             ;;
  68         h|help)
  69             usage
  70             exit 0
  71             ;;
  72         ?)
  73             echo "Syntax error: Unknown short option '$OPTARG'" >&2
  74             exit 2
  75             ;;
  76         *)
  77             echo "Syntax error: Unknown long option '$opt'" >&2
  78             exit 2
  79             ;;
  80     esac
  81 break; done
  82 done
  83 
  84 echo "Loglevel: $loglevel"
  85 echo "Range: $r1 to $r2"
  86 echo "First non-option-argument (if exists): ${!OPTIND-}"
  87 
  88 # End of file

With this version you can have long and short options side by side and you shouldn't need to modify the code from line 26 to 60. This solution can also handle multiple arguments for long options, just use ${OPTARG} or ${OPTARG[0]} for the first argument, ${OPTARG[1]} for the second argument, ${OPTARG[2]} for the third argument and so on. It has the same disadvantage of its predecessor in not being portable and specific to bash. Additionally, it breaks arguments with whitespace (line 49) for long options, when given using the syntax that does not include equal sign (=).

Silly repeated brute-force scanning

Another approach is to check options with if statements "on demand". A function like this one may be useful:

   1 #!/bin/bash
   2 
   3 HaveOpt ()
   4 {
   5     local needle=$1
   6     shift
   7 
   8     while [[ $1 == -* ]]
   9     do
  10         # By convention, "--" means end of options.
  11         case "$1" in
  12             --)      return 1 ;;
  13             $needle) return 0 ;;
  14         esac
  15 
  16         shift
  17     done
  18 
  19     return 1
  20 }
  21 
  22 HaveOpt --quick "$@" && echo "Option quick is set"
  23 
  24 # End of file

and it will work if script is run as:

  • YES: ./script --quick
  • YES: ./script -other --quick

but will stop on first argument with no "-" in front (or on --):

  • NO: ./script -bar foo --quick
  • NO: ./script -bar -- --quick

Of course, this approach (iterating over the argument list every time you want to check for one) is far less efficient than just iterating once and setting flag variables.

It also spreads the options throughout the program. The literal option --quick may appear a hundred lines down inside the main body of the program, nowhere near any other option name. This is a nightmare for maintenance.

Complex nonstandard add-on utilities

bhepple suggests the use of process-getopt (GPL licensed) and offers this example code:

PROG=$(basename $0)
VERSION='1.2'
USAGE="A tiny example using process-getopt(1)"

# call process-getopt functions to define some options:
source process-getopt

SLOT=""
SLOT_func()   { [ "${1:-""}" ] && SLOT="yes"; }      # callback for SLOT option
add_opt SLOT "boolean option" s "" slot

TOKEN=""
TOKEN_func()  { [ "${1:-""}" ] && TOKEN="$2"; }      # callback for TOKEN option
add_opt TOKEN "this option takes a value" t n token number

add_std_opts     # define the standard options --help etc:

TEMP=$(call_getopt "$@") || exit 1
eval set -- "$TEMP" # just as with getopt(1)

# remove the options from the command line
process_opts "$@" || shift "$?"

echo "SLOT=$SLOT"
echo "TOKEN=$TOKEN"
echo "args=$@"

Here, all information about each option is defined in one place making for much easier authoring and maintenance. A lot of the dirty work is handled automatically and standards are obeyed as in getopt(1) - because it calls getopt for you.

  • Actually, what the author forgot to say was that it's actually using getopts semantics, rather than getopt. I ran this test:

     ~/process-getopt-1.6$ set -- one 'rm -rf /' 'foo;bar' "'"
     ~/process-getopt-1.6$ call_getopt "$@"
      -- 'rm -rf /' 'foo;bar' ''\'''
  • It appears to be intelligent enough to handle null options, whitespace-containing options, and single-quote-containing options in a manner that makes the eval not blow up in your face. But this is not an endorsement of the process-getopt software overall; I don't know it well enough. -GreyCat

It's written and tested on Linux where getopt(1) supports long options. For portability, it tests the local getopt(1) at runtime and if it finds an non-GNU one (ie one that does not return 4 for getopt --test) it only processes short options. It does not use the bash builtin getopts(1) command. -bhepple

Rearranging arguments

If you want to mix flags and arguments here's a function to rearrange all options to place flags first. Use before doing your normal getopts parsing.

   1 arrange_opts() {
   2     local flags args optstr=$1
   3     shift
   4 
   5     while (($#)); do
   6         case $1 in
   7             --) args+=("$@")
   8                 break;
   9                 ;;
  10             -*) flags+=("$1")
  11                 if [[ $optstr == *"${1: -1}:"* ]]; then
  12                     flags+=("$2")
  13                     shift
  14                 fi
  15                 ;;
  16             * ) args+=("$1")
  17                 ;;
  18         esac
  19         shift
  20     done
  21     OPTARR=("${flags[@]}" "${args[@]}")
  22 }
  23 
  24 example() {
  25     local OPTIND OPTARR optstring=ab:cd
  26 
  27     printf 'before arrange:'
  28     printf '<%s>' "$@"
  29     echo
  30 
  31     arrange_opts "$optstring" "$@"
  32     set -- "${OPTARR[@]}"
  33 
  34     printf 'after  arrange:'
  35     printf '<%s>' "$@"
  36     printf \\n\\n
  37 
  38     printf flag:arg\\n
  39 
  40     OPTIND=1
  41     while getopts "$optstring" opt; do
  42         case $opt in
  43             a|c|d) printf        %s\\n "$opt"    ;;
  44             b    ) printf      b:%s\\n "$OPTARG" ;;
  45             \?   ) printf badopt:%s\\n "$OPTARG" ;;
  46             :    ) printf  noarg:%s\\n "$OPTARG" ;;
  47             *    ) printf    wtf:%s\\n "$opt"    ;;
  48         esac
  49     done
  50     shift $((OPTIND-1))
  51 
  52     printf \\nargs:
  53     printf '<%s>' "$@"
  54     echo
  55 }
  56 
  57 example -ad foo -b bar baz

ComplexOptionParsing (last edited 2023-12-07 09:46:56 by emanuele6)