Skip to content
Permalink
Browse files

Simplify the MAIN_HELPER code some more

- remove unnecessary variables
- merge several if's into an if / elsif / else sequence
- remove now unnecessary return statements
- change a .grep to a .first, as we need a boolean anyway
- use a Set instead of a Z=> 1 xx *
- lose scope on loop
  • Loading branch information...
lizmat committed Oct 9, 2018
1 parent 1d41213 commit 7c9b2955dbafbe1961456555197b65ebf1ea0d2d
Showing with 9 additions and 16 deletions.
  1. +9 −16 src/core/Main.pm6
@@ -63,7 +63,6 @@ my sub MAIN_HELPER($IN-as-ARGSFILES, $retval = 0) {
nqp::push($positional, thevalue($passed-value));
}
}

Capture.new( list => $positional.List, hash => %named )
}

@@ -188,20 +187,16 @@ my sub MAIN_HELPER($IN-as-ARGSFILES, $retval = 0) {
@help-msgs.append(@arg-help.map: { ' ' ~ .key ~ ' ' x ($offset - .key.chars) ~ .value });
}

my $usage = "Usage:\n" ~ @help-msgs.map(' ' ~ *).join("\n");
$usage;
"Usage:\n" ~ @help-msgs.map(' ' ~ *).join("\n")
}

sub has-unexpected-named-arguments($signature, %named-arguments) {
my @named-params = $signature.params.grep: *.named;
return False if @named-params.grep: *.slurpy;

my %accepts-argument = @named-params.map({ .named_names.Slip }) Z=> 1 xx *;
for %named-arguments.keys -> $name {
return True if !%accepts-argument{$name}
}
return False if @named-params.first: *.slurpy;

False;
my %accepts-argument is Set = @named-params.map( *.named_names.Slip );
return True unless %accepts-argument{$_} for %named-arguments.keys;
False
}

# Process command line arguments
@@ -230,24 +225,22 @@ my sub MAIN_HELPER($IN-as-ARGSFILES, $retval = 0) {
my $*ARGFILES := IO::ArgFiles.new: (my $in := $*IN),
:nl-in($in.nl-in), :chomp($in.chomp), :encoding($in.encoding),
:bin(nqp::hllbool(nqp::isfalse($in.encoding)));
$main(|$capture);
$main(|$capture).sink;
}
else {
$main(|$capture);
$main(|$capture).sink;
}
return;
}

# We could not find the correct MAIN to dispatch to!
# Let's try to run a user defined USAGE sub
if callframe(1).my<&USAGE> -> $usage {
elsif callframe(1).my<&USAGE> -> $usage {
$usage();
return;
}

# We could not find a user defined USAGE sub!
# Let's display the default USAGE message
if $capture<help> {
elsif $capture<help> {
$*OUT.say($*USAGE);
exit 0;
}

0 comments on commit 7c9b295

Please sign in to comment.
You can’t perform that action at this time.