English 中文(简体)
我怎样才能掌握在Perl书写文字的指挥线论点/变量?
原标题:How can I manage command line arguments/variables for a script written in Perl?
  • 时间:2010-05-28 04:13:57
  •  标签:
  • perl

我试图处理用户在执行指挥时提出的许多论点。 至今,我一直在试图限制我的文字设计,以管理我很容易与Getopt打交道的旗帜:

GetOptions ("a" => $a, "b" => $b);

这样,我就可以检查一下,是否指定了一种或一种b,然后执行各自的法典/职能。

然而,现在我有这样的情况,即用户可以具体说明以下两个论点变量:

command -a black -b white

这是罚款,但我无法找到一种很好的方法来确定——a或——b是首先确定的。 因此,我不知道,在我执行过<条码>后,论点变量是否被分配到<条码>。

我怎么能说明与<代码>-a有关的哪些变量,以及上述例子中哪些变量与-b有关?

最佳回答

选择支持各种选择,例如:

GetOptions(  a=s  => $a,  b=s  => $b);
print "a is $a and b is $b";

a. 与贵问题印刷中的指挥线一致:

a is black and b is white

See manual page , forton more options. 这是一个非常强大的单元。

问题回答
my $a =   ; # option variable with default value (false)
my $b =   ; # option variable with default value (false)

GetOptions ( a  => $a,  b  => $b);

print "a is $a and b is $b

请通过Getopt:Long。 从这里看。

The call to GetOptions() parses the command line arguments that are present in @ARGV and sets the option variable to the value 1 if the option did occur on the command line. Otherwise, the option variable is not touched. Setting the option value to true is often called enabling the option.

The option name as specified to the GetOptions() function is called the option specification. Later we ll see that this specification can contain more than just the option name. The reference to the variable is called the option destination.

GetOptions() will return a true value if the command line could be processed successfully. Otherwise, it will write error messages to STDERR, and return a false result.

也可以帮助你们。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签