Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: GetOpt unknown args

by haukex (Archbishop)
on Oct 25, 2018 at 20:25 UTC ( [id://1224704]=note: print w/replies, xml ) Need Help??


in reply to GetOpt unknown args

Also, I found out that I can use '--' option. But it really feels to me as a workaround and I don't want to make the users use it.

I don't think it's a workaround, using -- to indicate "stop processing options here" is a really common thing - perl, git, GNU getopt, and many other tools do it, which is why Getopt::Long supports it.

I also don't think messing with @ARGV is a bad thing to do, especially since you'd just be looking for a fixed string.

use warnings; use strict; use List::Util qw/first/; @ARGV = qw/create --folder empty --size 50 --additional hey how ---are --you 555/; my $i = first { $ARGV[$_] eq '--additional' } 0..$#ARGV; my (undef,@add) = defined $i ? splice @ARGV, $i, $#ARGV-$i+1 : (); use Data::Dump; dd \@ARGV, \@add; __END__ ( ["create", "--folder", "empty", "--size", 50], ["hey", "how", "---are", "--you", 555], )

Admittedly not the most beautiful code, but it works. If you want more elegance, I'd suggest either -- or hippo's suggestion...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found