Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Getopt::Long-based commandline argument replacement substitution expansion

by jdporter (Canon)
on Apr 06, 2008 at 02:25 UTC ( #678586=snippet: print w/ replies, xml ) Need Help??

Description:

Modifies @ARGV by replacing a certain option (with or without argument) with something else. Uses Getopt::Long.

In my application, which takes a list of filespecs on the commandline, I replace -tar foo.tar with /tmp/tar/* after having extracted foo.tar into /tmp/tar/:

option_replacement( "tar=s", sub { system "tar xf $_[1] -C $tmpdir"; " +$tmpdir/*" } );
The first arg is the sort of thing you pass as an option spec to Getopt::Long's GetOption().
The second arg is a sub ref, the interface of which is exactly the same as a sub you'd pass as an option handler to GetOption(), except that it returns the list of strings to insert into @ARGV.

use Getopt::Long;
sub option_replacement
{
    my( $spec, $func ) = @_;
    local $Getopt::Long::passthrough=1;
    my @newARGV;
    GetOptions(
        $spec => sub { push @newARGV, &$func },
        '<>'  => sub { push @newARGV, @_ },
    );
    @ARGV = @newARGV;
}

Comment on Getopt::Long-based commandline argument replacement substitution expansion
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://678586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-11-22 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (118 votes), past polls