Created
July 11, 2012 10:00
-
-
Save Suor/3089395 to your computer and use it in GitHub Desktop.
Parse command line options with kind of pattern matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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