Skip to content

Instantly share code, notes, and snippets.

@Suor
Created July 11, 2012 10:00
Show Gist options
  • Save Suor/3089395 to your computer and use it in GitHub Desktop.
Save Suor/3089395 to your computer and use it in GitHub Desktop.
Parse command line options with kind of pattern matching
given (\@ARGV) {
when ([]) {
make_all();
}
when ([qr/first|second|third/]) {
make_source(@ARGV);
}
when ([qr/first|second|third/, qr/\d\d\d\d-\d\d-\d\d/]) {
make_source_day(@ARGV);
}
default {
say <<USAGE;
usage: $0 [source [date]]
sources are: first, second or third
date should be in YYYY-mm-dd format
USAGE
exit;
}
}
# or on 5.14+
use v5.14;
use constant ANY => sub {1};
given (\@ARGV) {
make_all() when [];
make_source(@ARGV) when [qr/first|second|third/];
say "Unknown source" when [ANY];
make_source_day(@ARGV) when [qr/first|second|third/, qr/\d\d\d\d-\d\d-\d\d/];
say "Wrong date format" when [qr/first|second|third/, ANY];
say "Unknown source" when [ANY, ANY];
default { usage() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment