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=

In reply to Re: Set getopt defaults based on $0 by Anonyrnous Monk
in thread Set getopt defaults based on $0 by samwyse

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.