Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Another take on command line switches

by grinder (Bishop)
on Feb 08, 2002 at 11:42 UTC ( #144086=snippet: print w/ replies, xml ) Need Help??

Description: 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 {
    ...
}
Comment on Another take on command line switches
Select or Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://144086]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-09-22 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls