Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi - am continuing the previous thread here as it became long ,hope someone can provide an alternative idea or a solution?

Is there a way to add options to option values?meaning ,in the below example the option is "input" and it's values are data,modem,apps but for these values I want to add options (-nd -na -nc)?Can someone pls advise how can I achieve that?

To explain again ,the ones in "[]" are optional sub-options(they may or may not present always) to the values of "input"option, data,modem,apps are values to option "input",they can also change.

EXAMPLE:- findfiles -input [-nd -na ] data [-nc -nd] modem apps -des "finding fi +les" -r 1000 Basically ,pseudco code of what I want to achieve is below.Really appr +eciate if someone can give me the perlversion of the code. for each "input option value" { call function A if not exits nd call function B if not exits na call function C if not exists nc }

Replies are listed 'Best First'.
Re: (Continuation ....)Dynamic option
by Anonymous Monk on Mar 16, 2011 at 07:33 UTC
    Hi - am continuing the previous thread here as it became long ,hope someone can provide an alternative idea or a solution?

    This probably hasn't been suggested yet, but maybe you can try to implement one of the ideas?

Re: (Continuation ....)Dynamic option
by cdarke (Prior) on Mar 16, 2011 at 12:11 UTC
    Adding options to data values is non-standard, in the extreme. You also appear to have multi-character options with only a single hyphen, -input instead of --input for example, which breaks the standard. Therfore I don't think any of the normal modules can help.

    The only way I can see of doing this is to parse the argument list yourself. For example:
    use warnings; use strict; # findfiles -input [-nd -na ] data [-nc -nd] modem apps -des "finding +files" -r 1000 # "finding files" altered to finding_files for test purposes our @ARGV = qw(-input -nd -na data -nc -nd modem apps -des finding_fil +es -r 1000); use Data::Dumper; use GetOpt::Std; my %args = ('-input' => [{}], '-des' => [{}], '-r' => [{}]); my $active_arg = 0; for my $arg (@ARGV) { if (substr($arg,0,1) eq '-') { if (exists $args{$arg}) { $active_arg = $arg } elsif ($active_arg) { $args{$active_arg}[0]{$arg} = undef } else { die "Invalid argument(1): $arg" } } else { if ($active_arg) { push @{$args{$active_arg}},$arg } else { die "Invalid argument(2): $arg" } } } print Dumper(\%args),"\n";
    Gives:
    $VAR1 = { '-r' => [ {}, '1000' ], '-des' => [ {}, 'finding_files' ], '-input' => [ { '-nc' => undef, '-nd' => undef, '-na' => undef }, 'data', 'modem', 'apps' ] };
    You can then check to see if options have been set by using exists on the relevant hash.
    Update: The problem with this though is that I am not setting the "sub-options" for the data items, for example -nd is duplicated. There is an inconsistency in your model so far as I can see. How do we know if 'data' should have options or not? Still, you might be able to develop my suggestion further, sicne you know what is valid and what is not.
      You also appear to have multi-character options with only a single hyphen, -input instead of --input for example, which breaks the standard.

      Getopt::Long supports single hyphen just fine for long options ...

      # x.pl # perl 5.8.8, Getopt::Long 2.35 use Data::Dumper; use Getopt::Long; my ( $ink , $int ); GetOptions( 'ink' => \$ink , 'int' => \$int ); print Dumper( $ink , $int );
      # perl x.pl -ink $VAR1 = 1; $VAR2 = undef; # perl x.pl -ink -int $VAR1 = 1; $VAR2 = 1;
Re: (Continuation ....)Dynamic option
by Anonymous Monk on Mar 16, 2011 at 17:05 UTC
    Applying the KISS principle, here is what I would do
    myprogram.pl alltheoptionsineed.json myprogram.pl alltheoptionsineed.yml myprogram.pl alltheoptionsineed.ini myprogram.pl alltheoptionsineed.xml myprogram.pl alltheoptionsineed.pl
    In myprogram.pl you get the options by using Config::Any on the filename in @ARGV, along with defaults gotten by File::UserConfig -- least work possible :)