summaryrefslogtreecommitdiff
path: root/bin/efa
diff options
context:
space:
mode:
authorDaniel Friesel <derf@derf.homelinux.org>2010-06-05 18:40:21 +0200
committerDaniel Friesel <derf@derf.homelinux.org>2010-06-05 18:40:21 +0200
commit1ba1ca71b347fe7b9fc168e5a3e4eec4518af8d0 (patch)
tree03134efc56dab2586517ebb230df7cedf778b1b4 /bin/efa
parentb71529f3b7fed44b337027a623252db346acd1a0 (diff)
Parse most options via subs instead of vars
Diffstat (limited to 'bin/efa')
-rwxr-xr-xbin/efa204
1 files changed, 104 insertions, 100 deletions
diff --git a/bin/efa b/bin/efa
index c9cdb36..ea5724b 100755
--- a/bin/efa
+++ b/bin/efa
@@ -23,111 +23,33 @@ my $www = WWW::Mechanize->new(
);
my (@from, @to, @via);
my ($from_type, $to_type, $via_type) = ('stop') x 3;
-my ($time, $time_depart, $time_arrive);
-my $date;
-my @exclude;
-my $maxinter;
-my $include;
-my $prefer;
-my $proximity;
-my $walk_speed;
-my $with_bike;
-my $timeout = 60;
my $ignore_info = 'Fahrradmitnahme';
my ($test_dump, $test_parse);
my $xp_ambiguous = '//select';
-GetOptions(
- 'a|arrive=s' => \$time_arrive,
- 'b|bike' => \$with_bike,
- 'd|date=s' => \$date,
- 'depart=s' => \$time_depart,
- 'e|exclude=s' => \@exclude,
- 'from=s{2}' => \@from,
- 'from-type=s' => \$from_type,
- 'h|help' => sub {exec('perldoc', '-F', $0)},
- 'I|ignore-info=s{0,1}' => \$ignore_info,
- 'm|max-change=i' => \$maxinter,
- 'post=s' => \%post,
- 'P|prefer=s' => \$prefer,
- 'p|proximity' => \$proximity,
- 'i|include=s' => \$include,
- 'test-dump' => \$test_dump,
- 'test-parse' => \$test_parse,
- 't|time=s' => \$time,
- 'timeout=i' => \$timeout,
- 'to=s{2}' => \@to,
- 'to-type=s' => \$to_type,
- 'v|version' => sub {print "efa version $version\n"; exit 0},
- 'via=s{2}' => \@via,
- 'via-type=s' => \$via_type,
- 'w|walk-speed=s' => \$walk_speed,
-
-) or die("Please see $0 --help\n");
-
-@exclude = split(/,/, join(',', @exclude));
-
-if (not (@from and @to)) {
- if (@ARGV == 4) {
- (@from[0,1], @to[0,1]) = @ARGV;
- }
- elsif (@ARGV == 6) {
- (@from[0,1], @via[0,1], @to[0,1]) = @ARGV;
- }
-}
-
-if (@to != 2 or @from != 2) {
- die("Insufficient to/from arguments, see $0 --help for usage\n");
-}
-
-for my $pair (
- [$from[1], \$from_type],
- [$via[1] , \$via_type ],
- [$to[1] , \$to_type ],
-) {
- next if (not defined $pair->[0]);
- for my $type (['addr', 'address'], ['poi', 'poi']) {
- if ($pair->[0] =~ s/ ^ $type->[0] : \s* (.+) $ /$1/x) {
- ${$pair->[1]} = $type->[1];
- }
- }
-}
-
-@post{'place_origin', 'name_origin'} = @from;
-@post{'place_destination', 'name_destination'} = @to;
-if (@via == 2) {
- @post{'place_via', 'name_via'} = @via;
-}
-
-# note that $from_type etc are changed here
-foreach my $type ($from_type, $to_type, $via_type) {
- if (not ($type ~~ ['stop', 'address', 'poi'])) {
- $type = 'stop';
- die("from/to/via type: Must be stop, address or poi, not '$type'\n");
- }
-}
-
-$post{type_origin} = $from_type;
-$post{type_destination} = $to_type;
-$post{type_via} = $via_type;
-
-if ($time_arrive) {
- $time = $time_arrive;
+sub opt_time_arr {
$post{itdTripDateTimeDepArr} = 'arr';
+ opt_time(@_);
}
-elsif ($time_depart) {
- $time = $time_depart;
+
+sub opt_time_dep {
$post{itdTripDateTimeDepArr} = 'dep';
+ opt_time(@_);
}
-if ($time) {
+sub opt_time {
+ my (undef, $time) = @_;
+
if ($time !~ /^ [0-2]? \d : [0-5]? \d $/x) {
die("Invalid argument. Usage: --time HH:MM\n");
}
@post{'itdTimeHour', 'itdTimeMinute'} = split(/:/, $time);
}
-if ($date) {
+
+sub opt_date {
+ my ($date) = @_;
+
if ($date !~ /^ [0-3]? \d \. [01]? \d \. (?: \d{4} )? $/x) {
die("Invalid argument: Usage: --date DD.MM.[YYYY]\n");
}
@@ -135,12 +57,14 @@ if ($date) {
$post{itdDateYear} //= (localtime(time))[5] + 1900;
}
-if (@exclude) {
+sub opt_exclude {
my @mapping = qw/
zug s-bahn u-bahn stadtbahn tram stadtbus regionalbus
schnellbus seilbahn schiff ast sonstige
/;
my $ok = 0;
+ my (undef, $str) = @_;
+ my @exclude = split(/,/, $str);
foreach my $exclude_type (@exclude) {
for my $map_id (0 .. $#mapping) {
@@ -155,12 +79,14 @@ if (@exclude) {
}
}
-# Argument verification done by Getopt::Long
-if (defined($maxinter)) {
- $post{maxChanges} = $maxinter;
+sub opt_maxinter {
+ my (undef, $opt) = @_;
+ $post{maxChanges} = $opt;
}
-if ($prefer) {
+sub opt_prefer {
+ my (undef, $prefer) = @_;
+
given ($prefer) {
when ('speed') { $post{routeType} = 'LEASTTIME' }
when ('nowait') { $post{routeType} = 'LEASTINTERCHANGE' }
@@ -171,11 +97,13 @@ if ($prefer) {
}
}
-if ($proximity) {
+sub opt_proximity {
$post{useProxFootSearch} = 1;
}
-if ($include) {
+sub opt_include {
+ my (undef, $include) = @_;
+
given ($include) {
when ('local') { $post{lineRestriction} = 403 }
when ('ic') { $post{lineRestriction} = 401 }
@@ -187,7 +115,9 @@ if ($include) {
}
}
-if ($walk_speed) {
+sub opt_walk_speed {
+ my (undef, $walk_speed) = @_;
+
if ($walk_speed ~~ ['normal', 'fast', 'slow']) {
$post{changeSpeed} = $walk_speed;
}
@@ -196,17 +126,91 @@ if ($walk_speed) {
}
}
-if ($with_bike) {
+sub opt_bike {
$ignore_info = undef;
$post{bikeTakeAlong} = 1;
}
+sub opt_timeout {
+ my (undef, $timeout) = @_;
+ $www->timeout($timeout);
+}
+
+GetOptions(
+ 'a|arrive=s' => \&opt_time_arr,
+ 'b|bike' => \&opt_bike,
+ 'd|date=s' => \&opt_date,
+ 'depart=s' => \&opt_time_dep,
+ 'e|exclude=s' => \&opt_exclude,
+ 'from=s{2}' => \@from,
+ 'from-type=s' => \$from_type,
+ 'h|help' => sub {exec('perldoc', '-F', $0)},
+ 'I|ignore-info=s{0,1}' => \$ignore_info,
+ 'm|max-change=i' => \&opt_maxinter,
+ 'post=s' => \%post,
+ 'P|prefer=s' => \&opt_prefer,
+ 'p|proximity' => \&opt_proximity,
+ 'i|include=s' => \&opt_include,
+ 'test-dump' => \$test_dump,
+ 'test-parse' => \$test_parse,
+ 't|time=s' => \&opt_time,
+ 'timeout=i' => \&opt_timeout,
+ 'to=s{2}' => \@to,
+ 'to-type=s' => \$to_type,
+ 'v|version' => sub {print "efa version $version\n"; exit 0},
+ 'via=s{2}' => \@via,
+ 'via-type=s' => \$via_type,
+ 'w|walk-speed=s' => \&opt_walk_speed,
+
+) or die("Please see $0 --help\n");
+
+if (not (@from and @to)) {
+ if (@ARGV == 4) {
+ (@from[0,1], @to[0,1]) = @ARGV;
+ }
+ elsif (@ARGV == 6) {
+ (@from[0,1], @via[0,1], @to[0,1]) = @ARGV;
+ }
+}
+
+if (@to != 2 or @from != 2) {
+ die("Insufficient to/from arguments, see $0 --help for usage\n");
+}
+
+for my $pair (
+ [$from[1], \$from_type],
+ [$via[1] , \$via_type ],
+ [$to[1] , \$to_type ],
+) {
+ next if (not defined $pair->[0]);
+ for my $type (['addr', 'address'], ['poi', 'poi']) {
+ if ($pair->[0] =~ s/ ^ $type->[0] : \s* (.+) $ /$1/x) {
+ ${$pair->[1]} = $type->[1];
+ }
+ }
+}
+
+@post{'place_origin', 'name_origin'} = @from;
+@post{'place_destination', 'name_destination'} = @to;
+if (@via == 2) {
+ @post{'place_via', 'name_via'} = @via;
+}
+
+foreach my $type ($from_type, $to_type, $via_type) {
+ if (not ($type ~~ ['stop', 'address', 'poi'])) {
+ die("from/to/via type: Must be stop, address or poi, not '$type'\n");
+ }
+}
+
+$post{type_origin} = $from_type;
+$post{type_destination} = $to_type;
+$post{type_via} = $via_type;
+
if ($test_parse) {
local $/;
$content = <STDIN>;
}
else {
- $www->timeout($timeout);
$www->get($firsturl);
$www->submit_form(
form_name => 'jp',