Re: I need help with get-options

2025-04-04 Thread ToddAndMargo via perl6-users

On 4/2/25 8:22 AM, Bruce Gray wrote:


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


The problem is with the syntax of the `new`.
You need parenthesis instead of curly braces.
With that change, your code works as expected.


And I have it right on every other .new in my code.
Even my keeper how to has it right.  Mumble, Mumble.

Question: should the compiler have caught this?  Or
is there some other purpose for the .new{} syntax?



For even DRYer code, you can flatten `%opts` directly into the `new` 
constructor, like so:
     # use lib 'C:/NtUtil', 'C:/NtUtil/p6lib';   # use this one on 
customer machines

     use Getopt::Long;
     class CommandLineClass is rw {
         has Bool $.help           = False;
         has Bool $.debug          = False;
         has Str  $.UNC_BackupPath = Q[\ 
\192.168.240.10\MyDocsBackup\backup1];

         has Int  $.rotates        = 2;
         has      $.ParentDir      = '/';
     }
     my %opts = get-options( 'help', 'debug', 'UNC_BackupPath=s', 
'rotates=i', 'ParentDir=s' ).hash;

     my CommandLineClass $CommandLine .= new( |%opts );
     say $CommandLine.raku if $CommandLine.debug;


Sweet!



--
Hope this helps,
Bruce Gray (Util of PerlMonks)



Very much so!  Thank you!



Re: I need help with get-options

2025-04-02 Thread Bruce Gray


> On Apr 2, 2025, at 05:47, ToddAndMargo via perl6-users  
> wrote:

--snip--

> 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 understand.

> 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

--snip--


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

The problem is with the syntax of the `new`.
You need parenthesis instead of curly braces.
With that change, your code works as expected.

For even DRYer code, you can flatten `%opts` directly into the `new` 
constructor, like so:
# use lib 'C:/NtUtil', 'C:/NtUtil/p6lib';   # use this one on customer 
machines
use Getopt::Long;
class CommandLineClass is rw {
has Bool $.help   = False;
has Bool $.debug  = False;
has Str  $.UNC_BackupPath = Q[\\192.168.240.10\MyDocsBackup\backup1];
has Int  $.rotates= 2;
has  $.ParentDir  = '/';
}
my %opts = get-options( 'help', 'debug', 'UNC_BackupPath=s', 'rotates=i', 
'ParentDir=s' ).hash;
my CommandLineClass $CommandLine .= new( |%opts );
say $CommandLine.raku if $CommandLine.debug;

-- 
Hope this helps,
Bruce Gray (Util of PerlMonks)



Re: I need help with get-options

2025-04-02 Thread ToddAndMargo via perl6-users

On 4/1/25 4:04 PM, Bruce Gray wrote:




On Apr 1, 2025, at 03:55, ToddAndMargo via perl6-users  
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;   # 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



#!/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  $.rotatesis 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.Str;
$CommandLine.help   = %opts;
$CommandLine.debug  = %opts;
$CommandLine.rotates= %opts.Int;

if $CommandLine.debug  {
   print $CommandLine.help ~ "\n";
   print $CommandLine.debug ~ "\n";
   print $CommandLine.UNC_BackupPath ~ "\n";
   print $CommandLine.rotates ~ "\n";
}



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
helpTrue
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  at getopstest.raku line 34

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

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



Re: I need help with get-options

2025-04-01 Thread ToddAndMargo via perl6-users

Should have said "I need help with get-options"




Re: I need help with get-options

2025-04-01 Thread Bruce Gray



> On Apr 1, 2025, at 03:55, ToddAndMargo via perl6-users  
> 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;   # Just the `debug` argument, forced to Bool
say %CommandLine.raku;  # All the specified arguments.


> Many thanks,
> -T

You are very welcome!
-- 
Bruce Gray (Util of PerlMonks)