Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

GetOptions getting a string

by ovedpo15 (Pilgrim)
on Mar 24, 2018 at 18:24 UTC ( [id://1211676]=perlquestion: print w/replies, xml ) Need Help??

ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks!

I would like to use Getopt::Long module in to declare a flag which can get one string, many strings or none.
I know how to make each one separate, but I don't know how to do all of them.
For example:
GetOptions("flag=s{,}" => "$flag");

If I run "./perl_script.pl --flag" it will say that I didn't gave it a string.
How can I handle this case? I would like that the following code will work:

"./perl_script.pl --flag"
"./perl_script.pl --flag a"
"./perl_script.pl --flag a,b,c,d,e,f"

Replies are listed 'Best First'.
Re: GetOptions getting a string
by poj (Abbot) on Mar 24, 2018 at 19:47 UTC

    From the docs for Getopt::Long

    Using a colon : instead of the equals sign indicates that the option value is optional. In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while numeric options are set to 0 .
    GetOptions("flag:s" => \$flag);

    poj
Re: GetOptions getting a string
by BillKSmith (Monsignor) on Mar 24, 2018 at 20:13 UTC
    I agree with Your Mother that this is probably not a good idea. If you really need it, here is a way to do it.
    C:\Users\Bill\forums\monks>type perl_script.pl use strict; use warnings; use Getopt::Long; my $temp; GetOptions("flag:s" => \$temp); my @flags = split /,/, $temp ; print join(' ', @flags,), ' ', scalar @flags, " strings\n"; C:\Users\Bill\forums\monks>.\perl_script.pl --flag 0 strings C:\Users\Bill\forums\monks>.\perl_script.pl --flag a a 1 strings C:\Users\Bill\forums\monks>.\perl_script.pl --flag a,b,c a b c 3 strings
    Bill
Re: GetOptions getting a string
by Your Mother (Archbishop) on Mar 24, 2018 at 19:11 UTC

    For me, the use case seems too DWIM. I could be wrong and just can't think of an ideal case but it seems like you're trying to overload an argument list to be a trigger as well. It might be better served by either a default noop trigger arg like -flag 0 or -flag . or just having a new boolean flag, like -runnit, to do what you want -flag without arguments to do. You also have the option of processing your own argument list after (or even before but after is much more sane) Getopt is done if its options don't cover exactly what you want.

Re: GetOptions getting a string
by RichardK (Parson) on Mar 25, 2018 at 10:20 UTC
    Also from Getopt::Long see the section 'Options with multiple values'

    It says :-

    Often it is useful to allow comma-separated lists of values as well as multiple occurrences of the options. This is easy using Perl's split() + and join() operators: GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(',',@libfiles));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1211676]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found