Skip to content

Commit

Permalink
Simplify candidate selection in MAIN usage
Browse files Browse the repository at this point in the history
A recent change tried to be smarter on which MAIN candidates to show.  This
was however too smart for its own good, omitting candidates when it shouldn't.
Now limiting of candidates will only happen if the first given parameter
matches a **constant** value as the first parameter.  So if you have multiple
MAIN candidates that have "foo" as the first parameter, and you give "foo"
as the first command line argument, then only those candidates will be
selected for the usage message if no appropriate candidate was found for
execution.  In all other cases, all candidates will be shown if no candidate
was found for execution.

Spotted at Andrew Shitov's talk at the German Perl Workshop.
  • Loading branch information
lizmat committed Mar 22, 2019
1 parent bea5199 commit dd1c805
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/core/Main.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,18 @@ my sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles) {

# Select candidates for which to create USAGE string
sub usage-candidates($capture) {
my @candidates = &main.candidates;
my @positionals = $capture.list;

my @candos;
while @positionals && !@candos {

# Find candidates on which all these positionals match
@candos = @candidates.grep: -> $sub {
my @params = $sub.signature.params;
if @positionals <= @params {
(^@positionals).first( -> int $i {
!(@params[$i].constraints.ACCEPTS(@positionals[$i]))
} ).defined.not
my @candidates = &main.candidates.grep: { !.?is-hidden-from-USAGE }
if $capture.list -> @positionals {
my $first := @positionals[0];
if @candidates.grep: -> $sub {
if $sub.signature.params[0].cool_constant -> $literal {
$literal.ACCEPTS($first)
}
} -> @candos {
return @candos;
}
@positionals.pop;
}
(@candos || @candidates)
.grep: { nqp::not_i(nqp::can($_,'is-hidden-from-USAGE')) }
@candidates
}

for usage-candidates(capture) -> $sub {
Expand Down

0 comments on commit dd1c805

Please sign in to comment.