Another variation with Getopt::Long and hash tables to declare the defaults:
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my ($verb, $obj) = $0 =~ m|([^/]+)-([^/]+)$|;
my %defaults_verbs = (
'create' => {
foo => "foo",
bar => "",
},
'update' => {
foo => "",
bar => "bar",
},
#...
);
my %defaults_objs = (
'tap' => {
baz => 42,
},
'fap' => {
baz => 99,
},
#...
);
my %defaults_combined = (
'create-tap' => {
foo => "boo",
baz => 13,
},
#...
);
# merge individual defaults hashrefs (order matters), e.g.:
my $opts = {
%{ $defaults_verbs{$verb} //{} },
%{ $defaults_objs{$obj} //{} },
%{ $defaults_combined{"$verb-$obj"} //{} },
};
GetOptions($opts, 'foo=s', 'bar=s', 'baz=i');
for my $opt (sort keys %$opts) {
print "$opt=$opts->{$opt}\n";
}
$ for name in create-tap update-tap ; do ln -s 882180.pl $name ; done
$ ./create-tap
bar=
baz=13
foo=boo
$ ./create-tap --foo=FOO
bar=
baz=13
foo=FOO
$ ./update-tap
bar=bar
baz=42
foo=
$ ./update-tap --baz=55
bar=bar
baz=55
foo=
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.