Perl Getopt::Long , supporting spaces for arguments

有些话、适合烂在心里 提交于 2019-12-12 18:17:25

问题


I have a perl script, which uses GetOpts long.A command like

automate -action build,deploy -modules chat,email,login is easily handled.

What I want to achieve is, allow 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.",".$_;
      }
}

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5645355/perl-getoptlong-supporting-spaces-for-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!