2010-11-10 30 views
4

我有一个Perl脚本,可以称为指定可选参数的正确语法是什么?

perl mysrc.pl -a=3 -b=4 -c=6 

或作为

perl mysrc.pl -t=15 

基本上,(或者提供t值)OR(对于所有的abc提供值)。至少需要指定一组值。

我如何说上述语法?

perl mysrc.pl 
    [-a=<value of a>] 
    [-b=<value of b>] 
    [-c=<value of c>] 
    [-t=<value of t>] 

意味着所有的参数都是可选的,但并非如此。什么是编写mysrc.pl的语法的正确方法?

回答

4

两个选项:要么使用“|”为分组,以避免“可选”的情况下,或列表竞争惯例在多行

perl mysrc.pl {-a=<value of a> -b=<value of b> -c=<value of c>|-t=<value of t>} 

perl mysrc.pl UseCaseOneOptions|UseCaseTwoOptions 
    UseCaseOneOptions: -a=<value of a> -b=<value of b> -c=<value of c> 
    UseCaseTwoOptions: -t=<value of t> 

对于非常复杂的选项集(认为CVS)的“OR”符号和非方括号,做CVS做什么(目前没有xterm,所以下面是内存的粗略近似值) - 也就是说,通用的“帮助”消息只列出了所有可能的用例,并且为每个用例的选项集提供帮助,用例帮助命令。

$ cvs --help 
    Usage: cvs <command> <per-command-options> 
    Please type "cvs command --help" to get help on specific command's options 
    Commands are: 
     cvs add 
     cvs commmit 
     cvs remove 
     ... 

$ cvs checkout --help 
    Usage: cvs checkout [-p] [-A] [-m message] [-M message_file] file_path 
     -m message:   check-in comment 
     -M file:    read check-in comment from this file 
     -p:     non-sticky checkout. Print the file to STDOUT. 

$ cvs diff --help 
    Usage: cvs diff [-r VER1] [-r VER2] [-w] file_path 
     -w:     Ignore whitespace 
1

你的意思只是帮助文本?在这种情况下,你可以做颠覆做什么,例如:

$ svn help merge 
merge: Apply the differences between two sources to a working copy path. 
usage: 1. merge sourceURL1[@N] sourceURL2[@M] [WCPATH] 
     2. merge [email protected] [email protected] [WCPATH] 
     3. merge [-c M[,N...] | -r N:M ...] SOURCE[@REV] [WCPATH] 
+0

是的,帮助文本。 – Lazer 2010-11-10 08:22:38

3

我可能会使用:

mycmd [ -t=tval | -a=aval -b=bval -c=cval ] ... 

,其中“...”表示任何其他选项,或文件名,或者一点都没有。如果其中一组是强制性的,我可以使用大括号'{}'代替方括号'[]'。方括号通常表示'可选'。

相关问题