use v6;
my %all-flags := {};
grammar OptionsParser {
rule TOP {
^^ ('-' <flag-with-values>)+ %% <.ws> $$
}
token flag-with-values {
<flag-name> <.ws> <flag-value>
}
token flag-name {
<alnum>+
}
token flag-value {
<[a..zA..Z0..9\-]>+
}
}
class OptionsParserActions {
method flag-with-values($/) {
my ($flag-name, $flag-value) = ($/<flag-name>.Str, $/<flag-value>.Str);
%all-flags{$flag-name} = $flag-value;
}
}
sub parse-options($flags) {
OptionsParser.parse(@*ARGS.join(' '), :actions(OptionsParserActions));
%all-flags;
}
DOC CHECK {
use Test;
subtest "Test case" => {
my $input = <-a 1 -b 2 -c a-b-c -d four>;
parse-options($input).&is({ 'a' => '1', 'b' => '2', 'c' => 'a-b-c', 'd' => 'four' });
}
}