开发者

What is the correct syntax to specify optional parameters?

I have a Perl script that can be called as

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

or as

perl mysrc.pl -t=15

Basically, (either provide value for t) OR (provide values for all of a, 开发者_运维百科b and c). Atleast one of the groups of values has to be specified.

How do I say the above in syntax?

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

would mean that all the parameters are optional, which is not exactly the case. What is the correct way to write the syntax for mysrc.pl?


Two options: either use "|" as an "OR" symbol and non-square brackets for grouping to avoid the "optional" context, or list competing usages on multiple lines

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>

For REALLY complicated sets of options (think CVS), do what CVS does (no xterm at the moment, so below is a crude approximation from memory) - that is, the generic "help" message only lists all possible use cases, and to get help about option sets for each use case, you issue a per-use-case help command.

$ 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


I would probably use:

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

where the '...' represents any other options, or the file names, or nothing at all. And if one or the other set is mandatory, I might use braces '{}' instead of square brackets '[]'. The square brackets usually indicate 'optional'.


You mean just the help text? In that case you can do what Subversion does, for example:

$ 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 sourceWCPATH1@N sourceWCPATH2@M [WCPATH]
       3. merge [-c M[,N...] | -r N:M ...] SOURCE[@REV] [WCPATH]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜