perl – 指定可选参数的正确语法是什么?

前端之家收集整理的这篇文章主要介绍了perl – 指定可选参数的正确语法是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个可以被称为的Perl脚本

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

或者作为

perl mysrc.pl -t=15

基本上,(或者为t提供值)OR(为a,b和c提供所有值).至少必须指定其中一组值.

我怎么说语法上面的内容

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

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

解决方法

两个选项:使用“|”作为“OR”符号和非方括号用于分组以避免“可选”上下文,或列出多行竞争用法

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),做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

猜你在找的Perl相关文章