1

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`;
toolic
  • 57,801
  • 17
  • 75
  • 117
Jeffrey Ray
  • 1,244
  • 2
  • 9
  • 20
  • 2
    Could you provide a short code example of what you want to achieve? – hlovdal Mar 31 '14 at 12:31
  • 1
    You will have to be a bit more specific about what you mean by "format" and "modify" if you want us to know what you mean. – TLP Mar 31 '14 at 12:40
  • Parsing arguments into strings and feeding them to another Perl script is rather cumbersome and fragile. The better option is to modularize the scripts and import the code into your main script. – TLP Mar 31 '14 at 19:08

2 Answers2

1

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( ... )
toolic
  • 57,801
  • 17
  • 75
  • 117
0

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);
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Not really. Getopt::Long offers a lot of options, just iterating through the %ARG keys and values and saying '-key=val' is not going to work for all but the most basic configurations. I added an example above. Please correct me if I am wrong or can you provide me with code that you would use, if not just a `foreach $key in (keys %ARG)` – Jeffrey Ray Mar 31 '14 at 15:29