对于bash命令行参数验证库 [英] Command line argument validation library for Bash

查看:121
本文介绍了对于bash命令行参数验证库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个可重复使用的code段,做的命令行参数验证对于bash。

在理想情况下一个类似于由Apache的百科全书CLI提供的功能:

共享CLI支持不同类型的选项:


  • POSIX类似的选项(即焦油-zxvf的foo.tar.gz)

  • GNU像长选项(即杜--human可读--max深度= 1)

  • 短选项附加价值(即GCC -O2 foo.c的)

  • 长选项用连字符(即蚂蚁-projecthelp)

  • ...

和它自动生成程序中的用法的消息,像这样的:

 用法:LS
 -A, - 几乎所有的没有列出暗示。和..
 -a, - 都不隐瞒开头的条目。
 -B, - 忽略的备份不列出隐含entried结尾〜
 -b, - 打印逃生八进制溢出的非图形字符
    --block尺寸和LT; SIZE>使用SIZE字节的块
 -c与-lt:排序,并显示,的ctime(上一次
                          文件状态信息修改)与
                          -l:显示ctime但根据名称排序否则:排序
                          通过的ctime
 按列-C列表条目

我想包括我的bash脚本的开头这样code段和整个脚本重复使用它。

必须有这样的事情。我不相信我们都写code到这种效果或类似的:

 #!/斌/庆典NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS = 3number_of_supplied_command_line_arguments = $#功能show_command_usage(){
  回声用法:
  (......)
}如果((number_of_supplied_command_line_arguments&下; NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS));然后
  show_command_usage
  出口
科幻...


解决方案

这是我使用的解决方案(发现它在网络上的某个地方,也许在这里本身,不记得是肯定的)。请注意,GNU getopt的(的/ usr / bin中/ getopt的)不支持单短线长选项(蚂蚁-projecthelp 使用选项 -a 风格),但我还没有那么它不是示例中显示用它。

这code解析为3个选项: - 主机值 -h值 - 端口值 -p值 - 表值 -t值。在的情况下需要的参数没有设置,对于它的测试是

 #获取和分析使用的/ usr / bin中/ getopt的选项
OPTIONS = $(getopt的-o H:P:T:--long主机:端口:表:-n$ 0 - $ @)
#注意周围的`$ OPTIONS'引号:他们是在处理空间至关重要
#选项值!
EVAL集 - $ OPTIONS而真实的;做
    案$ 1
            -h | --host)HOST = $ 2;快步2 ;;
            -t | --table)表= $ 2;快步2 ;;
            -p | --port)
                    案$ 2
                            )PORT = 1313;快步2 ;;
                            *)PORT = $ 2;快步2 ;;
                    ESAC ;;
             - )转变;打破;;
            *)回声内部错误! ;出口1 ;;
    ESAC
DONE
如果[[-z$ HOST]] || [[-z$ TABLE]] || [[-z$ PORT]];然后
    用法()
    出口
如果

这是另一种实现使用 getopts的 shell内建(这仅支持小选项):

 而getopts的:H:P:T:选项;做
    案$选项,在
         高)HOST = $ OPTARG ;;
         P)PORT = $ OPTARG ;;
         T)表= $ OPTARG ;;
        *)使用();出口1 ;;
    ESAC
DONE
如果[[-z$ HOST]] || [[-z$ TABLE]] || [[-z$ PORT]];然后
    用法()
    出口
如果移$((OPTIND - 1))

进一步阅读 GNU getopt的并的 getopts的bash的内建

I am looking for a reusable code snippet that does command line argument validation for bash.

Ideally something akin to the functionality offered by Apache Commons CLI:

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)
  • ...

and it generates a "usage" message for the program automatically, like this:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entried ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns

I would include this code snippet at the beginning of my Bash scripts and reuse it across scripts.

There must be something like this. I don't believe we are all writing code to this effect or similar:

#!/bin/bash

NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS=3

number_of_supplied_command_line_arguments=$#

function show_command_usage() {
  echo usage:
  (...)
}

if (( number_of_supplied_command_line_arguments < NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS )); then
  show_command_usage
  exit
fi

...

解决方案

This is the solution I use (found it on the net somewhere, probably here itself, don't remember for sure). Please note that the GNU getopt (/usr/bin/getopt) does support single dash long options (ant -projecthelp style) using the option -a, however I haven't used it so it is not shown in the example.

This code parses for 3 options: --host value or -h value, --port value or -p value and --table value or -t value. In case the required parameter isn't set, a test for it is

# Get and parse options using /usr/bin/getopt
OPTIONS=$(getopt -o h:p:t: --long host:,port:,table: -n "$0" -- "$@")
# Note the quotes around `$OPTIONS': they are essential for handling spaces in 
# option values!
eval set -- "$OPTIONS"

while true ; do
    case "$1" in
            -h|--host) HOST=$2 ; shift 2 ;;
            -t|--table)TABLE=$2 ; shift 2 ;;
            -p|--port)
                    case "$2" in
                            "") PORT=1313; shift 2 ;;
                            *)  PORT=$2; shift 2 ;;
                    esac;;
            --) shift ; break ;;
            *) echo "Internal error!" ; exit 1 ;;
    esac
done
if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
    usage()
    exit
if

An alternative implementation using the getopts shell builtin(this only supports small options):

while getopts ":h:p:t:" option; do
    case "$option" in
         h) HOST=$OPTARG ;;
         p) PORT=$OPTARG ;;
         t) TABLE=$OPTARG ;;
        *) usage(); exit 1 ;;
    esac
done
if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
    usage()
    exit
if

shift $((OPTIND - 1))

Further reading for GNU getopt and getopts bash builtin

这篇关于对于bash命令行参数验证库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆