Skip to content

Commit

Permalink
make Main.pm output a usage message on an unexpected named argument i…
Browse files Browse the repository at this point in the history
…nstead of bind failing

Signed-off-by: Moritz Lenz <moritz@faui2k3.org>
  • Loading branch information
felher authored and moritz committed Jul 30, 2012
1 parent f4b83df commit 705fabb
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/core/Main.pm
Expand Up @@ -146,14 +146,27 @@ my sub MAIN_HELPER($retval = 0) is hidden_from_backtrace {
return $usage;
}

sub has-unexpected-named-arguments($signature, %named-arguments) {
my %accepts-argument = $signature.params.grep({ .named }).map({ .named_names }) Z=> 1 xx *;
for %named-arguments.keys -> $name {
return True if !%accepts-argument{$name}
}

return False;
}

# Process command line arguments
my ($p, $n) = process-cmd-args(@*ARGS).lol;

# Generate default $?USAGE message
my $?USAGE = gen-usage();

# If dispatch to MAIN is possible, do so
if $m.candidates_matching(|@($p), |%($n)).elems {
# Get a list of candidates that match according to the dispatcher
my @matching_candidates = $m.candidates_matching(|@($p), |%($n));
# Sort out all that would fail due to binding
@matching_candidates .=grep: {!has-unexpected-named-arguments($_.signature, $n)};
# If there are still some candidates left, try to dispatch to MAIN
if +@matching_candidates {
return $m(|@($p), |%($n));
}

Expand Down

0 comments on commit 705fabb

Please sign in to comment.