Unittest for multiple -a. Code refactor.

This commit is contained in:
Ole Tange 2010-07-14 21:54:33 +02:00
parent 3adfc665f1
commit 248fa1c940
4 changed files with 53 additions and 63 deletions

View file

@ -1,6 +1,3 @@
multiple -a == ::::
# Hvordan udregnes system limits på remote systems hvis jeg ikke ved, hvormange # Hvordan udregnes system limits på remote systems hvis jeg ikke ved, hvormange
# argumenter, der er? Lav system limits lokalt og lad det være max # argumenter, der er? Lav system limits lokalt og lad det være max

View file

@ -82,7 +82,7 @@ B<{.}> can be changed with B<-U>.
=item B<{>I<n>B<}> =item B<{>I<n>B<}>
Argument from argument file I<n> or the I<n>'th argument. See B<::::> Argument from argument file I<n> or the I<n>'th argument. See B<-a>
and B<-N>. and B<-N>.
B<{>I<n>B<}> can be used the same places as B<{}>. B<{>I<n>B<}> can be used the same places as B<{}>.
@ -113,11 +113,9 @@ If B<--arg-file> is set arguments from that file will be appended.
=item B<::::> I<argfiles> (beta test) =item B<::::> I<argfiles> (beta test)
Use arguments from the command line as arguments files for input. One Alternative version of B<-a> I<argfile1> B<-a> I<argfile2> ...
line will be read from each of the files. The arguments can be
accessed in the command as B<{1}> .. B<{>I<n>B<}>, so B<{1}> will be a See B<-a>.
line from the first file, and B<{6}> will refer to the line with the
same line number from the 6th file.
=item B<--null> =item B<--null>
@ -137,6 +135,12 @@ Read items from the file I<input-file> instead of stdin (standard input). If
you use this option, stdin is given to the first process run. you use this option, stdin is given to the first process run.
Otherwise, stdin is redirected from /dev/null. Otherwise, stdin is redirected from /dev/null.
If multiple B<-a> are given, one line will be read from each of the
files. The arguments can be accessed in the command as B<{1}>
.. B<{>I<n>B<}>, so B<{1}> will be a line from the first file, and
B<{6}> will refer to the line with the same line number from the 6th
file.
=item B<--arg-sep> I<sep-str> (beta testing) =item B<--arg-sep> I<sep-str> (beta testing)
@ -1947,6 +1951,7 @@ sub parse_options {
} }
$Global::total_jobs += @ARGV; $Global::total_jobs += @ARGV;
@ARGV=(); @ARGV=();
last;
} else { } else {
push @new_argv, $arg; push @new_argv, $arg;
} }
@ -1956,10 +1961,7 @@ sub parse_options {
} }
if(grep /^::::$/o, @ARGV) { if(grep /^::::$/o, @ARGV) {
# max-replace-args # convert :::: to multiple -a
if(not @::opt_a) { push @::opt_a, "/dev/null"; }
# read all the files and merge them
my @new_argv = (); my @new_argv = ();
my @argument_files; my @argument_files;
while(@ARGV) { while(@ARGV) {
@ -1973,36 +1975,7 @@ sub parse_options {
} }
# Output: @ARGV = command option # Output: @ARGV = command option
@ARGV=@new_argv; @ARGV=@new_argv;
$Global::input_is_filename ||= (@ARGV); push @::opt_a, @argument_files;
$::opt_N = $#argument_files+1;
$Global::max_number_of_args = $#argument_files+1;
# read the files
my @content;
my $max_lineno = 0;
my $in_fh = gensym;
for (my $fileno = 0; $fileno <= $#argument_files; $fileno++) {
if(not open ($in_fh, $argument_files[$fileno])) {
print STDERR ("$Global::progname: Cannot open $argument_files[$fileno]\n");
exit (255);
}
for (my $lineno=0; $content[$fileno][$lineno] = get_next_arg_from_fh($in_fh); $lineno++) {
$max_lineno = max($max_lineno,$lineno);
}
close $in_fh;
}
for (my $lineno=0; $lineno <= $max_lineno; $lineno++) {
for (my $fileno = 0; $fileno <= $#argument_files; $fileno++) {
my $arg = $content[$fileno][$lineno];
if(defined $arg) {
unget_arg($arg);
} else {
unget_arg("");
}
}
}
$Global::total_jobs += $max_lineno;
} }
# must be done after ::: and :::: because they mess with @ARGV # must be done after ::: and :::: because they mess with @ARGV
@ -2012,21 +1985,10 @@ sub parse_options {
# must be done after opt_arg_sep # must be done after opt_arg_sep
if($#::opt_a == 0) { if($#::opt_a == 0) {
# One -a => xargs compatibility # One -a => xargs compatibility
if(not open(ARGFILE,"<",$::opt_a[0])) { $Global::argfile = open_or_exit($::opt_a[0]);
print STDERR "$Global::progname: ".
"Cannot open input file `$::opt_a[0]': ".
"No such file or directory\n";
exit(255);
}
$Global::argfile = *ARGFILE;
} else { } else {
if(not open(ARGFILE,"<","/dev/null")) { # Multiple -a => xapply style
print STDERR "$Global::progname: ". $Global::argfile = open_or_exit("/dev/null");
"Cannot open input file `/dev/null': ".
"No such file or directory\n";
exit(255);
}
$Global::argfile = *ARGFILE;
$::opt_N = $#::opt_a+1; $::opt_N = $#::opt_a+1;
$Global::max_number_of_args = $#::opt_a+1; $Global::max_number_of_args = $#::opt_a+1;
# read the files # read the files
@ -2034,10 +1996,7 @@ sub parse_options {
my $max_lineno = 0; my $max_lineno = 0;
my $in_fh = gensym; my $in_fh = gensym;
for (my $fileno = 0; $fileno <= $#::opt_a; $fileno++) { for (my $fileno = 0; $fileno <= $#::opt_a; $fileno++) {
if(not open ($in_fh, $::opt_a[$fileno])) { $in_fh = open_or_exit($::opt_a[$fileno]);
print STDERR ("$Global::progname: Cannot open $::opt_a[$fileno]\n");
exit (255);
}
for (my $lineno=0; for (my $lineno=0;
$content[$fileno][$lineno] = get_next_arg_from_fh($in_fh); $content[$fileno][$lineno] = get_next_arg_from_fh($in_fh);
$lineno++) { $lineno++) {
@ -2068,7 +2027,6 @@ sub parse_options {
$Global::Xargs = 1; $Global::Xargs = 1;
} }
if(defined $::opt_eta) { if(defined $::opt_eta) {
# must be done after opt_a # must be done after opt_a
$::opt_progress = $::opt_eta; $::opt_progress = $::opt_eta;
@ -2109,6 +2067,18 @@ sub parse_options {
} }
} }
sub open_or_exit {
my $file = shift;
my $fh = gensym;
if(not open($fh,"<",$file)) {
print STDERR "$Global::progname: ".
"Cannot open input file `$file': ".
"No such file or directory\n";
exit(255);
}
return $fh;
}
sub cleanup { sub cleanup {
# Returns: N/A # Returns: N/A
if(@::opt_basefile) { if(@::opt_basefile) {

View file

@ -32,3 +32,13 @@ echo '### Quoting if there is no command and 2 arg files'
parallel -kv :::: <(echo 'echo a') <(echo 'echo b') parallel -kv :::: <(echo 'echo a') <(echo 'echo b')
echo '### Quoting if there is no command and 2 arg files of uneven length' echo '### Quoting if there is no command and 2 arg files of uneven length'
parallel -kv :::: <(echo 'echo a';echo echo a1) <(echo 'echo b') parallel -kv :::: <(echo 'echo a';echo echo a1) <(echo 'echo b')
echo '### Test multiple -a'
parallel -kv -a <(echo a) -a <(echo b) echo {2} {1}
parallel -kv echo {2} {1} :::: <(echo a) <(echo b)
echo '### Multiple -a: An unused file'
parallel -kv -a <(echo a) -a <(echo b) -a <(echo c) echo {2} {1}
parallel -kv echo {2} {1} :::: <(echo a) <(echo b) <(echo c)
echo '### Multiple -a: nonexistent'
stdout parallel -kv echo {2} {1} :::: nonexist nonexist2
stdout parallel -kv -a nonexist -a nonexist2 echo {2} {1}

View file

@ -10,7 +10,7 @@
3 4 5 3 4 5
6 7 6 7
### Test :::: on nonexistent ### Test :::: on nonexistent
parallel: Cannot open nonexistent parallel: Cannot open input file `nonexistent': No such file or directory
### Test :::: two files ### Test :::: two files
1 5 1 5
2 6 2 6
@ -81,3 +81,16 @@ echo a echo b
a echo b a echo b
echo a1 echo a1
a1 a1
### Test multiple -a
echo b a
b a
echo b a
b a
### Multiple -a: An unused file
echo b a
b a
echo b a
b a
### Multiple -a: nonexistent
parallel: Cannot open input file `nonexist': No such file or directory
parallel: Cannot open input file `nonexist': No such file or directory