Search code examples
perlgetopt-long

GetOptions Optional 2nd value to an argument


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?


Solution

  • 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,
    );