develooper Front page | perl.perl6.users | Postings from April 2025

Re: I need help with get-options

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
April 2, 2025 10:47
Subject:
Re: I need help with get-options
Message ID:
[email protected]
On 4/1/25 4:04 PM, Bruce Gray wrote:
> 
> 
>> On Apr 1, 2025, at 03:55, ToddAndMargo via perl6-users <[email protected]> wrote:
> 
> --snip--
> 
>> I have the following run string:
>> raku C:\NtUtil\RLA.Backup.raku --rotates 345 --UNC_BackupPath \\192.168.240.10\Backup\MyDocsBackup\backup1 --debug
>>
>>
>> use Getopt::Long;  # get-options
>> get-options('debug' => $CommandLine.debug );
>>
>> error out with
>>     No such method 'debug' for invocant of type 'List'
>>
>> What am I doing wrong?
> 
> You are not giving us a https://en.wikipedia.org/wiki/Minimal_reproducible_example , so I am having to guess.
> My guess is that you have defined `$CommandLine` in a way that lacks a writeable `.debug` method.
> 
> You *could* simplify the call to `get-options` to use a simple temp variables (similar to the documentation), then copy the temp into some `debug` and similar parts  of your more complex `$CommandLine` data structure, but I expect your full code is trying to avoid such temp vars.
> 
> Here is a complete runnable program to demonstrate skipping any temp vars, using a wild guess that `$CommandLine` is the sole instance of a OO data class:
> 
>      class CommandLineInfo {
>          has Bool $.debug is rw = False;
>      }
> 
>      my CommandLineInfo $CommandLine .= new;
> 
>      use Getopt::Long;
>      get-options( 'debug' => $CommandLine.debug );
> 
>      say $CommandLine.debug; # Will be `True` or `False`, depending on command-line arg.
> 
> If this code does not align with (and is not adaptable to) your use case, we (or at least *I*) will need more information from you, especially the definition of `$CommandLine` in your current code.
> As always, minimal *runnable* code will allow any of us to provide an answer to you more quickly.
> 
> FWIW, I use `sub MAIN`, but if I were to use `Getopt::Long`, I might use the (under-documented) method of having `get-options` build the data structure itself:
> 	use Getopt::Long;
> 	my %CommandLine = get-options( 'debug', 'rotates=i', 'UNC_BackupPath=p' ).hash;
> 	say ?%CommandLine<debug>;   # Just the `debug` argument, forced to Bool
> 	say %CommandLine.raku;      # All the specified arguments.
> 
> 
>> Many thanks,
>> -T
> 
> You are very welcome!


Hi Bruce,

Sorry.  I do know I am suppose to post some minimal code.
I was programming for 11 straight hours and was not
thinking too clearly.

I was trying to do what you said.  Read it into a hash, then
extract the values into an OOP structure.    (I use to like
hashes, but dropped them hard when I figured out OOP
structures.  I absolutely  A-D-O-R-E  OOP structures.)

Thank you again for the help!
-T


Windows Server 2025 (souped up W11)

raku -v
Welcome to RakudoΓäó v2025.02.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2025.02.

https://raku.land/cpan:LEONT/Getopt::Long


<getopstest.raku>
#!/usr/bin/env raku

use lib 'C:/NtUtil', 'C:/NtUtil/p6lib';   # use this one on customer 
machines
use Getopt::Long;  # get-options


class CommandLineClass {
     has Bool $.help           is rw;
     has Bool $.debug          is rw;
     has Str  $.UNC_BackupPath is rw;
     has Int  $.rotates        is rw;
     has      $.ParentDir      is rw;
}

my $CommandLine = CommandLineClass.new{
     help           => False,
     debug          => False,
     UNC_BackupPath => Q[\\192.168.240.10\MyDocsBackup\backup1],
     rotates        => 2,
     ParentDir      => "/"
};


# raku getopstest.raku --rotates 456 --UNC_BackupPath 
\\192.168.240.235\MyDocsBackup\\backup1 --help --debug

my %opts;
# %opts = ( 'help'  => False, 'debug' => False, 'UNC_BackupPath=s' => 
"", 'rotates=i' => 0 );


# %opts = get-options( %opts ).hash;
%opts = get-options( 'help', 'debug', 'UNC_BackupPath=s', 'rotates=i' 
).hash;
say "\%opts =\n" ~ %opts ~ "\n";

$CommandLine.UNC_BackupPath = %opts<UNC_BackupPath>.Str;
$CommandLine.help           = %opts<help>;
$CommandLine.debug          = %opts<debug>;
$CommandLine.rotates        = %opts<rotates>.Int;

if $CommandLine.debug  {
    print $CommandLine.help ~ "\n";
    print $CommandLine.debug ~ "\n";
    print $CommandLine.UNC_BackupPath ~ "\n";
    print $CommandLine.rotates ~ "\n";
}
</getopstest.raku>


C:\NtUtil>raku getopstest.raku --rotates 456 --UNC_BackupPath 
\\192.168.240.235\MyDocsBackup\\backup1 --help --debug

%opts =
UNC_BackupPath  \\192.168.240.235\MyDocsBackup\\backup1
debug   True
help    True
rotates 456

No such method 'UNC_BackupPath' for invocant of type 'List'
   in method throw at 'SETTING::'src/core.c/Exception.rakumod line 65
   in block <unit> at getopstest.raku line 34

Line 34 is
    $CommandLine.UNC_BackupPath = %opts<UNC_BackupPath>.Str;

Line 34-37 all give the same error if I comment out the ones
above them


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at [email protected] | Group listing | About