0

I would like an option where the first value is mandatory, and the 2nd value is optional.

For example,

./foo --arg mandatory optional

If I use =s{2} the user is forced to enter the second option.

I don't want to allow n-number of values...I want to mandate only allowing two values, with the second value being optional.

Is this a feature supported by GetOptions?

toolic
  • 57,801
  • 17
  • 75
  • 117
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115

2 Answers2

5

Assuming you're using the Getopt::Long module, using =s{1,2} should do it:

use Getopt::Long;

my @arg;
GetOptions(
    "arg=s{1,2}" => \@arg,
);
TobyLL
  • 2,098
  • 2
  • 17
  • 23
1

An alternative to Getopt::long is to use Getopt::Std

use strict;

use Getopt::Std;

my %opt;
getopts('a:b:c:;d', \%opt);

So -d perhaps might be used for switching debugging on in the code.

The colon : after the option means you need to add a value to the switch

Anything after the semi-colon ; is optional.

Usage: perl myscript.pl -a <value> -b <value> -c <value> [ -d ]

thonnor
  • 1,206
  • 2
  • 14
  • 28