I was working on a program the other day that performed a series of operations, all of which could be run individually and independently. Usually, all would be run together in batch mode, but for testing, it would be useful to be able to run different operations, or stages, individually.

So the idea was that in the absence of command line switches, all the operations would be performed. Otherwise, no operations would be performed, unless the correponding switch was explicitly stated on the command line. I find the construct has a certain elegance.

Naturally, a corresponding solution could be created with Getopt::Long, but I have to admit I have grown fond of Getopt::Mixed because the processing of command line switches happens more or less in your own code. YMMV.

It turns out that I now need to add switches in order to govern how certain stages operate, so the concept no longer holds, but I want to leave this idea here, should I ever need to come back to it.

use Getopt::Mixed; { Getopt::Mixed::init(' a b c d e alpha>a bravo>b charlie>c delta>d echo>e '); my $restricted = 0; my %go; while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption( +) ) { ++$restricted; $go{$option}++; } Getopt::Mixed::cleanup(); sub go { my $stage = shift; return 1 unless $restricted; $go{$stage} ? 1 : 0; } } go('a') and stage_a(); go('b') and stage_b(); ... sub stage_a { ... }

Oops, looking again at this in a new light, I realise that there is vestigial code that no longer serves any purpose. Specifically, the $restricted variable is redundant. An updated version could be written as

use Getopt::Mixed; { Getopt::Mixed::init(' a b c d e alpha>a bravo>b charlie>c delta>d echo>e '); my %go; while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption( +) ) { $go{$option}++; } Getopt::Mixed::cleanup(); sub go { 0 == scalar keys %go or defined( $go{$_[0]} ) } } go('a') and stage_a(); go('b') and stage_b(); ... sub stage_a { ... }

In reply to Another take on command line switches by grinder

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.