16

我正在使用getopt(not getops) 为我的 bash 脚本提供处理选项和开关(长 --option 和短 -o 形式)的能力。

我希望能够捕获无效选项并处理它们,通常会回显用户应该尝试cmd --help然后退出脚本。

问题是,getopt 正在捕获无效选项,它本身会输出一条消息,例如“getopt: invalid option -- 'x'”

这是我用来设置 getopt 参数的模式:

set -- $(getopt -o $SHORT_OPTIONS -l $LONG_OPTIONS -- "$@")

其中 $LONG_OPTIONS 和 $SHORT_OPTIONS 都是以逗号分隔的选项列表。

以下是我处理选项的方式:

 while [ $# -gt 0 ]
    do
        case "$1" in
            -h|--help)
                cat <<END_HELP_OUTPUT

    Help
    ----

    Usage: ./cmd.sh 

    END_HELP_OUTPUT

                shift;
                exit
                ;;
            --opt1)
                FLAG1=true
                shift
                ;;
            --opt2)
                FLAG2=true
                shift
                ;;
            --)
                shift
                break
                ;;
            *)
                echo "Option $1 is not a valid option."
                echo "Try './cmd.sh --help for more information."
                shift
                exit
                ;;
        esac
    done

getopt -q将抑制输出,但我在语句中的捕获方案case仍然无法达到我的预期。相反,程序只是执行,尽管参数无效。

4

6 回答 6

14

这种风格适合我:

params="$(getopt -o d:h -l diff:,help --name "$cmdname" -- "$@")"

if [ $? -ne 0 ]
then
    usage
fi

eval set -- "$params"
unset params

while true
do
    case $1 in
        -d|--diff)
            diff_exec=(${2-})
            shift 2
            ;;
        -h|--help)
            usage
            exit
            ;;
        --)
            shift
            break
            ;;
        *)
            usage
            ;;
    esac
done
于 2011-10-06T13:56:07.560 回答
1

这不是最强大的解决方案,但它是合理的;它依赖于以下内容:

  • 打印的错误消息getopt以“getopt:”为前缀
  • 假设是,通过getopt' 错误消息的清理版本是可以接受的,并增加了自定义信息。

代码片段:

# Invoke getopt; suppress its stderr initially.
args=$(getopt -o $SHORT_OPTIONS -l $LONG_OPTIONS -- "$@" 2>/dev/null)
if [[ $? -ne 0 ]]; then # getopt reported failure
    # Rerun the same getopt command so we can capture stderr output *only* this time.
    # Inefficient (and a potential maintenance headache, if literals were involved), but this will only execute in case of invalid input.
    # Alternatively, redirect the first getopt invocation's stderr output to a temp. file and read it here.
    errmsg=$(getopt -o $SHORT_OPTIONS -l $LONG_OPTIONS -- "$@" 2>&1 1>&-)
    # Strip getopt's prefix and augment with custom information.
    echo -e "${errmsg#getopt: }\nTry './cmd.sh --help for more information." 1>&2
    exit 1
fi
于 2012-06-06T19:53:57.360 回答
1

你必须使用 getopt 吗?如果你只是使用

while [ $# -gt 0 ]; do
  case "$1" in
    -d|--diff)
       diff_exec=(${2-})
       shift
       ;;
    -h|--help)
       usage
       exit
       ;;
     --)
       break
       ;;
     *)
       usage
       ;;
    esac
    shift
done

然后你自己的代码正在做检查。

于 2013-03-30T11:19:25.203 回答
1

我发现这可以作为 getopts 案例语句中的最后一项:

*) eval echo "无法识别的参数 \$$[OPTIND-1]"; 用法; 出口 ;;

于 2013-07-19T14:22:22.427 回答
0

我不确定这是否有帮助,但getopt(1)使用getopt(3)并且如果我没记错的话,如果OPTSTRING的第一个字符是冒号, getopt(3)会抑制错误报告。

于 2011-10-05T15:28:54.757 回答
0

这是我使用过的命令行解析。它可以通过更多的解析逻辑来处理缺失的选项和参数。

对于命令行:-a AA -b BB -c CC,结果s/ba=AA b=BB c=CC

OPT=( "$@" )  # Parses the command line into words.

for [[ I=0;I<${#OPT[@]};I++ ]]  
   do
      case "${OPT[$I]}" in         
         -a) a=${OPT[$I+1]} ;;         
         -b) b=${OPT[$I+1]} ;;         
         -c) c=${OPT[$I+1]} ;;    
      esac
  done
于 2013-11-02T02:27:24.787 回答