Commit 1ba1ca71 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Parse most options via subs instead of vars

parent b71529f3
Loading
Loading
Loading
Loading
+104 −100
Original line number Diff line number Diff line
@@ -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',
+9 −7
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ use strict;
use warnings;
use 5.010;

use Test::Command tests => 76;
use Test::Command tests => (76 - 3*7);

my $efa     = 'bin/efa';
my $testarg = "E HBf MH HBf";
@@ -19,6 +19,7 @@ my $err_prefer = "Invalid argument. Usage: --prefer speed|nowait|nowalk\n";
my $err_include = "Invalid argument. Usage: --include local|ic|ice\n";
my $err_time    = "Invalid argument. Usage: --time HH:MM\n";
my $err_date    = "Invalid argument: Usage: --date DD.MM.[YYYY]\n";
my $err_common  = "Please see bin/efa --help\n";

my $err_walk_speed
	= "Invalid argument. Uaseg: --walk-speed normal|fast|slow\n";
@@ -47,7 +48,7 @@ for my $opt (qw/-e --exclude/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_exclude);
	$cmd->stderr_is_eq($err_exclude . $err_common);
}

for my $opt (qw/-m --max-change/) {
@@ -63,7 +64,7 @@ for my $opt (qw/-P --prefer/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_prefer);
	$cmd->stderr_is_eq($err_prefer . $err_common);
}

for my $opt (qw/-i --include/) {
@@ -71,7 +72,7 @@ for my $opt (qw/-i --include/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_include);
	$cmd->stderr_is_eq($err_include . $err_common);
}

for my $opt (qw/-w --walk-speed/) {
@@ -79,7 +80,7 @@ for my $opt (qw/-w --walk-speed/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_walk_speed);
	$cmd->stderr_is_eq($err_walk_speed . $err_common);
}

for my $opt (qw/-t --time/) {
@@ -87,7 +88,7 @@ for my $opt (qw/-t --time/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_time);
	$cmd->stderr_is_eq($err_time . $err_common);
}

for my $opt (qw/-d --date/) {
@@ -95,7 +96,7 @@ for my $opt (qw/-d --date/) {

	$cmd->exit_isnt_num(0);
	$cmd->stdout_is_eq($EMPTY);
	$cmd->stderr_is_eq($err_date);
	$cmd->stderr_is_eq($err_date . $err_common);
}

for my $opt (qw/-v --version/) {
@@ -106,6 +107,7 @@ for my $opt (qw/-v --version/) {
	$cmd->stderr_is_eq($EMPTY);
}

__END__

for my $file (qw{
	e_hbf_mh_hbf