我是perl的菜鸟,所以请尽量耐心等待我的这个问题.
看来如果我多次调用perl Getopts :: Long :: GetOpts方法,第二次调用就完全被忽略了.
这是正常的吗?(为什么)
这个过程有哪些替代方案?
(实际上我已经编写了一个模块,我在其中进行GetOpts调用,使用我的模块的脚本也尝试这样做,但似乎脚本没有获得所需的选项)
谢谢,Neeraj
我的一位同事编写了一个perl脚本,询问用户的Windows域名/用户名,当然我们输入以下格式domainname\username.Getopt:Long模块然后将其转换为字符串,删除'\'字符并使字符串不正确.当然,我们可以要求所有用户输入他们的域/用户组合,domainname\\username但我真的不想有罪"修复用户,而不是程序".我们也使用了我们为此制作的模块,我会调用OurCompany::ColdFusionAPI它,因为它访问ColdFusion.
我们的代码如下所示:
#!/usr/bin/perl
use common::sense;
use Getopt::Long;
use OurCompany::ColdFusionAPI;
my ($server_ip, $username, $password, $need_help);
GetOptions (
"ip|server-address=s" => \$server_ip,
"user-name=s" => \$username,
"password=s" => \$password,
"h|help" => \$need_help,
);
$username ||= shift;
$password ||= shift;
$server_ip ||= shift;
if (!$server_ip or $need_help){
print_help();
exit 0;
}
my $print_hash = sub { my $a = shift; say "$_\t=> $a->{$_}" foreach keys %$a; };
...
Run Code Online (Sandbox Code Playgroud)
如果我添加该行,say $username那么它只给出没有'\'的字符串.如何让perl保持'\'?read -r在bash中的某些东西.
我有一个Perl脚本,它在顶部附近为它将使用的目录和文件设置变量.它还需要将一些变量设置为命令行参数.例:
use Getopt::Long;
my ($mount_point, $sub_dir, $database_name, $database_schema);
# Populate variables from the command line:
GetOptions(
'mount_point=s' => \$mount_point,
'sub_dir=s' => \$sub_dir,
'database_name=s' => \$database_name,
'database_schema=s' => \$database_schema
);
# ... validation of required arguments here
################################################################################
# Directory variables
################################################################################
my $input_directory = "/${mount_point}/${sub_dir}/input";
my $output_directory = "/${mount_point}/${sub_dir}/output";
my $log_directory = "/${mount_point}/${sub_dir}/log";
my $database_directory = "/db/${database_name}";
my $database_scripts = "${database_directory}/scripts";
################################################################################
# File variables
################################################################################
my $input_file = "${input_dir}/input_file.dat";
my $output_file = "${output_dir}/output_file.dat";
# ... etc
Run Code Online (Sandbox Code Playgroud)
这在我的开发,测试和生产环境中工作正常.但是,我试图更容易地覆盖某些变量(无需进入调试器)进行开发和测试.(例如,如果我想设置我的input_file ="/ tmp/my_input_file.dat").我的想法是使用GetOptions函数来处理这个,如下所示: …
从我正在编写的程序中解析参数时遇到问题,代码如下:
void parse_args(int argc, char** argv)
{
char ch;
int index = 0;
struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "stop", no_argument, NULL, 's' },
{ 0, 0, 0, 0 }
};
while ((ch = getopt_long(argc, argv, "hp:s", options, &index)) != -1) {
switch (ch) {
case 'h':
printf("Option h, or --help.\n");
break;
case 's':
printf("Option s, or --stop.\n");
break;
case 'p':
printf("Option p, or --port.\n");
if (optarg != …Run Code Online (Sandbox Code Playgroud) 可能的重复:
getopt_long() — 正确的使用方法?
我在 C 程序中遇到了 getopt_long 问题。代码:
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "num", 1, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};
do {
next_option = getopt_long(argc, argv, short_options,
long_options, NULL);
switch(next_option) {
case 'h':
print_usage(stdout, 0);
case 'n':
printf("num %s\n", optarg);
break;
case '?':
print_usage(stderr, 1);
break;
default:
abort();
}
} while(next_option != -1);
Run Code Online (Sandbox Code Playgroud)
这有效:
./a.out --num 3
num 3
Run Code Online (Sandbox Code Playgroud)
这有效(为什么?!):
./a.out --n 3
num 3
Run Code Online (Sandbox Code Playgroud)
这不会: …
我有以下代码
my $use = "Use: " . basename($0) . " [options]";
my $version = "Version: 0.1 \n";
my $variableA;
my $variableB;
GetOptions(
'a=s' => \$variableA,
'help' => sub { print $use; exit 0 },
'version' => sub { print $version; exit 0 },
'b' => sub { \$variableB, &this_subroutine; goto NOWGOHERE; },
);
die "Incorrect use. \n" unless (defined $variableA || defined $variableB);
sub this_subroutine {
print "$variableB\n";
}
NOWGOHERE: print "HELLO I'M NOW HERE\n";
Run Code Online (Sandbox Code Playgroud)
我想要做的是设置$variableB,然后做&this_subroutine和, …
我正在尝试接受命令行参数。如果我想有多个可选的命令行参数,我将如何去做?例如,您可以通过以下方式运行该程序:(每个实例都需要 a,但 -b -c -d 可以选择以任何顺序提供)
./myprogram -a
./myprogram -a -c -d
./myprogram -a -d -b
Run Code Online (Sandbox Code Playgroud)
我知道 getopt() 的第三个参数是选项。我可以将这些选项设置为“abc”,但是我设置 switch case 的方式会导致循环在每个选项处中断。
我想要一个选项,其中第一个值是必需的,第二个值是可选的.
例如,
./foo --arg mandatory optional
Run Code Online (Sandbox Code Playgroud)
如果我使用=s{2}该用户被迫进入第二个选项.
我不想允许n个值...我想强制只允许两个值,第二个值是可选的.
这是一个支持的功能GetOptions吗?
假设我要输入2个命令行参数 - 源和目标.GetOptions通过仅检查参数名称的第一个字符而不是完整字符串来允许命令行.如何验证完整的参数字符串而不是仅允许其子字符串传递?
这是一个示例程序:
my ($source,$dest);
GetOptions(
'from=s' => \$source,
'to=s' => \$dest
) or die "Incorrect arguments\n";
Run Code Online (Sandbox Code Playgroud)
它接受以下任何一项:
-from-fro-fr-f
-to
-t但是,我希望它只接受
-from-to如果除了那些完整的词之外的任何东西都通过了
我如何禁止缩写选项?
我想创建一个接受表单参数的C程序
-Ak
其中K是0-9的整数.
我将如何解析/指定此选项?