50
while getopts "hd:R:" arg; do
  case $arg in
    h)
      echo "usage" 
      ;;
    d)
      dir=$OPTARG
      ;;
    R)
      if [[ $OPTARG =~ ^[0-9]+$ ]];then
        level=$OPTARG
      else
        level=1
      fi
      ;;
    \?)
      echo "WRONG" >&2
      ;;
  esac
done
  • level 指的是参数-R,dir 指的是参数-d

  • 当我输入 ./count.sh -R 1 -d test/它正常工作

  • 当我输入./count.sh -d test/ -R 1它正常工作

  • 但我想让它在我输入./count.sh -d test/ -R./count.sh -R -d test/

这意味着我想要-R一个默认值并且命令序列更加灵活。

4

13 回答 13

26

错误的。实际上getopts确实支持可选参数!从 bash 手册页:

If  a  required  argument is not found, and getopts is not silent, 
a question mark (?) is placed in name, OPTARG is unset, and a diagnostic
message is printed.  If getopts is silent, then a colon (:) is placed in name 
and OPTARG is set to the option character found.

当手册页显示“静默”时,表示静默错误报告。要启用它,optstring的第一个字符需要是冒号:

while getopts ":hd:R:" arg; do
    # ...rest of iverson's loop should work as posted 
done

由于 Bash 的getopt无法识别结束选项列表,因此当是最后一个选项--时,它可能不起作用,然后是一些路径参数。-R

PS:传统上,getopt.c使用两个冒号 ( ::) 来指定一个可选参数。但是,Bash 使用的版本没有。

于 2013-02-10T14:22:46.960 回答
22

getopts并不真正支持这一点;但编写自己的替代品并不难。

while true; do
    case $1 in
      -R) level=1
            shift
            case $1 in
              *[!0-9]* | "") ;;
              *) level=$1; shift ;;
            esac ;;
        # ... Other options ...
        -*) echo "$0: Unrecognized option $1" >&2
            exit 2;;
        *) break ;;
    esac
done
于 2012-07-17T07:32:13.410 回答
21

此解决方法定义不带参数的“R”(没有“:”),测试“-R”之后的任何参数(管理命令行上的最后一个选项)并测试现有参数是否以破折号开头。

# No : after R
while getopts "hd:R" arg; do
  case $arg in
  (...)
  R)
    # Check next positional parameter
    eval nextopt=\${$OPTIND}
    # existing or starting with dash?
    if [[ -n $nextopt && $nextopt != -* ]] ; then
      OPTIND=$((OPTIND + 1))
      level=$nextopt
    else
      level=1
    fi
    ;;
  (...)
  esac
done
于 2016-08-01T11:28:39.673 回答
12

我同意 Tripleee,getopts 不支持可选参数处理。

我已经确定的折衷解决方案是使用相同选项标志的大写/小写组合来区分带参数的选项和不带参数的选项。

例子:

COMMAND_LINE_OPTIONS_HELP='
Command line options:
    -I          Process all the files in the default dir: '`pwd`'/input/
    -i  DIR     Process all the files in the user specified input dir
    -h          Print this help menu

Examples:
    Process all files in the default input dir
        '`basename $0`' -I

    Process all files in the user specified input dir
        '`basename $0`' -i ~/my/input/dir

'

VALID_COMMAND_LINE_OPTIONS="i:Ih"
INPUT_DIR=

while getopts $VALID_COMMAND_LINE_OPTIONS options; do
    #echo "option is " $options
    case $options in
        h)
            echo "$COMMAND_LINE_OPTIONS_HELP"
            exit $E_OPTERROR;
        ;;
        I)
            INPUT_DIR=`pwd`/input
            echo ""
            echo "***************************"
            echo "Use DEFAULT input dir : $INPUT_DIR"
            echo "***************************"
        ;;
        i)
            INPUT_DIR=$OPTARG
            echo ""
            echo "***************************"
            echo "Use USER SPECIFIED input dir : $INPUT_DIR"
            echo "***************************"
        ;;
        \?)
            echo "Usage: `basename $0` -h for help";
            echo "$COMMAND_LINE_OPTIONS_HELP"
            exit $E_OPTERROR;
        ;;
    esac
done
于 2012-07-25T20:49:06.583 回答
8

这实际上很容易。只需在 R 之后删除尾随冒号并使用 OPTIND

while getopts "hRd:" opt; do
   case $opt in
      h) echo -e $USAGE && exit
      ;;
      d) DIR="$OPTARG"
      ;;
      R)       
        if [[ ${@:$OPTIND} =~ ^[0-9]+$ ]];then
          LEVEL=${@:$OPTIND}
          OPTIND=$((OPTIND+1))
        else
          LEVEL=1
        fi
      ;;
      \?) echo "Invalid option -$OPTARG" >&2
      ;;
   esac
done
echo $LEVEL $DIR

count.sh -d 测试

测试

count.sh -d 测试 -R

1 次测试

count.sh -R -d 测试

1 次测试

count.sh -d 测试 -R 2

2 测试

count.sh -R 2 -d 测试

2 测试

于 2017-08-01T16:27:10.627 回答
3

受@calandoa 的回答(唯一真正有效的!)的启发,我制作了一个简单的功能,可以轻松多次使用。

getopts_get_optional_argument() {
  eval next_token=\${$OPTIND}
  if [[ -n $next_token && $next_token != -* ]]; then
    OPTIND=$((OPTIND + 1))
    OPTARG=$next_token
  else
    OPTARG=""
  fi
}

一个示例用法:

while getopts "hdR" option; do
  case $option in
  d)
    getopts_get_optional_argument $@
    dir=${OPTARG}
    ;;
  R)
    getopts_get_optional_argument $@
    level=${OPTARG:-1}
    ;;
  h)
    show_usage && exit 0
    ;;
  \?)
    show_usage && exit 1
    ;;
  esac
done

这为我们提供了一种实用的方法来获得“缺少的功能getopts:)

请注意,尽管如此,带有可选参数的命令行选项似乎被明确地劝阻

准则 7:选项参数不应是可选的。

但是如果没有这个,我没有直观的方法来实现我的案例:我有 2 种模式可以通过使用一个标志或其他标志来激活,并且它们都有一个带有明确默认值的参数。引入第三个标志只是为了消除歧义,使它看起来是一种糟糕的 CLI 风格。

我已经用许多组合对此进行了测试,包括@aaron-sua 的答案中的所有组合并且效果很好。

于 2019-07-31T17:15:53.590 回答
0

尝试:

while getopts "hd:R:" arg; do
  case $arg in
    h)
      echo "usage" 
    ;;
    d)
      dir=$OPTARG
    ;;
    R)
      if [[ $OPTARG =~ ^[0-9]+$ ]];then
        level=$OPTARG
      elif [[ $OPTARG =~ ^-. ]];then
        level=1
        let OPTIND=$OPTIND-1
      else
        level=1
      fi          
    ;;
    \?)
      echo "WRONG" >&2
    ;;
  esac
done

我认为上面的代码在仍然使用getopts. getopts遇到时,我在您的代码中添加了以下三行-R

      elif [[ $OPTARG =~ ^-. ]];then
        level=1
        let OPTIND=$OPTIND-1

如果-R遇到并且第一个参数看起来像另一个 getopts 参数,则将 level 设置为默认值1,然后将$OPTIND变量减一。下一次getopts去抓取一个参数,它会抓取正确的参数而不是跳过它。


这是基于Jan Schampera 在本教程中的评论中的代码的类似示例:

#!/bin/bash
while getopts :abc: opt; do
  case $opt in
    a)
      echo "option a"
    ;;
    b)
      echo "option b"
    ;;
    c)
      echo "option c"

      if [[ $OPTARG = -* ]]; then
        ((OPTIND--))
        continue
      fi

      echo "(c) argument $OPTARG"
    ;;
    \?)
      echo "WTF!"
      exit 1
    ;;
  esac
done

当您发现 OPTARG von -c 是以连字符开头的内容时,请重置 OPTIND 并重新运行 getopts(继续 while 循环)。哦,当然,这并不完美,需要更多的稳健性。这只是一个例子。

于 2013-12-03T21:15:40.073 回答
0

以下代码通过检查前导破折号来解决此问题,如果找到则减少 OPTIND 以指向跳过的选项进行处理。这通常可以正常工作,只是您不知道用户在命令行上放置选项的顺序 - 如果您的可选参数选项是最后一个并且不提供参数,getopts 会出错。

为了解决最后一个参数丢失的问题,“$@”数组只需附加一个空字符串“$@”,这样 getopts 就会满足于它已经吞噬了另一个选项参数。为了修复这个新的空参数,设置了一个变量来保存要处理的所有选项的总数 - 当处理最后一个选项时,调用一个名为 trim 的辅助函数,并在使用该值之前删除空字符串。

这不是工作代码,它只有占位符,但您可以轻松修改它,并且稍加注意,它对于构建一个健壮的系统很有用。

#!/usr/bin/env bash 
declare  -r CHECK_FLOAT="%f"  
declare  -r CHECK_INTEGER="%i"  

 ## <arg 1> Number - Number to check
 ## <arg 2> String - Number type to check
 ## <arg 3> String - Error message
function check_number() {
  local NUMBER="${1}"
  local NUMBER_TYPE="${2}"
  local ERROR_MESG="${3}"
  local FILTERED_NUMBER=$(sed 's/[^.e0-9+\^]//g' <<< "${NUMBER}")
  local -i PASS=1
  local -i FAIL=0
    if [[ -z "${NUMBER}" ]]; then 
        echo "Empty number argument passed to check_number()." 1>&2
        echo "${ERROR_MESG}" 1>&2
        echo "${FAIL}"          
  elif [[ -z "${NUMBER_TYPE}" ]]; then 
        echo "Empty number type argument passed to check_number()." 1>&2
        echo "${ERROR_MESG}" 1>&2
        echo "${FAIL}"          
  elif [[ ! "${#NUMBER}" -eq "${#FILTERED_NUMBER}" ]]; then 
        echo "Non numeric characters found in number argument passed to check_number()." 1>&2
        echo "${ERROR_MESG}" 1>&2
        echo "${FAIL}"          
  else  
   case "${NUMBER_TYPE}" in
     "${CHECK_FLOAT}")
         if ((! $(printf "${CHECK_FLOAT}" "${NUMBER}" &>/dev/random;echo $?))); then
            echo "${PASS}"
         else
            echo "${ERROR_MESG}" 1>&2
            echo "${FAIL}"
         fi
         ;;
     "${CHECK_INTEGER}")
         if ((! $(printf "${CHECK_INTEGER}" "${NUMBER}" &>/dev/random;echo $?))); then
            echo "${PASS}"
         else
            echo "${ERROR_MESG}" 1>&2
            echo "${FAIL}"
         fi
         ;;
                      *)
         echo "Invalid number type format: ${NUMBER_TYPE} to check_number()." 1>&2
         echo "${FAIL}"
         ;;
    esac
 fi 
}

 ## Note: Number can be any printf acceptable format and includes leading quotes and quotations, 
 ##       and anything else that corresponds to the POSIX specification. 
 ##       E.g. "'1e+03" is valid POSIX float format, see http://mywiki.wooledge.org/BashFAQ/054
 ## <arg 1> Number - Number to print
 ## <arg 2> String - Number type to print
function print_number() { 
  local NUMBER="${1}" 
  local NUMBER_TYPE="${2}" 
  case "${NUMBER_TYPE}" in 
      "${CHECK_FLOAT}") 
           printf "${CHECK_FLOAT}" "${NUMBER}" || echo "Error printing Float in print_number()." 1>&2
        ;;                 
    "${CHECK_INTEGER}") 
           printf "${CHECK_INTEGER}" "${NUMBER}" || echo "Error printing Integer in print_number()." 1>&2
        ;;                 
                     *) 
        echo "Invalid number type format: ${NUMBER_TYPE} to print_number()." 1>&2
        ;;                 
   esac
} 

 ## <arg 1> String - String to trim single ending whitespace from
function trim_string() { 
 local STRING="${1}" 
 echo -En $(sed 's/ $//' <<< "${STRING}") || echo "Error in trim_string() expected a sensible string, found: ${STRING}" 1>&2
} 

 ## This a hack for getopts because getopts does not support optional
 ## arguments very intuitively. E.g. Regardless of whether the values
 ## begin with a dash, getopts presumes that anything following an
 ## option that takes an option argument is the option argument. To fix  
 ## this the index variable OPTIND is decremented so it points back to  
 ## the otherwise skipped value in the array option argument. This works
 ## except for when the missing argument is on the end of the list,
 ## in this case getopts will not have anything to gobble as an
 ## argument to the option and will want to error out. To avoid this an
 ## empty string is appended to the argument array, yet in so doing
 ## care must be taken to manage this added empty string appropriately.
 ## As a result any option that doesn't exit at the time its processed
 ## needs to be made to accept an argument, otherwise you will never
 ## know if the option will be the last option sent thus having an empty
 ## string attached and causing it to land in the default handler.
function process_options() {
local OPTIND OPTERR=0 OPTARG OPTION h d r s M R S D
local ERROR_MSG=""  
local OPTION_VAL=""
local EXIT_VALUE=0
local -i NUM_OPTIONS
let NUM_OPTIONS=${#@}+1
while getopts “:h?d:DM:R:S:s:r:” OPTION "$@";
 do
     case "$OPTION" in
         h)
             help | more
             exit 0
             ;;
         r)
             OPTION_VAL=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             ERROR_MSG="Invalid input: Integer or floating point number required."
             if [[ -z "${OPTION_VAL}" ]]; then
               ## can set global flags here 
               :;
             elif [[ "${OPTION_VAL}" =~ ^-. ]]; then
               let OPTIND=${OPTIND}-1
               ## can set global flags here 
             elif [ "${OPTION_VAL}" = "0" ]; then
               ## can set global flags here 
               :;               
             elif (($(check_number "${OPTION_VAL}" "${CHECK_FLOAT}" "${ERROR_MSG}"))); then
               :; ## do something really useful here..               
             else
               echo "${ERROR_MSG}" 1>&2 && exit -1
             fi
             ;;
         d)
             OPTION_VAL=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             [[  ! -z "${OPTION_VAL}" && "${OPTION_VAL}" =~ ^-. ]] && let OPTIND=${OPTIND}-1            
             DEBUGMODE=1
             set -xuo pipefail
             ;;
         s)
             OPTION_VAL=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             if [[ ! -z "${OPTION_VAL}" && "${OPTION_VAL}" =~ ^-. ]]; then ## if you want a variable value that begins with a dash, escape it
               let OPTIND=${OPTIND}-1
             else
              GLOBAL_SCRIPT_VAR="${OPTION_VAL}"
                :; ## do more important things
             fi
             ;;
         M)  
             OPTION_VAL=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             ERROR_MSG=$(echo "Error - Invalid input: ${OPTION_VAL}, Integer required"\
                              "retry with an appropriate option argument.")
             if [[ -z "${OPTION_VAL}" ]]; then
               echo "${ERROR_MSG}" 1>&2 && exit -1
             elif [[ "${OPTION_VAL}" =~ ^-. ]]; then
               let OPTIND=${OPTIND}-1
               echo "${ERROR_MSG}" 1>&2 && exit -1
             elif (($(check_number "${OPTION_VAL}" "${CHECK_INTEGER}" "${ERROR_MSG}"))); then
             :; ## do something useful here
             else
               echo "${ERROR_MSG}" 1>&2 && exit -1
             fi
             ;;                      
         R)  
             OPTION_VAL=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             ERROR_MSG=$(echo "Error - Invalid option argument: ${OPTION_VAL},"\
                              "the value supplied to -R is expected to be a "\
                              "qualified path to a random character device.")            
             if [[ -z "${OPTION_VAL}" ]]; then
               echo "${ERROR_MSG}" 1>&2 && exit -1
             elif [[ "${OPTION_VAL}" =~ ^-. ]]; then
               let OPTIND=${OPTIND}-1
               echo "${ERROR_MSG}" 1>&2 && exit -1
             elif [[ -c "${OPTION_VAL}" ]]; then
               :; ## Instead of erroring do something useful here..  
             else
               echo "${ERROR_MSG}" 1>&2 && exit -1
             fi
             ;;                      
         S)  
             STATEMENT=$(((${NUM_OPTIONS}==${OPTIND})) && trim_string "${OPTARG##*=}" || echo -En "${OPTARG##*=}")
             ERROR_MSG="Error - Default text string to set cannot be empty."
             if [[ -z "${STATEMENT}" ]]; then
               ## Instead of erroring you could set a flag or do something else with your code here..  
             elif [[ "${STATEMENT}" =~ ^-. ]]; then ## if you want a statement that begins with a dash, escape it
               let OPTIND=${OPTIND}-1
               echo "${ERROR_MSG}" 1>&2 && exit -1
               echo "${ERROR_MSG}" 1>&2 && exit -1
             else
                :; ## do something even more useful here you can modify the above as well 
             fi
             ;;                      
         D)  
             ## Do something useful as long as it is an exit, it is okay to not worry about the option arguments 
             exit 0
             ;;          
         *)
             EXIT_VALUE=-1
             ;&                  
         ?)
             usage
             exit ${EXIT_VALUE}
             ;;
     esac
done
}

process_options "$@ " ## extra space, so getopts can find arguments  
于 2015-03-13T00:18:46.650 回答
0

您始终可以决定用小写或大写来区分选项。

但是我的想法是调用getopts两次和第一次解析,不带参数忽略它们(R),然后第二次只解析带有参数支持的选项(R:)。唯一的技巧是OPTIND(index) 在处理期间需要更改,因为它保持指向当前参数的指针。

这是代码:

#!/usr/bin/env bash
while getopts ":hd:R" arg; do
  case $arg in
    d) # Set directory, e.g. -d /foo
      dir=$OPTARG
      ;;
    R) # Optional level value, e.g. -R 123
      OI=$OPTIND # Backup old value.
      ((OPTIND--)) # Decrease argument index, to parse -R again.
      while getopts ":R:" r; do
        case $r in
          R)
            # Check if value is in numeric format.
            if [[ $OPTARG =~ ^[0-9]+$ ]]; then
              level=$OPTARG
            else
              level=1
            fi
          ;;
          :)
            # Missing -R value.
            level=1
          ;;
        esac
      done
      [ -z "$level" ] && level=1 # If value not found, set to 1.
      OPTIND=$OI # Restore old value.
      ;;
    \? | h | *) # Display help.
      echo "$0 usage:" && grep " .)\ #" $0
      exit 0
      ;;
  esac
done
echo Dir: $dir
echo Level: $level

以下是一些适用于场景的测试:

$ ./getopts.sh -h
./getopts.sh usage:
    d) # Set directory, e.g. -d /foo
    R) # Optional level value, e.g. -R 123
    \? | h | *) # Display help.
$ ./getopts.sh -d /foo
Dir: /foo
Level:
$ ./getopts.sh -d /foo -R
Dir: /foo
Level: 1
$ ./getopts.sh -d /foo -R 123
Dir: /foo
Level: 123
$ ./getopts.sh -d /foo -R wtf
Dir: /foo
Level: 1
$ ./getopts.sh -R -d /foo
Dir: /foo
Level: 1

不起作用的场景(因此代码需要更多调整):

$ ./getopts.sh -R 123 -d /foo
Dir:
Level: 123

有关getopts使用的更多信息,请参见man bash

另请参阅:Bash Hackers Wiki 上的小型 getopts 教程

于 2015-12-30T13:57:37.253 回答
0

我自己也遇到了这个问题,觉得现有的解决方案都不是真正干净的。在进行了一些工作并尝试了各种事情之后,我发现利用 getopts SILENT 模式:) ...似乎已经完成了这个技巧,同时保持了 OPTIND 同步。


usage: test.sh [-abst] [-r [DEPTH]] filename
*NOTE: -r (recursive) with no depth given means full recursion

#!/usr/bin/env bash

depth='-d 1'

while getopts ':abr:st' opt; do
    case "${opt}" in
        a) echo a;;
        b) echo b;;
        r) if [[ "${OPTARG}" =~ ^[0-9]+$ ]]; then
               depth="-d ${OPTARG}"
           else
               depth=
               (( OPTIND-- ))
           fi
           ;;
        s) echo s;;
        t) echo t;;
        :) [[ "${OPTARG}" = 'r' ]] && depth=;;
        *) echo >&2 "Invalid option: ${opt}"; exit 1;;
    esac
done
shift $(( OPTIND - 1 ))

filename="$1"
...
于 2019-01-14T04:20:50.367 回答
0

我认为有两种方法。

首先是 calandoa 的回答,使用 OPTIND 并且没有静音模式。

其次是使用 OPTIND 和静默模式。

while getopts ":Rr:" name; do
    case ${name} in
        R)
            eval nextArg=\${$OPTIND}
            # check option followed by nothing or other option.
            if [[ -z ${nextArg} || $nextArg =~ ^-.* ]]; then
                level=1
            elif [[ $nextArg =~ ^[0-9]+$ ]]; then
                level=$nextArg
                OPTIND=$((OPTIND + 1))
            else
                level=1
            fi
            ;;
        r)
            # check option followed by other option.
            if [[ $OPTARG =~ ^-.* ]]; then
                OPTIND=$((OPTIND - 1))
                level2=2
            elif [[ $OPTARG =~ ^[0-9]+$ ]]; then
                level2="$OPTARG"
            else
                level2=2
            fi
            ;;
        :)
            # check no argument
            case $OPTARG in
                r)
                    level2=2
                    ;;
            esac
    esac
done

echo "Level 1 : $level"
echo "Level 2 : $level2"
于 2020-11-26T08:32:44.363 回答
0

到目前为止提供的所有解决方案都将代码放入case ... in ... esac,但在我看来,修改命令会更自然getopts,因此我编写了这个函数:

编辑:

现在,您可以指定可选参数的类型(请参阅使用信息)。

此外,$nextArg该函数现在不再测试是否“看起来像”一个选项 arg,而是检查是否$nextArg包含来自$optstring. 这样,不包含在其中的选项字母$optstring可以用作可选参数,与getopts' 强制参数一样。

最新变化:

  • 固定测试 if$nextArg是一个选项 arg:
    测试是否$nextArg以破折号开头。如果没有此测试,则无法识别
    包含 from 字母的可选参数。$optstring
  • 添加了正则表达式类型说明符(请参阅使用信息)。
  • 固定:0 未被识别为指定为 int 的可选 arg。
  • 简化测试 if$nextArg是一个 int。
  • 类型说明符::/.../:用于perl测试是否$nextArg匹配正则表达式。
    这样,您就可以从(几乎 (*))Perl 正则表达式的全部功能中受益。
    (*):见最后一段使用信息。
  • 已修复:不适用于一个以上的正则表达式类型说明符:
    使用perl而不是grep/sed构造,因为需要非贪婪匹配。

用法:

调用:getopts-plus optstring name "$@"

optstring: 和普通的一样,但是你可以通过在选项字母后面附加::getopts来指定带有可选参数的选项。

但是,如果您的脚本支持使用带有可选参数作为唯一选项参数的选项的调用,然后是非选项参数,则非选项参数将被视为该选项的参数。

如果你很幸运并且可选参数应该是一个整数,而非选项参数是一个字符串,反之亦然,你可以通过附加:::i为整数或:::s来指定类型字符串来解决这个问题。

如果这不适用,您可以通过将::/.../附加到选项字母来为可选参数指定 Perl 正则表达式。
有关 Perl 正则表达式的介绍,请参见此处:https ://perldoc.perl.org/perlretut
请注意:ATM,只会/.../在 之后被识别为正则表达式::,即不能使用其他分隔符,也不能使用修饰符,因此 egm#...#a不会认可。
如果在带有可选参数的选项之后有一个非选项参数,那么只有匹配正则表达式时才会被认为是可选参数。
需要明确的是:::/.../并非用于参数验证,而仅用于区分具有可选参数和非选项参数的选项的参数。

#!/bin/bash

# Invocation: getopts-plus optstring name "$@"\
# \
# optstring: Like normal getopts, but you may specify options with optional argument
# by appending :: to the option letter.\
# \
# However, if your script supports an invocation with an option with optional
# argument as the only option argument, followed by a non-option argument,
# the non-option argument will be considered to be the argument for the option.\
# \
# If you're lucky and the optional argument is expected to be an integer, whereas
# the non-option argument is a string or vice versa, you may specify the type by
# appending :::i for an integer or :::s for a string to solve that issue.\
# \
# If that doesn't apply, you may specify a Perl regexp for the optional arg by appending
# ::/.../ to the option letter.\
# See here for an introduction to Perl regexps: https://perldoc.perl.org/perlretut
# Please note: ATM, only /.../ will be recognised as a regexp after ::,\
# i. e. neither other delimiters, nor modifiers may be used, so e. g. m#...#a will
# not be recognised.\
# If there is a non-option argument after the option with optional argument, it will
# be considered to be the optional argument only if it matches the regexp.\
# To be clear: ::/.../ is not meant for argument validation but solely to discriminate
# between arguments for options with optional argument and non-option arguments.
function getopts-plus
{
    local optstring=$1
    local -n name=$2

    shift 2

    local optionalArgSuffixRE='::(?::[si]|/.*?/)?'
    local optionalArgTypeCaptureRE=':::([si])|::(/.*?/)'

    # If we pass 'opt' for 'name' (as I always do when using getopts) and there is
    # also a local variable 'opt', the "outer" 'opt' will always be empty.
    # I don't understand why a local variable interferes with caller's variable with
    # same name in this case; however, we can easily circumvent this.
    local opt_

    # Extract options with optional arg

    local -A isOptWithOptionalArg

    while read opt_; do
        # Using an associative array as set
        isOptWithOptionalArg[$opt_]=1
    done <<<$(perlGetCaptures "$optstring" "([a-zA-Z])$optionalArgSuffixRE")

    # Extract all option letters (used to weed out possible optional args that are option args)
    local optLetters=$(perlGetCaptures "$optstring" "([a-zA-Z])(?:$optionalArgSuffixRE|:)?")

    # Save original optstring, then remove our suffix(es)
    local optstringOrg=$optstring
    optstring=$(perl -pe "s#$optionalArgSuffixRE##g" <<<$optstring)

    getopts $optstring name "$@" || return # Return value is getopts' exit value.

    # If current option is an option with optional arg and if an arg has been provided,
    # check if that arg is not an option and if it isn't, check if that arg matches(*)
    # the specified type, if any, and if it does or no type has been specified,
    # assign it to OPTARG and inc OPTIND.
    #
    # (*) We detect an int because it's easy, but we assume a string if it's not an int
    # because detecting a string would be complicated.
    # So it sounds strange to call it a match if we know that the optional arg is specified
    # to be a string, but merely that the provided arg is not an int, but in this context,
    # "not an int" is equivalent to "string". At least I think so, but I might be wrong.

    if ((isOptWithOptionalArg[$name])) && [[ ${!OPTIND} ]]; then
        local nextArg=${!OPTIND} foundOpt=0

        # Test if $nextArg is an option arg
        if [[ $nextArg == -* ]]; then
            # Check if $nextArg contains a letter from $optLetters.
            # This way, an option not contained in $optstring can be
            # used as optional arg, as with getopts' mandatory args.

            local i

            # Start at char 1 to skip the leading dash
            for ((i = 1; i < ${#nextArg}; i++)); do
                while read opt_; do
                    [[ ${nextArg:i:1} == $opt_ ]] && foundOpt=1 && break 2
                done <<<$optLetters
            done

            ((foundOpt)) && return
        fi

        # Extract type of optional arg if specified
        local optArgType=$(perlGetCaptures "$optstringOrg" "$name(?:$optionalArgTypeCaptureRE)" '$1$2')

        local nextArgIsOptArg=0

        case $optArgType in
            /*/) # Check if $nextArg matches regexp
                perlMatch "$nextArg" "$optArgType" && nextArgIsOptArg=1
                ;;
            [si]) # Check if $nextArg is an int
                local nextArgIsInt=0

                [[ $nextArg =~ ^[0-9]+$ ]] && nextArgIsInt=1

                # Test if specified type and arg type match (see (*) above).
                # N.B.: We need command groups since && and || between commands have same precedence.
                { [[ $optArgType == i ]] && ((nextArgIsInt)) || { [[ $optArgType == s ]] && ((! nextArgIsInt)); }; } && nextArgIsOptArg=1
                ;;
            '') # No type or regexp specified => Assume $nextArg is optional arg.
                nextArgIsOptArg=1
                ;;
        esac

        if ((nextArgIsOptArg)); then
            OPTARG=$nextArg && ((OPTIND++))
        fi
    fi
}

# Uses perl to match \<string\> against \<regexp\>.\
# Returns with code 0 on a match and 1 otherwise.
function perlMatch # Args: <string> <regexp>
{
    perl -e 'q('"$1"') =~ '"$2"' and exit 0; exit 1;'
}

# Uses perl to match \<string\> against \<regexp\>
# and prints each capture on a separate line.\
# If \<regexp\> contains more than one capture group,
# you must specify the \<line format\> which is an
# arbitrary Perl string containing your desired backrefs.\
# By default, merely $1 will be printed.
function perlGetCaptures # Args: <string> <regexp> [<line format>]
{
    local lineFmt=${3:-\$1}

    # Matching repeatedly with g option gives one set of captures at a time.
    perl -e 'while (q('"$1"') =~ m#'"$2"'#g) { print(qq('"$lineFmt"') . "\n"); }'
}

如果您不需要它们,则函数体内没有注释的相同脚本:

#!/bin/bash

# Invocation: getopts-plus optstring name "$@"\
# \
# optstring: Like normal getopts, but you may specify options with optional argument
# by appending :: to the option letter.\
# \
# However, if your script supports an invocation with an option with optional
# argument as the only option argument, followed by a non-option argument,
# the non-option argument will be considered to be the argument for the option.\
# \
# If you're lucky and the optional argument is expected to be an integer, whereas
# the non-option argument is a string or vice versa, you may specify the type by
# appending :::i for an integer or :::s for a string to solve that issue.\
# \
# If that doesn't apply, you may specify a Perl regexp for the optional arg by appending
# ::/.../ to the option letter.\
# See here for an introduction to Perl regexps: https://perldoc.perl.org/perlretut
# Please note: ATM, only /.../ will be recognised as a regexp after ::,\
# i. e. neither other delimiters, nor modifiers may be used, so e. g. m#...#a will
# not be recognised.\
# If there is a non-option argument after the option with optional argument, it will
# be considered to be the optional argument only if it matches the regexp.\
# To be clear: ::/.../ is not meant for argument validation but solely to discriminate
# between arguments for options with optional argument and non-option arguments.
function getopts-plus
{
    local optstring=$1
    local -n name=$2

    shift 2

    local optionalArgSuffixRE='::(?::[si]|/.*?/)?'
    local optionalArgTypeCaptureRE=':::([si])|::(/.*?/)'

    local opt_

    local -A isOptWithOptionalArg

    while read opt_; do
        isOptWithOptionalArg[$opt_]=1
    done <<<$(perlGetCaptures "$optstring" "([a-zA-Z])$optionalArgSuffixRE")

    local optLetters=$(perlGetCaptures "$optstring" "([a-zA-Z])(?:$optionalArgSuffixRE|:)?")

    local optstringOrg=$optstring
    optstring=$(perl -pe "s#$optionalArgSuffixRE##g" <<<$optstring)

    getopts $optstring name "$@" || return

    if ((isOptWithOptionalArg[$name])) && [[ ${!OPTIND} ]]; then
        local nextArg=${!OPTIND} foundOpt=0

        if [[ $nextArg == -* ]]; then
            local i

            for ((i = 1; i < ${#nextArg}; i++)); do
                while read opt_; do
                    [[ ${nextArg:i:1} == $opt_ ]] && foundOpt=1 && break 2
                done <<<$optLetters
            done

            ((foundOpt)) && return
        fi

        local optArgType=$(perlGetCaptures "$optstringOrg" "$name(?:$optionalArgTypeCaptureRE)" '$1$2')

        local nextArgIsOptArg=0

        case $optArgType in
            /*/)
                perlMatch "$nextArg" "$optArgType" && nextArgIsOptArg=1
                ;;
            [si])
                local nextArgIsInt=0

                [[ $nextArg =~ ^[0-9]+$ ]] && nextArgIsInt=1

                { [[ $optArgType == i ]] && ((nextArgIsInt)) || { [[ $optArgType == s ]] && ((! nextArgIsInt)); }; } && nextArgIsOptArg=1
                ;;
            '')
                nextArgIsOptArg=1
                ;;
        esac

        if ((nextArgIsOptArg)); then
            OPTARG=$nextArg && ((OPTIND++))
        fi
    fi
}

# Uses perl to match \<string\> against \<regexp\>.\
# Returns with code 0 on a match and 1 otherwise.
function perlMatch # Args: <string> <regexp>
{
    perl -e 'q('"$1"') =~ '"$2"' and exit 0; exit 1;'
}

# Uses perl to match \<string\> against \<regexp\>
# and prints each capture on a separate line.\
# If \<regexp\> contains more than one capture group,
# you must specify the \<line format\> which is an
# arbitrary Perl string containing your desired backrefs.\
# By default, merely $1 will be printed.
function perlGetCaptures # Args: <string> <regexp> [<line format>]
{
    local lineFmt=${3:-\$1}

    perl -e 'while (q('"$1"') =~ m#'"$2"'#g) { print(qq('"$lineFmt"') . "\n"); }'
}

使用最新版本的一些测试:

指定为整数的可选 arg 类型-g,不传递 int 但后跟非选项字符串 arg。

$ . ./getopts-plus.sh
$ while getopts-plus 'b:c::de::f::g:::ia' opt -ab 99 -c 11 -def 55 -g "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == '' <-- Empty because "hello you" is not an int

像上面一样,但使用 int arg。

$ OPTIND=1
$ while getopts-plus 'b:c::de::f::g:::ia' opt -ab 99 -c 11 -def 55 -g 7 "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == '7' <-- The passed int

-h添加了带有 regexp的可选选项/^(a|b|ab|ba)$/,没有传递参数。

$ OPTIND=1
$ while getopts-plus 'b:c::de::f::g:::ih::/^(a|b|ab|ba)$/a' opt -ab 99 -c 11 -def 55 -gh "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == ''

-------------------------

opt == 'h'

OPTARG == '' <-- Empty because "hello you" does not match the regexp

像上面一样,但 arg 匹配正则表达式。

$ OPTIND=1
$ while getopts-plus 'b:c::de::f::g:::ih::/^(a|b|ab|ba)$/a' opt -ab 99 -c 11 -def 55 -gh ab "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == ''

-------------------------

opt == 'h'

OPTARG == 'ab' <-- The arg that matches the regexp

-i添加了另一个带有正则表达式的正则表达式类型的可选选项/^\w+$/(使用 Perl 标记\w,表示字母数字或下划线),没有传递参数。

$ OPTIND=1
$ while getopts-plus 'b:c::de::f::g:::ih::/^(a|b|ab|ba)$/ai::/^\w+$/' opt -ab 99 -c 11 -def 55 -gh ab -i "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done
[23:10:49]

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == ''

-------------------------

opt == 'h'

OPTARG == 'ab'

-------------------------

opt == 'i'

OPTARG == '' <-- Empty because "hello you" contains a space.

像上面一样,但 arg 匹配正则表达式。

$ OPTIND=1
$ while getopts-plus 'b:c::de::f::g:::ih::/^(a|b|ab|ba)$/ai::/^\w+$/' opt -ab 99 -c 11 -def 55 -gh ab -i foo_Bar_1 "hello you"; do e opt OPTARG; echo; printf "%.0s-" $(seq 1 25); echo -e "\n"; done
[23:15:23]

opt == 'a'

OPTARG == ''

-------------------------

opt == 'b'

OPTARG == '99'

-------------------------

opt == 'c'

OPTARG == '11'

-------------------------

opt == 'd'

OPTARG == ''

-------------------------

opt == 'e'

OPTARG == ''

-------------------------

opt == 'f'

OPTARG == '55'

-------------------------

opt == 'g'

OPTARG == ''

-------------------------

opt == 'h'

OPTARG == 'ab'

-------------------------

opt == 'i'

OPTARG == 'foo_Bar_1' <-- Matched because it contains only alphanumeric chars and underscores.
于 2021-07-06T20:06:23.393 回答
0

旧线程,但我想无论如何我都会分享我所做的(这也大多比这个线程更老)。我厌倦了试图让 getopt 做我想做的事,并且在沮丧中这样做是为了支持带有可选参数的短选项和长选项。这是很长的路要走,肯定有人会笑,但它完全按照我想要的方式工作 - 所有三种情况的过度评论示例如下:

#!/usr/bin/bash
# Begin testme.sh

shopt -s extglob;
VERSION="1.0"

function get_args(){
  while test -n "${1}" ; do
    case "${1}" in
      -a | --all)
         # dumb single argument example
         PROCESS_ALL="yes"
         shift 1
      ;;
      -b | --buildnum)
         # requires a second argument so use check_arg() below
         check_arg $1 $2
         BUILD_NUM=${2}
         shift 2
      ;;
      -c | --cache)
        # Example where argument is not required, don't use check_arg()
        if [ echo ${2} | grep -q "^-" ]; then
          # no argument given, use default cache value
          CACHEDIR=~/mycachedir
          # Note: this could have been set upon entering the script
          #       and used the negative above as well
          shift 1
        else
          cache=${2}
          shift 2
        fi
      ;;
      -h | --help)
        showhelp
        exit 0
      ;;
      -v | --version)
        echo -e "$(basename ${0}) ${VERSION}\n"
        exit 0
      ;;
      # Handle getopt style short args (reason for shopts above)
      -+([a-z,A-Z]))
        # split up the arguments and call recursively with trailing break
        arg="${1}"
        newargs=$( echo ${1} | sed 's@-@@' | \
                               sed 's/.\{1\}/& /g' | \
                               sed 's/[^ ]* */-&/g')
        newargs="${newargs} $(echo ${@} | sed "s@${arg}@@")"
        get_args ${newargs}
        break;
      ;;
      *)
        echo -e "Invalid argument ${1}!\n\n"
        showhelp
        exit 1
      ;;
    esac
  done
}

# Super lazy, but I didn't want an if/then/else for every required arg
function check_arg(){
  if [ echo "${2}" | grep -q "^-" ]; then
    echo "Error:  $1 requires a valid argument."
    exit 1
  fi
}

function showhelp(){
  echo ""
  echo "`basename ${0}` is a utility to..."
}

# Process command line arguments
get_args $@
...
# End testme.sh

我从来没有遇到过它,但我想可能存在我需要第二个参数以“-”字符开头的情况,在这种情况下,我会在调用 get_args() 之前将其删除。我也使用了 with 固定位置参数,在这种情况下,它们在最后,但相同的解决方案。另外,我认为便携式版本可以只处理 *) 中的组合短 args,但我认为如果 bash 的要求太高,那你就靠你自己了。

于 2021-08-07T06:36:31.780 回答