用户友好的命令行选项

时间:2012-08-15 22:51:53

标签: bash shell unix command-line command-line-arguments

我从c ++知道boost::program_options,这使我能够在几乎所有时间内编写用户友好的命令行选项。 “用户友好”是指,短期和长期选项以及与此类似的描述性帮助命令:

Copy standard input to each FILE, and also to standard output.

  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
      --help     display this help and exit
      --version  output version information and exit

对于我自己,我必须发现这在bash中非常尴尬,内置的getopts仅支持短选项。这是正确的还是我错了?

您如何实现用户友好的命令行选项?如果您知道任何链接,最佳实践或深入教程,我将非常感激。

2 个答案:

答案 0 :(得分:1)

GNU getopt支持长选项,可以在任何类似Bourne或类似csh的shell中使用。 ksh93的内置getopts也支持长选项。 zsh有一个zparseopts。

有一个getopts的POSIX shell实现(作为shell函数)支持http://stchaz.free.fr/getopts_long.sh

的长选项

答案 1 :(得分:0)

感谢您指出我正确的信息来源。

我决定这样做https://github.com/Mythli/tech/blob/master/bash/getopt.sh 代码非常简单,因此不需要解释。