Question

I am using Getopt::Long to parse options passed to my program. I would like to format these options (after modifying them) to pass to another program.

Does Getopt do this, or is there possibly another module that can do this for me?

Example:

use Getopt::Long qw(:config no_ignore_case );

# set defaults
my %ARG;
$ARG{config} = './default-config.ini';
$ARG{debug} = 0;

# process command line options
GetOptions( \%ARG, 'config|c=s', 'debug!');

# modify them as necessary
if ( if $ARG{debug} ) {
   $ARG{config} = './debug-config.ini' ;
   $ARG{debug} = 0;
   $ARG{verbal} = 1;
}

# format options string to pass to other command

# expecting something like this in options string:

# '-config=./debug-config.ini --verbal'

$options_string = some_method_or_module_to_format( %ARG, 'config=s', 'verbal' );

`some-other-script-1.pl $options_string`;
`some-other-script-2.pl $options_string`;
`some-other-script-3.pl $options_string`;
Était-ce utile?

La solution

No, Getopt::Long simply "parses the command line from @ARGV, recognizing and removing specified options". It does not do any formatting of the options.

If you want to retain all the options as passed to your program, you can make a copy of the original array before calling GetOptions:

my @opts = @ARGV;
GetOptions( ... )

Autres conseils

I would like to format these options (after modifying them) to pass to another program. Does Getopt do this or is there possibly another module that can do this for me?

It would take just as long to instruct a module how to do it than it would to instruct Perl how to do it. There's no need for such a module.

my @args;
push @args, "--config", $ARG{config} if defined($ARG{config});
push @args, "--verbal"               if $ARG{verbal};

my $args = shell_quote(@args);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top