Skip to content

Commit

Permalink
Add support for bundling arguments to the default argument parser
Browse files Browse the repository at this point in the history
This adds a new entry to %*SUB-MAIN-OPTS, bundling. If it is enabled,
any single dash argument will be interpreted as a bundle of single
character arguments: -ab will parse to :a, :b instead of :ab. If it is
not enabled, or if the argument starts with a double dash, it will
still parse as :ab as before.
  • Loading branch information
Leont committed Oct 6, 2020
1 parent acd95d7 commit 04164a0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/core.c/Main.pm6
Expand Up @@ -46,6 +46,7 @@ my sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles) {
# Convert raw command line args into positional and named args for MAIN
sub default-args-to-capture($, @args is copy --> Capture:D) {
my $no-named-after = nqp::isfalse(%sub-main-opts<named-anywhere>);
my $bundling = nqp::istrue(%sub-main-opts<bundling>);

my $positional := nqp::create(IterationBuffer);
my %named;
Expand All @@ -72,21 +73,31 @@ my sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles) {

# named
elsif $passed-value
~~ /^ [ '--' | '-' | ':' ] ('/'?) (<-[0..9\.]> .*) $/ { # 'hlfix
my str $arg = $1.Str;
~~ /^ ( '--' | '-' | ':' ) ('/'?) (<-[0..9\.]> .*) $/ { # 'hlfix
my str $arg = $2.Str;
my $split := nqp::split("=",$arg);

# explicit value
if nqp::isgt_i(nqp::elems($split),1) {
my str $name = nqp::shift($split);
%named.push: $name => $0.chars
die "Can't combine bundling with explicit arguments" if $bundling && nqp::iseq_s($0.Str, '-');
%named.push: $name => $1.chars
?? thevalue(nqp::join("=",$split)) but False
!! thevalue(nqp::join("=",$split));
}

# implicit value
else {
%named.push: $arg => !($0.chars);
if $bundling && nqp::iseq_s($0.Str, '-') {
die "Can't combine bundling with explicit negation" if $1.chars;
my @chars = nqp::split('',$arg);
for @chars -> $char {
%named.push: $char => True;
}
}
else {
%named.push: $arg => !($1.chars);
}
}
}

Expand Down

0 comments on commit 04164a0

Please sign in to comment.