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; }