便携式getopt_long等效用于shell脚本
发布时间:2020-12-15 20:56:46 所属栏目:安全 来源:网络整理
导读:我想在 shell脚本中解析长选项. POSIX仅提供getopts来解析单个字母选项.有没有人知道在shell中实现长选项解析的可移植(POSIX)方法?我已经看过autoconf在生成配置脚本时的作用,但结果远非优雅.我只能接受长选项的完整拼写.仍应允许单字母选项,可能在组中. 我
我想在
shell脚本中解析长选项. POSIX仅提供getopts来解析单个字母选项.有没有人知道在shell中实现长选项解析的可移植(POSIX)方法?我已经看过autoconf在生成配置脚本时的作用,但结果远非优雅.我只能接受长选项的完整拼写.仍应允许单字母选项,可能在组中.
我正在考虑一个shell函数,它采用空格分隔的形式选项[= flags]的args列表,其中标志表示该选项采用arg或可以多次指定.与C对应物不同,不需要区分字符串,整数和浮点数. 解决方法
针对便携式shell getopt_long命令的设计说明
我有一个程序getoptx,它使用单字母选项(因此它不是你的问题的答案),但它正确处理带空格的参数,原始的getopt命令(而不是shell内置的getopts)不.源代码中的规范说: /* ** Usage: eval set -- $(getoptx abc: "$@") ** eval set -- $(getoptx abc: -a -c 'a b c' -b abc 'd e f') ** The positional parameters are: ** $1 = "-a" ** $2 = "-c" ** $3 = "a b c" ** $4 = "-b" ** $5 = "--" ** $6 = "abc" ** $7 = "d e f" ** ** The advantage of this over the standard getopt program is that it handles ** spaces and other metacharacters (including single quotes) in the option ** values and other arguments. The standard code does not! The downside is ** the non-standard invocation syntax compared with: ** ** set -- $(getopt abc: "$@") */ 我推荐你的getopt_long的eval set – $(getopt_long“$optspec”“$@”)表示法. getopt_long的一个主要问题是参数规范的复杂性 – 示例中的$optspec. 您可能需要查看Solaris CLIP(命令行界面范例)中使用的符号表示法;它使用单个字符串(如原始的getopt()函数)来描述选项. (谷歌:’solaris剪辑命令行界面范例’;只使用’solaris剪辑’可以获得视频剪辑.) 这个材料是从Sun的getopt_clip()派生的部分示例: /* Example 2: Check Options and Arguments. The following example parses a set of command line options and prints messages to standard output for each option and argument that it encounters. This example can be expanded to be CLIP-compliant by substituting the long string for the optstring argument: While not encouraged by the CLIP specification,multiple long-option aliases can also be assigned as shown in the following example: :a(ascii)b(binary):(in-file)(input)o:(outfile)(output)V(version)?(help) */ static const char *arg0 = 0; static void print_help(void) { printf("Usage: %s [-a][-b][-V][-?][-f file][-o file][path ...]n",arg0); printf("Usage: %s [-ascii][-binary][-version][-in-file file][-out-file file][path ...]n",arg0); exit(0); } static const char optstr[] = ":a(ascii)b(binary)f:(in-file)o:(out-file)V(version)?(help)"; int main(int argc,char **argv) { int c; char *filename; arg0 = argv[0]; while ((c = getopt_clip(argc,argv,optstr)) != -1) { ... } ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读