Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax to specify optional parameters?

Tags:

syntax

perl

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?

like image 509
Lazer Avatar asked Feb 25 '23 21:02

Lazer


2 Answers

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
like image 68
DVK Avatar answered Mar 05 '23 16:03

DVK


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'.

like image 42
Jonathan Leffler Avatar answered Mar 05 '23 16:03

Jonathan Leffler