On 2020-01-05 14:32, David Santiago wrote:
Hello.

I'm following https://docs.raku.org/language/5to6-nutshell#Getopt::Long
but i still haven't figured it out how do i use a constraint in a
named parameter when processing a command line.

I have this piece of code:

multi sub MAIN("apt",
                 :$layout where $layout ~~
/^<[12345]>\+[kk|1]$|^6\+$|^[studio|unusual]$/ #= Apartment layout.
Values accepted: 1+kk,2+kk,...,5+kk, 6+, studio, unusual
) {
     say "Enter";
}

however when i run it i get the --help message:

$ raku script.raku apt --layout '1+kk'
Usage:
   script.raku [--layout=<Any where { ... }>] apartment

   --layout=<Any where { ... }>    Apartment layout. Values accepted:
1+kk,2+kk,...,5+kk, 6+, studio, unusual


If i make layout a positional parameter then it works correctly. Is
there a way to make this work with a named parameter?
And how do i make possible to use the "--layout" option several times?
Changing the parameter from $layout to @layout doesn't work.



Best regards,
David Santiago



Hi David,

This is probably not going to help, by maybe there
will be something in it that will help.

-T

<perl6.command.line.parcing.values.txt>
perl 6: How to parce values from the command line


References:

https://stackoverflow.com/questions/59112680/perl6-how-do-i-read-mixed-parameters-from-the-command-line
    https://modules.raku.org/dist/Getopt::Long:cpan:LEONT
    https://stackoverflow.com/questions/57041053/main-subroutine
    https://docs.raku.org/language/create-cli#index-entry-MAIN


If you are only interested in parameters with a single dash, you'll need GetOpt::Long

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is an example using Getopt::Long:

use v6;
use Getopt::Long;

my %opt = help => 0, 'r=s' => "", 'q=s' => "", "w=s" => "";
my %options = get-options(%opt).hash;
say %options;

Example run:

$ p.p6  -w xyz -q def -r abc
{help => 0, q => def, r => abc, w => xyz}

----------------------------------

Here is an example using Getopt::Long:

use v6;
use Getopt::Long;

my %opt = help => 0, 'r=s' => "", 'q=s' => "", 'w=s' => "";
my %options = get-options(%opt).hash;
say %options;
say @*ARGS;

Example run:

$ p.p6  -w xyz -q def -r abc hello
{help => 0, q => def, r => abc, w => xyz}
[hello]


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Use the MAIN sub:

#!/usr/bin/env raku

use v6;

sub MAIN(:$these ="These", :$are="Are", :$params="Params") {
    say "$these $are $params";
}

You can type these parameters in any order:

./command-line.p6 --are=well --these=those
those well Params

And will also catch any extra parameter, showing you the actual parameters:

./command-line.p6 --are=well --these=those --not=this_one
Usage:
  ./command-line.p6 [--these=<Any>] [--are=<Any>] [--params=<Any>]

If you are only interested in parameters with a single dash, you'll need GetOpt::Long

</perl6.command.line.parcing.values.txt>

Reply via email to