Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found