我有一个脚本,它从CLI中获取3个输入变量并分别将它插入3个变量:
GetOptions("old_path=s" => \$old_path,
"var=s" => \$var,
"new_path=s" => \$new_path,
"log_path=s" => \$log_path)
or die ("Error in input variables\n");e
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以添加子程序或任何其他GetOptions参数来解释如何给出输入变量.如果有任何错误,请纠正我.谢谢.
我只想使用getopt从带有长选项的命令行参数获取输入
例: ./script --empid 123
options, args = getopt.getopt(sys.argv[1:],['empid='])
for opt, arg in options:
print 'opts',opt
if opt in ('--empid'):
emp_Id = arg
Run Code Online (Sandbox Code Playgroud)
我收到getopt.GetoptError: option --empid not recognised以上代码的错误错误。可能出了什么问题?
我正在使用GetOpt :: long模块从命令行获取参数并将其分配给相应的变量.但是我在打印时遇到错误.代码和错误如下:
#!usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
GetOptions(
"mount_path=s" => \my $old_path,
"var=s" => \my $var,
"log_path=s" => \my $log_path,
) or die "Error in input variables\n";
print <<"END_INPUTS";
These are your inputs:
old_path= $old_path
var = $var
log_path=$log_path
Press enter twice if all looks GOOD
*********************************************************
END_INPUTS
Run Code Online (Sandbox Code Playgroud)
命令行参数如下:
perl getvar.pl --mount_path=/var/sslvpn --var=7.0.0.2_va-SSLVPN-!7.0.0.2.16sv+.jpn-05!j+g_554863- --log_path=log.txt
Run Code Online (Sandbox Code Playgroud)
我在运行时遇到以下错误
-bash: !7: event not found
Run Code Online (Sandbox Code Playgroud)
请有人帮我这个.谢谢.
我想限制用户输入(通过Getopt::Long)可能的三个值中的一个。值为“pc-number”、“ip-address”和“surname”。
当有两个值时,我正在执行以下操作:
if ((!$pc_number and !$address) or ($pc_number and $address)) {
pod2usage("You must supply pc_number OR ip_address.");
exit;
} elsif ($pc_number) {
(do stuff)
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能简单地确保用户只设置了三个变量中的一个?
我正在尝试解析脚本中的命令行选项和值。该脚本接受 2 个选项:updategroup或validategroup. 该updategroup选项应接受 2 个值。例如:
./script.pl -updategroup 'group1' 'enable'
这就是我的称呼GetOptions:
GetOptions(\%args,"updategroup=s{2}","validategroup=s");
Run Code Online (Sandbox Code Playgroud)
调用它后,我想将 2 个值存储在一个数组中。如何从%args哈希变量中获取这个值?
我有以下代码:
struct option longopts[] =
{
{"version", no_argument, 0, 'v'}
};
if (argc > 1)
{
int c;
int longindex;
while ((c = getopt_long (argc, argv, "v", longopts, &longindex)) != -1)
{
switch (c)
{
case 'v':
puts (VERSION_INFO);
exit (0);
case '?':
exit (1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么带有--versio(故意拼写错误)的参数会导致段错误但是-a(这也是一个无效选项)只是在屏幕上打印"无效选项"消息?
c getopt getopt-long segmentation-fault command-line-arguments
我需要一些使用perls Getopt :: Lazy模块的帮助.我尝试了cpan页面中的示例:
#!/usr/bin/perl
#
#use warnings;
#use strict;
use Getopt::Lazy
'help|h' => 'Show this help screen',
'verbose|v' => 'Show verbose output',
'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
-summary => 'a simple example usage of Getopt::Lazy',
-usage => '%c %o file1 [file2 ..]',
;
getopt;
print usage and exit 1 unless @ARGV;
Run Code Online (Sandbox Code Playgroud)
当我把它放在一个文件中执行它时./mygetopt.pl -h,我希望打印出帮助信息,但没有任何反应.当我没有-h参数调用它时,我希望它打印用法消息.没有这样的事情发生.此外,当我使用严格和警告时,我收到消息
Unquoted string "usage" may clash with future reserved word at …Run Code Online (Sandbox Code Playgroud) 我正在尝试编辑Perl程序以使用Get Options和Pod Usage模块.当我试图这样做时,它似乎打破了它.第一个代码示例是有效的原始文件,第二个代码示例是不起作用的已编辑版本.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
use File::Basename;
use FindBin;
use lib "$FindBin::Bin/../../lib";
use TNT::Utils::Crypto;
use TNT::Utils::DB;
$|=1 if _running_interactively(); # autoflush STDOUT for better status feedback
my $survey = shift or die "Must provide survey name";
my $db_type = shift or die "Must provide database type (mysql|prod|sqlite|test)";
my $mode = shift or die "Must provide mode 'NORMAL' or 'ROLLOVER'";
my @files = (shift) or die "Must provide file names to load or 'FAKE' for …Run Code Online (Sandbox Code Playgroud) 我有一个程序getopt_get()用来解析命令行参数。我的代码是这样的:
int opt;
int optionIndex;
static struct option longOptions[] = {
{"help", no_argument, NULL, 'h'},
{"test", no_argument, NULL, 't'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "ht", longOptions, &optionIndex)) != -1) {
switch (opt) {
case 'h':
help();
return 0;
break;
case 't':
init();
test();
return 0;
break;
case '?':
help();
return 1;
break;
default:
printf("default.\n");
}
Run Code Online (Sandbox Code Playgroud)
当我将正确的命令行参数传递给程序时,它运行良好。但是当错误的参数传递给程序时,它会打印出像这样烦人和多余的词。
例如,我将错误的参数“q”传递给程序
$ ./program -q
./program: invalid option -- 'q'
Usage: -h -t
Run Code Online (Sandbox Code Playgroud)
当有错误的参数时,我只希望它运行我的函数help()而不打印任何单词。
./program: invalid …Run Code Online (Sandbox Code Playgroud) I have a negatable option defined as top!.
When a command uses this option, say : command_name -top, prints a warning to the user
Code snippet for it is as follows :
if ($args{top}) { print "Warning"; }
Run Code Online (Sandbox Code Playgroud)
但是,当命令使用否定选项时,例如:command_name -notop,它不会向用户打印相同的警告。
有没有办法在这两种情况下获得相同的警告?