the best way to make an option dependent on another?

What is best depends on the circumstances, but here is another way that might be better if you need sequential processing of your arguments:

use strict; use warnings; use Getopt::Long; my %args; my $result = GetOptions ( "c=s" => \&COMP1, "t=s" => sub { if($args{c}) { COMP2(@_); # COMP2 will not be called if option c is n +ull? } else { die 'Warning: arg -t without preceding -c: arg -t igno +red'; } }, '<>' => sub {print "\nThat is not a valid parameter\n";}, ); die "Error: argument processing did not complete successfully" unless( +$result); exit(0); sub COMP1 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; } sub COMP2 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; }

Note carefully what happens with out of order (for some definitions of order) arguments:

$ ./test.pl -t asdf -c qwer Warning: arg -t without preceding -c: arg -t ignored at ./test.pl line + 13. Option -c is value=qwer Error: argument processing did not complete successfully at ./test.pl +line 18.

This may not be the result you want.

You can have Getopt::Long stop processing when an error is encountered in the handler by calling die with '!FINISH'.

use strict; use warnings; use Getopt::Long; my %args; my $result = GetOptions ( "c=s" => \&COMP1, "t=s" => sub { if($args{c}) { COMP2(@_); # COMP2 will not be called if option c is n +ull? } else { warn 'Error: arg -t without preceding -c: processing t +erminated'; die '!FINISH'; } }, '<>' => sub {print "\nThat is not a valid parameter\n";}, ); die "Error: argument processing did not complete successfully" unless( +$result); exit(0); sub COMP1 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; } sub COMP2 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; }

Given the same "out of order" arguments, this version does not proceed to process the -c argument:

$ ./test.pl -t asdf -c qwer Error: arg -t without preceding -c: processing terminated at ./test.pl + line 13.

But notice that Getopt::Long doesn't return an error indication in this case. It returns with the argument list partially processed. Of course, you could test for this and proceed or not as might be appropriate.

You should also consider whether any processing should be done if there is an error in the arguments. It might be best to analyze all the arguments and terminate if there is an error or process them if all is well. Consider the following case:

use strict; use warnings; use Getopt::Long; my %args; my $result = GetOptions ( "c=s" => \&COMP1, "t=s" => sub { if($args{c}) { COMP2(@_); # COMP2 will not be called if option c is n +ull? } else { warn 'Error: arg -t without preceding -c: processing t +erminated'; die '!FINISH'; } }, '<>' => sub {print "Frobnicating $_[0]\n";}, ); die "Error: argument processing did not complete successfully" unless( +$result); exit(0); sub COMP1 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; } sub COMP2 { print "Option -$_[0] is value=$_[1]\n"; $args{$_[0]} = $_[1]; }

Which might be called with the following arguments:

$ ./test.pl foo -t bar -c baz Frobnicating foo Error: arg -t without preceding -c: processing terminated at ./test.pl + line 13.

In this case, you might not want your foo frobnicated, or it might be exactly what you want.

In summary, this is a very long winded (and, one hopes, in passing, at least somewhat informative) way of saying that what is best depends on the circumstances but with some imagination almost anything can be done.


In reply to Re^3: Processing arguments with Getopt::Long by ig
in thread Processing arguments with Getopt::Long by MKJ747

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.