perf/aggregate: use Getopt::Long for option parsing

When passing an option '--foo' that it does not recognize, the aggregate.perl script should die with an helpful error message like: Unknown option: foo ./aggregate.perl [options] [--] [<dir_or_rev>...] [--] \ [<test_script>...] > Options: --codespeed * Format output for Codespeed --reponame <str> * Send given reponame to codespeed --sort-by <str> * Sort output (only "regression" \ criteria is supported) rather than: fatal: Needed a single revision rev-parse --verify --foo: command returned error: 128 To implement that let's use Getopt::Long for option parsing instead of the current manual and sloppy parsing. This should save some code and make option parsing simpler, tighter and safer. This will avoid something like 'foo--sort-by=regression' to be handled as if '--sort-by=regression' had been used, for example. As Getopt::Long eats '--' at the end of options, this changes a bit the way '--' is handled as we can now have '--' both after the options and before the scripts. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Apr 25, 2018 at 18:10 UTC 38368cba2650bc51570356dfd660dfe192164082
1 file changed +26 -36
t/perf/aggregate.perl
+26 -36
@@ -4,6 +4,7 @@ use lib '../../perl/build/lib';
4 use strict;
5 use warnings;
6 use JSON;
7 +use Getopt::Long;
8 use Git;
9
10 sub get_times {
@@ -36,46 +37,34 @@ sub format_times {
37 return $out;
38 }
39
40 +sub usage {
41 + print <<EOT;
42 +./aggregate.perl [options] [--] [<dir_or_rev>...] [--] [<test_script>...] >
43 +
44 + Options:
45 + --codespeed * Format output for Codespeed
46 + --reponame <str> * Send given reponame to codespeed
47 + --sort-by <str> * Sort output (only "regression" criteria is supported)
48 + --subsection <str> * Use results from given subsection
49 +
50 +EOT
51 + exit(1);
52 +}
53 +
54 my (@dirs, %dirnames, %dirabbrevs, %prefixes, @tests,
55 $codespeed, $sortby, $subsection, $reponame);
56 +
57 +Getopt::Long::Configure qw/ require_order /;
58 +
59 +my $rc = GetOptions("codespeed" => \$codespeed,
60 + "reponame=s" => \$reponame,
61 + "sort-by=s" => \$sortby,
62 + "subsection=s" => \$subsection);
63 +usage() unless $rc;
64 +
65 while (scalar @ARGV) {
66 my $arg = $ARGV[0];
67 my $dir;
44 - if ($arg eq "--codespeed") {
45 - $codespeed = 1;
46 - shift @ARGV;
47 - next;
48 - }
49 - if ($arg =~ /--sort-by(?:=(.*))?/) {
50 - shift @ARGV;
51 - if (defined $1) {
52 - $sortby = $1;
53 - } else {
54 - $sortby = shift @ARGV;
55 - if (! defined $sortby) {
56 - die "'--sort-by' requires an argument";
57 - }
58 - }
59 - next;
60 - }
61 - if ($arg eq "--subsection") {
62 - shift @ARGV;
63 - $subsection = $ARGV[0];
64 - shift @ARGV;
65 - if (! $subsection) {
66 - die "empty subsection";
67 - }
68 - next;
69 - }
70 - if ($arg eq "--reponame") {
71 - shift @ARGV;
72 - $reponame = $ARGV[0];
73 - shift @ARGV;
74 - if (! $reponame) {
75 - die "empty reponame";
76 - }
77 - next;
78 - }
68 last if -f $arg or $arg eq "--";
69 if (! -d $arg) {
70 my $rev = Git::command_oneline(qw(rev-parse --verify), $arg);
@@ -225,7 +214,8 @@ sub print_sorted_results {
214 my ($sortby) = @_;
215
216 if ($sortby ne "regression") {
228 - die "only 'regression' is supported as '--sort-by' argument";
217 + print "Only 'regression' is supported as '--sort-by' argument\n";
218 + usage();
219 }
220
221 my @evolutions;