2010-11-10 66 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 ] ... 

,其中「...」表示任何其他選項,或文件名,或者一點都沒有。如果其中一組是強制性的,我可以使用大括號'{}'代替方括號'[]'。方括號通常表示'可選'。

相關問題