Why stunt the capabilities of getopt by shoehorning in other kinds of information in its switch strings?

In such cases, I usually just make a more complex structure which associates getopt switches to arrays which contain variables, help texts, validator functions and whatever else. Then I just map out the elements that Getopt::Long expects. For --help, I can also look at the switch strings and other clues to automate some of the messages for consistency.

This code is a sketch from memory. There are other structures that would do just as well.

my $Verbose; my $Secret; %Options = { 'verbose+' => [ \$Verbose, "Makes the output more verbose." ], 'secret!' => [ \$Secret, "Enables secret features." ], }; ... sub usage { ... foreach my $option (sort keys %Options) { my $switch = $option; $switch =~ s/=.*$//; $switch =~ s/\W*$//; $message = $Options{$option}[1]; $message .= " Adding $switch increases the effect." if $option =~ /\+$/; $message .= " Use --no$switch to disable." if $option =~ /\!$/; print Wrap(undef, undef, "$switch: $message"), $/; } ... GetOptions(map { $_, $Options{$_}[0] } keys %Options);

(One should mention the POD::Usage here, but I find it a little stiff, personally.)

--
[ e d @ h a l l e y . c c ]


In reply to Re: "self documenting" switch options by halley
in thread "self documenting" switch options by bobn

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.