Long可以容纳变量选项名吗?

I am writing a script that can do something like:

script-name --resource1=xxx --resource2=xxx

But this can go up to 50+. Is there a way to make GetOpt accept dynamic option names?

#0

What about auto-generating the options list for Getopt::Long like example below? Since the list of options will be likely pretty long, using Getopt::ArgvFile allow you to supply configuration file with options instead of specifying them on command-line.

use Getopt::Long;
use Getopt::ArgvFile;
use Data::Dump;

my @n = (1 .. 10);    # how many resources allowed
my %opts = (
    port                  => ':i',
    http_version          => ':s',
    invert_string         => ':s',
    ssl                   => '',
    expect                => ':s',
    string                => ':s',
    post_data             => ':s',
    max_age               => ':i',
    content_type          => ':s',
    regex                 => ':s',
    eregi                 => ':s',
    invert_regex          => '',
    authorization         => ':s',
    useragent             => ':s',
    pagesize              => ':s',
    expected_content_type => ':s',
    verify_xml            => '',
    rc                    => ':i',
    hostheader            => ':s',
    cookie                => ':s',
    encoding              => ':s',
    max_redirects         => ':i',
    onredirect_follow     => ':i',
    yca_cert              => ':s',
);

my %args = ();
GetOptions(\%args,
    map {
        my $i = $_;
        ( "resource$i:s", map { "resource${i}_$_$opts{$_}" } keys %opts )
    } @n
) or die;

dd \%args;

#1

Yes, as I just figured out how to do it myself as I wanted to accept a -# argument and Getopt::Long doesn't accept regex for an option name. So this is what I did:

use Getopt::Long qw(:config pass_through);

my $ret=GetOptions(
    \%gops,
    'lines|l',  # lines/records to display
    ... cut ...
    '<>' => \&filearg,          # Handle file names also attach current options
);

I then defined the filearg() function:

sub filearg {
    my $arg=shift;

    # First see if it is a number as in -20 as shortcut for -l 20
        if ($arg =~ /^--?(\d)+$/) {
        $gops{'lines'}=$1;
    } elsif (-f "$arg" && -r "$arg") {
        my %ops=%gops;
        $fops{$arg}=\%ops;
        push(@files, $arg);
    } else {
        push(@badargs, $arg);
    }
    return(undef);
}

So what is needed is the pass_through option, a check for what you want and setting those things when seen. Above I had undefined options passed to my function. I use this for file checks and for a special option -# where # is some integer. If it doesn't match I add to a badargs array, as doing this will not cause GetOptions to fail, so I have to check this array after the return from GetOptions to see if errors were seen. You can also have it end on option error by ending the callback function with die("!FINISH"); which will cause GetOptions to terminate the script.

What I use it for is the ability to have something like -20 FILE1 -30 FILE2, so options can be overridden for subsequent files. I see you being able to do something similar with a check for the first part of the option name and then the value. So if all of your options start with --resource then look for something like this in your function: /^--?(resource\w+)=(.*)$/ and then add into option array.

Anyway, hope this helps.

#2

Would it work with the same option name repeated?

For example: script-name --resource=xxx --resource=xxx

#3

Another method to try would be to just use some sort of configuration file. It seems like this might be the easiest method to both write and parse, given that you plan on having a large amount of information.

推荐文章

如何使用lseek读取文件的最后一个字符

如何使用lseek读取文件的最后一个字符

推荐文章

使用jQuery将XML解析为GMaps API的数组

使用jQuery将XML解析为GMaps API的数组

推荐文章

如何在GIT中最好地组织多个项目?

如何在GIT中最好地组织多个项目?

推荐文章

UITableView清除颜色问题

UITableView清除颜色问题

推荐文章

在“选择框”下拉列表中设置“选定”值-基于第一个字母

在“选择框”下拉列表中设置“选定”值-基于第一个字母

推荐文章

尝试执行Javascript时,线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常

尝试执行Javascript时,线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常

推荐文章

如何在internet上创建机器间的局域网

如何在internet上创建机器间的局域网

推荐文章

IE window.resize如何停止调整大小?

IE window.resize如何停止调整大小?

推荐文章

SVG-调整位于某个角度的矩形的大小

SVG-调整位于某个角度的矩形的大小

推荐文章

将inkcanvas调整到左侧和顶部

将inkcanvas调整到左侧和顶部

推荐文章

停止fadeToggle()的累积

停止fadeToggle()的累积

推荐文章

如何在android中设置线性布局的页眉和页脚

如何在android中设置线性布局的页眉和页脚

推荐文章

Actionscript-心率监护仪和绘图API?

Actionscript-心率监护仪和绘图API?

推荐文章

我可以使用Windows XP进行Windows Azure开发吗

我可以使用Windows XP进行Windows Azure开发吗

推荐文章

在运行时编辑IronPython?

在运行时编辑IronPython?

推荐文章

iphone/Objective C-比较双精度不起作用

iphone/Objective C-比较双精度不起作用