getopt-long

Getopts to flag bad options without dash

☆樱花仙子☆ 提交于 2020-11-29 09:56:48
问题 Getopt::Long::Configure("no_pass_through"); my %opts = (); GetOptions(\%opts, 'opt1=s', 'opt2=s', 'opt3' ); test.pl bad_option_without_dash How do I make getopts flag an error when a bad option is passed without a dash? I was expecting that no_pass_through will take care of this. What am I missing? 回答1: Getopt::Long just extracts the options. It's up to you to validate the value of those options and the non-option arguments (which are left in @ARGV ). Specifically, if you want to make sure

Getopts to flag bad options without dash

|▌冷眼眸甩不掉的悲伤 提交于 2020-11-29 09:56:12
问题 Getopt::Long::Configure("no_pass_through"); my %opts = (); GetOptions(\%opts, 'opt1=s', 'opt2=s', 'opt3' ); test.pl bad_option_without_dash How do I make getopts flag an error when a bad option is passed without a dash? I was expecting that no_pass_through will take care of this. What am I missing? 回答1: Getopt::Long just extracts the options. It's up to you to validate the value of those options and the non-option arguments (which are left in @ARGV ). Specifically, if you want to make sure

Using getopt_long (C++) how do I code up a long & short option to both require arguments?

若如初见. 提交于 2020-01-12 08:36:29
问题 #include <iostream> #include <getopt.h> #define no_argument 0 #define required_argument 1 #define optional_argument 2 int main(int argc, char * argv[]) { std::cout << "Hello" << std::endl; const struct option longopts[] = { {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {"stuff", required_argument, 0, 's'}, {0,0,0,0}, }; int index; int iarg=0; //turn off getopt error message opterr=1; while(iarg != -1) { iarg = getopt_long(argc, argv, "svh", longopts, &index); switch (iarg)

Using getopt in C with non-option arguments

别等时光非礼了梦想. 提交于 2019-12-30 01:56:07
问题 I'm making a small program in C that deals with a lot of command line arguments, so I decided to use getopt to sort them for me. However, I want two non-option arguments (source and destination files) to be mandatory, so you have to have them as arguments while calling the program, even if there's no flags or other arguments. Here's a simplified version of what I have to handle the arguments with flags: while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) { switch (c) { case 'i': { i = (int

How can I allow undefined options when parsing args with Getopt

十年热恋 提交于 2019-12-30 00:53:11
问题 If I have a command line like: my_script.pl -foo -WHATEVER My script knows about --foo , and I want Getopt to set variable $opt_foo , but I don't know anything about -WHATEVER . How can I tell Getopt to parse out the options that I've told it about, and then get the rest of the arguments in a string variable or a list? An example: use strict; use warnings; use Getopt::Long; my $foo; GetOptions('foo' => \$foo); print 'remaining options: ', @ARGV; Then, issuing perl getopttest.pl -foo -WHATEVER

getopt_long doesnt handle my arguments right

那年仲夏 提交于 2019-12-25 03:56:05
问题 int next_option; int keep_content =0; int option_index = 0; const string short_options = "c::"; const struct option long_options[] = { {"config", optional_argument, NULL, 'c'}, {"keep", no_argument, &keep_content, 1}, { NULL,0, NULL, 0} }; while((next_option = getopt_long(argc,argv,short_options.c_str(),long_options,&option_index))!= -1) { cout << "name: " << long_options[option_index].name << " " << "value: " << optarg << endl; cout << "keep_content: " << keep_content << endl; } I have the

Getopt::Lazy does not print usage message or anything else

你说的曾经没有我的故事 提交于 2019-12-24 14:56:39
问题 I need some help using perls Getopt::Lazy module. I tried the example from the cpan page: #!/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

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,

Using getopt in C for command line arguments

僤鯓⒐⒋嵵緔 提交于 2019-12-12 06:03:32
问题 I am working on trying to take in command line arguments. If I want to have multiple optional command line arguments how would I go about doing that? For example you can run the program in the following ways: (a is required every instance but -b -c -d can be given optionally and in any order) ./myprogram -a ./myprogram -a -c -d ./myprogram -a -d -b I know that getopt()'s third argument is options. I can set these options to be "abc" but will the way I have my switch case set up causes the

GetOptions() in perl does not validate full argument names

吃可爱长大的小学妹 提交于 2019-12-12 04:47:13
问题 Suppose I want to enter 2 command line parameters - source and destination. GetOptions allows the command line by checking only the first character of the argument name instead of the full string. How do I validate for the full arguments strings instead of just allowing its substrings to be passed? Here's an example program: my ($source,$dest); GetOptions( 'from=s' => \$source, 'to=s' => \$dest ) or die "Incorrect arguments\n"; It accepts any of: -from -fro -fr -f -to -t However, I want it to