You can do this with Getopt::Long::Descriptive . This is slightly different from Getopt::Long , but if you print a usage summary, it helps reduce duplication by doing all this for you.
Here I added a hidden source option, so $opt->source , which will contain the value dir or files depending on which option was provided, will force the one_of constraint on you. The specified values โโwill be in $opt->dir or $opt->files , depending on what was specified.
my ( $opt, $usage ) = describe_options( '%c %o', [ "source" => hidden => { 'one_of' => [ [ "dir=s" => "Directory" ], [ " files=s@ " => "FilesComma-separated list of files" ], ] } ], [ "man" => "..." ],
The main difference for the rest of your script is that all parameters are contained as methods of the $opt variable, and not each of them has its own variable, for example, using Getopt::Long .
source share