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

Hey guys!
I would like to create a script which gets input from the user via the flags. I use the GetOpt module for that. It looks like:

./my_script <action> --option1 --option2 ...

Example:

./my_script create --folder empty --size 50 ...

The action is not part of the GetOpt flags so I get it as $ARGV[0].
I would like to add a new flag that gets a string which can contain other flags (or any other string in that matter), something like this:

./my_script create --folder empty --size 50 --additional hey how ---are --you 555

So I can insert it into a variable:

$var = "hey how ---are --you 555"

I did some research and found out that I can use the pass_through flag of GetOpt. Problem is that I can't use the module Getopt::Long::Configure("pass_through") so I'm stuck with Getopt::Long.
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.

Furthermore, I can play with @ARGV array by removing the action and the additional information and then using the GetOpt, something like this:

1. insert the action into a variable and remove it from the @ARGV
2. insert the additional information into a variable and remove it from the @ARGV
3. Use GetOpt on the new @ARGV.

But it does not feel so right.
Are there any other methods? (I asked a similar question on another forum but there weren't any other answers).

Replies are listed 'Best First'.
Re: GetOpt unknown args
by haukex (Archbishop) on Oct 25, 2018 at 20:25 UTC
    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...

Re: GetOpt unknown args
by Corion (Patriarch) on Oct 25, 2018 at 16:11 UTC
Re: GetOpt unknown args
by hippo (Archbishop) on Oct 25, 2018 at 16:10 UTC

    Is there any reason not to invoke your script as:

    ./my_script create --folder empty --size 50 --additional "hey how ---a +re --you 555"

    That's how pretty much any other command handles such arguments and it is what I as a user would expect to do to feed such an argument to an option.

    I asked a similar question on another forum

    Would you be so good as to provide a link, please?