perlgetopt-long

Perl Getopt::Long supporting spaces for arguments


I have a Perl script, which uses GetOpts long. A command like this is easily handled:

automate -action build,deploy -modules chat,email,login 

What I want to achieve is to allow the user to give spaces between arguments.

E.g

automate -action build, deploy   -modules chat, email, login

The issue is that GetOpt::Long internally uses @ARGV to set the variables as needed, and a space changes the @ARGV array, which in turn will put only 'build' as an action, and only 'chat' as a module for the script, ignoring the rest of the arguments passed.

Is there a simple way to parse a command line like the one above in Perl?

I hope there is because otherwise I will have to use a very hacky way of changing the @ARGV array before it is passed to GetOpts.

Are there any other robust libraries out there which will do this for me?

---------------------------Tailor-made script--------------------------------

GetOptions("action=s{1,4}"=>\@myactions,
            "modules=s{,}"=>\@mymodules);

foreach(@mymodules)
{
      if($_ eq $mymodules[0])
      {
          $mymodules= $mymodules.$_;
          next;
      }
      if($dashboards =~ m/,$/ || $_ =~ m/^,/)
      {
          $mymodules= $mymodules.$_;
      }
      else
      {
          $mymodules= $mymodules.",".$_;
      }
}

Solution

  • Check out this Options with multiple values section in the Getopt::Long perldoc. It appears similar to what you're looking for.

    Example:

        GetOptions ("action=s{,}" => \@valuelist);
        @values = split(/[\s,]+/,join(',' , @valuelist));
    
        # @values will contain the list of values passed to the option.
        # This can handle the scenarios:
        # <command> -action build,deploy
        # <command> -action build, deploy
        # <command> -action build deploy