Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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??

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;
        $spec => sub { push @newARGV, &$func },
        '<>'  => sub { push @newARGV, @_ },
    @ARGV = @newARGV;

  • Comment on Getopt::Long-based commandline argument replacement substitution expansion
  • Download Code
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://678586]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2017-07-26 04:06 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (383 votes). Check out past polls.