Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow optional named parameters in CommandLine.pm
Specified with foo=s? get the passed value if any, empty string
otherwise. So --foo will set %options<foo> to '', --foo=bar will result
in %options<foo> := 'bar'. In the first case, %options<foo> will still
be false, but the existance of the argument can be checked with
pir::exists()
  • Loading branch information
Tadeusz Sośnierz committed Aug 22, 2011
1 parent 6fe5a07 commit 3e5dcec
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/HLL/CommandLine.pm
Expand Up @@ -178,7 +178,12 @@ class HLL::CommandLine::Parser {

method wants-value($x) {
my $spec := %!options{$x};
$spec eq 's';
pir::substr($spec, 0, 1) eq 's';
}

method optional-value($x) {
my $spec := %!options{$x};
$spec eq 's?';
}

method parse(@args) {
Expand Down Expand Up @@ -226,9 +231,12 @@ class HLL::CommandLine::Parser {
$value := pir::substr($opt, $idx + 1);
$opt := pir::substr($opt, 0, $idx);
$has-value := 1;
} elsif self.optional-value($opt) {
$value := '';
$has-value := 1;
}
pir::die("Illegal option --$opt") unless pir::exists(%!options, $opt);
pir::die("Option --$opt does not allow a value") if %!options{$opt} ne 's' && $has-value;
pir::die("Option --$opt does not allow a value") if !self.wants-value($opt) && $has-value;
if !$has-value && self.wants-value($opt) {
$value := get-value("--$opt");
}
Expand Down

0 comments on commit 3e5dcec

Please sign in to comment.