I take the purpose for this invocation, that matters for implementation here, to be the following.
There is a flag (call it $fresh) that need be set under the --import option, along with other flag(s) associated with --import. Additionally, there may be an independent option --fresh, which sets the $fresh flag.
While Getopt::Long doesn't support nested options this can be achieved using its other facilities. Set --import to take an optional argument with :, and set variables in a sub. If the word fresh is submitted as the value set the corresponding ($fresh) flag.
use warnings;
use strict;
use feature 'say';
use Getopt::Long;
my ($import, $fresh);
GetOptions(
'import:s' => sub {
$import = 1;
$fresh = 1 if $_[1] eq 'fresh';
},
'fresh!' => \$fresh # if independent --fresh option is needed
);
say 'import: ', $import // 'not submitted'; #/
say 'fresh: ', $fresh // 'not submitted';
The sub receives two arguments, the option name and value, and the value is used to check whether fresh was passed. The code as it stands does nothing for other words that may be passed, but it can be made to abort (with a usage message) if any value other than fresh is submitted.
Whatever particular reasons there are to require this invocation can be coded in the sub.
If a separate --fresh option is indeed provided then the user need be careful since it is possible to submit conflicting values for $fresh – one with --import and another in --fresh itself. This can be checked for in the code.
The option --import still works as a simple flag on its own.
Valid invocations are
gol.pl --import # $import is 1
gol.pl --import fresh # $import is 1, $fresh is 1
gol.pl --fresh # $fresh is 1
Since fresh is set in a sub as a value of --import it cannot be set with any other options.
This differs from the requirement by having fresh as a word, without dashes.
--freshfeeds the$freshvariable, and--importand--exportfeed the$importand$exportvariables, you can always justdie "--fresh only valid with --import or --export\n" if $fresh && ! $import;as the next line afterGetOptions(). - DavidO