difftool: parse options using Getopt::Long

Replace custom option/argument parser with standard Getopt::Long module. This shortens the code and makes it easier to understand. Signed-off-by: Tim Henigan <tim.henigan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tim Henigan committed Mar 22, 2012 at 15:52 UTC 3f94ff755e266520ba2f6007d557395c33d30fdc
1 file changed +46 -65
git-difftool.perl
+46 -65
@@ -15,17 +15,15 @@ use strict;
15 use warnings;
16 use Cwd qw(abs_path);
17 use File::Basename qw(dirname);
18 -
19 -require Git;
20 -
21 -my $DIR = abs_path(dirname($0));
22 -
18 +use Getopt::Long qw(:config pass_through);
19 +use Git;
20
21 sub usage
22 {
23 print << 'USAGE';
27 -usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
28 - [-y|--no-prompt] [-g|--gui]
24 +usage: git difftool [-t|--tool=<tool>]
25 + [-x|--extcmd=<cmd>] [-g|--gui]
26 + [--prompt] [-y|--no-prompt]
27 ['git diff' options]
28 USAGE
29 exit 1;
@@ -33,6 +31,7 @@ USAGE
31
32 sub setup_environment
33 {
34 + my $DIR = abs_path(dirname($0));
35 $ENV{PATH} = "$DIR:$ENV{PATH}";
36 $ENV{GIT_PAGER} = '';
37 $ENV{GIT_EXTERNAL_DIFF} = 'git-difftool--helper';
@@ -47,75 +46,57 @@ sub exe
46 return $exe;
47 }
48
50 -sub generate_command
51 -{
52 - my @command = (exe('git'), 'diff');
53 - my $skip_next = 0;
54 - my $idx = -1;
55 - my $prompt = '';
56 - for my $arg (@ARGV) {
57 - $idx++;
58 - if ($skip_next) {
59 - $skip_next = 0;
60 - next;
61 - }
62 - if ($arg eq '-t' || $arg eq '--tool') {
63 - usage() if $#ARGV <= $idx;
64 - $ENV{GIT_DIFF_TOOL} = $ARGV[$idx + 1];
65 - $skip_next = 1;
66 - next;
67 - }
68 - if ($arg =~ /^--tool=/) {
69 - $ENV{GIT_DIFF_TOOL} = substr($arg, 7);
70 - next;
71 - }
72 - if ($arg eq '-x' || $arg eq '--extcmd') {
73 - usage() if $#ARGV <= $idx;
74 - $ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
75 - $skip_next = 1;
76 - next;
77 - }
78 - if ($arg =~ /^--extcmd=/) {
79 - $ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
80 - next;
81 - }
82 - if ($arg eq '-g' || $arg eq '--gui') {
83 - eval {
84 - my $tool = Git::command_oneline('config',
85 - 'diff.guitool');
86 - if (length($tool)) {
87 - $ENV{GIT_DIFF_TOOL} = $tool;
88 - }
89 - };
90 - next;
91 - }
92 - if ($arg eq '-y' || $arg eq '--no-prompt') {
93 - $prompt = 'no';
94 - next;
95 - }
96 - if ($arg eq '--prompt') {
97 - $prompt = 'yes';
98 - next;
99 - }
100 - if ($arg eq '-h') {
101 - usage();
102 - }
103 - push @command, $arg;
49 +# parse command-line options. all unrecognized options and arguments
50 +# are passed through to the 'git diff' command.
51 +my ($difftool_cmd, $extcmd, $gui, $help, $prompt);
52 +GetOptions('g|gui' => \$gui,
53 + 'h' => \$help,
54 + 'prompt!' => \$prompt,
55 + 'y' => sub { $prompt = 0; },
56 + 't|tool:s' => \$difftool_cmd,
57 + 'x|extcmd:s' => \$extcmd);
58 +
59 +if (defined($help)) {
60 + usage();
61 +}
62 +if (defined($difftool_cmd)) {
63 + if (length($difftool_cmd) > 0) {
64 + $ENV{GIT_DIFF_TOOL} = $difftool_cmd;
65 + } else {
66 + print "No <tool> given for --tool=<tool>\n";
67 + usage();
68 }
105 - if ($prompt eq 'yes') {
69 +}
70 +if (defined($extcmd)) {
71 + if (length($extcmd) > 0) {
72 + $ENV{GIT_DIFFTOOL_EXTCMD} = $extcmd;
73 + } else {
74 + print "No <cmd> given for --extcmd=<cmd>\n";
75 + usage();
76 + }
77 +}
78 +if (defined($gui)) {
79 + my $guitool = "";
80 + $guitool = Git::config('diff.guitool');
81 + if (length($guitool) > 0) {
82 + $ENV{GIT_DIFF_TOOL} = $guitool;
83 + }
84 +}
85 +if (defined($prompt)) {
86 + if ($prompt) {
87 $ENV{GIT_DIFFTOOL_PROMPT} = 'true';
107 - } elsif ($prompt eq 'no') {
88 + } else {
89 $ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
90 }
110 - return @command
91 }
92
93 setup_environment();
94 +my @command = (exe('git'), 'diff', @ARGV);
95
96 # ActiveState Perl for Win32 does not implement POSIX semantics of
97 # exec* system call. It just spawns the given executable and finishes
98 # the starting program, exiting with code 0.
99 # system will at least catch the errors returned by git diff,
100 # allowing the caller of git difftool better handling of failures.
120 -my $rc = system(generate_command());
101 +my $rc = system(@command);
102 exit($rc | ($rc >> 8));