开发者

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/^,/)
      {
   开发者_JAVA百科       $mymodules= $mymodules.$_;
      }
      else
      {
          $mymodules= $mymodules.",".$_;
      }
}


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


That's a non-standard command line usage - so you'll need a non-standard command line parser. There are about 180 separate entries listed if you do a search for 'getopt' at http://search.cpan.org/, so there are many to choose from.

Superficially, you simply want to recognize some long option names, and then keep applying non-option arguments to the previous option name as they're read.

Would you insist on trailing commas? It feels clunky to do so. I can see:

automate -action build deploy -modules chat email login

Requiring commas at the end of some arguments would feel - weird.

You'd need to consider whether a double-dash option has special significance, and whether a single dash option has special significance:

somecmd -f - --

I don't know of a Perl module that handles your chosen notation, or any of the minor variations on it. That isn't quite the same as saying there is no such module, but you are attempting a slightly unusual argument parsing style, so it is quite likely that no-one has implemented exactly what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜