7 args2env - Turns command arguments into environment variables and executes command with the remained arguments
11 args2env [I<OPTIONS>] I<COMMAND> I<ARG_1> I<ARG_2> ... I<ARG_R2> I<ARG_R1>
19 =item -a, --arg I<NUM>
21 Move the I<NUM>th argument to the environment by the name B<< ARG_I<NUM> >>
22 (may be overridden by B<--template> option).
23 Counting starts from 1.
24 I<NUM> may be negative number, in which case it's counted from the end backwards.
26 =item -r, --right-arg I<NUM>
28 Same as B<< --arg -I<NUM> >>.
32 Move all arguments to environment.
34 =item -k, --keep I<NUM>
36 Keep the first I<NUM> arguments as arguments, and move the rest of them to environment.
38 =item -t, --template I<TEMPLATE>
40 How to name environment variables?
41 Must contain a B<%d> macro.
43 So the value of argument given by B<--arg 1> goes to B<ARG_1> variable.
45 =item -nt, --negative-template I<TEMPLATE>
47 How to name environment variables for arguments specified by negative number?
48 Must contain a B<%d> macro.
49 Default is B<ARG_R%d>, B<R> is for "right", because this arg is counted from the right.
50 So the value of argument given by B<--arg -1> goes to B<ARG_R1> variable.
61 use Getopt
::Long qw
/:config no_ignore_case no_bundling no_getopt_compat no_auto_abbrev require_order/;
63 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
66 $envname_template = 'ARG_%d';
67 $negative_envname_template = 'ARG_R%d';
70 'a|arg=i@' => \
@args_to_move,
71 'A|all' => sub { @args_to_move = (1 .. $#ARGV); },
72 'k|keep=i' => sub { @args_to_move = (($_[1]+1) .. $#ARGV); },
73 'r|right-arg=i@' => sub {
74 my ($getopt, $param) = @_;
75 push @args_to_move, -$param;
77 't|template=s' => \
$envname_template,
78 'nt|negative-template=s' => \
$negative_envname_template,
79 'help' => sub { pod2usage
(-exitval
=>0, -verbose
=>99); },
80 ) or pod2usage
(-exitval
=>2, -verbose
=>99);
82 if(grep {$_ == 0} @args_to_move)
84 warn "The 0th argument is the command itself, better not to remove.\n";
85 pod2usage
(-exitval
=>2, -verbose
=>99);
88 for my $arg_num (@args_to_move)
91 if($arg_num > 0) { $envname = sprintf $envname_template, abs $arg_num; }
92 else { $envname = sprintf $negative_envname_template, abs $arg_num; }
93 $ENV{$envname} = $ARGV[$arg_num];
94 $ARGV[$arg_num] = undef;
96 @ARGV = grep {defined} @ARGV;
98 exec {$ARGV[0]} @ARGV;
99 ($errno, $errstr) = (int $!, $!);
100 warn "$0: ${ARGV[0]}: $errstr\n";