3
我正在寻找一个可重用的代码片断,它为bash执行命令行参数验证。Bash的命令行参数验证库
理想一些类似于由Apache的百科全书CLI提供的功能:
共享CLI支持不同类型的选择:
- POSIX类似的选项 (即焦油-zxvf的foo.tar.gz。)
- GNU等长选项(即杜--human可读--max深度= 1)
- 短选项附值(即GCC -O2 foo.c的)
- 长选项与单个连字符(即。蚂蚁-projecthelp)
- ...
,并自动生成程序中的“用法”的消息,像这样:
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
我将包括之初此代码段我Bash脚本并跨脚本重用它。
一定有这样的事情。我不相信我们都编写代码来这种效果或类似:
#!/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
...
使用'getopts'作为简短选项。参见[BashFAQ/35](http://mywiki.wooledge.org/BashFAQ/035)。用Python编写脚本并使用[argparse](http://docs.python.org/library/argparse.html)。 –
我怀疑答案是缺乏模块基础结构使得以可移植的方式加载源库与每次重写选项解析大致相同的工作量。 – tripleee