getopt-long

Bash long options/flags - how to do it?

こ雲淡風輕ζ 提交于 2019-12-10 19:29:36
问题 I am trying to change my working script with getopts to getopt ( long flags ). Below i present my code which is working. getopts 'm:' mode modeValue=$OPTARG getopts 'p:' parameter parameterValue=$OPTARG getopts 'u:' parameter parameterValue2=$OPTARG getopts 'l:' parameter parameterValue3=$OPTARG getopts 'n:' parameter parameterValue4=$OPTARG getopts 'e:' parameter parameterValue5=$OPTARG getopts 'w:' parameter parameterValue6=$OPTARG getopts 'r:' parameter parameterValue7=$OPTARG case

How do I use getopt_long to parse multiple arguments?

孤街醉人 提交于 2019-12-07 07:27:13
问题 #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, "s:vh", longopts, &index); switch (iarg)

How to pass both mandatory and optional command line arguments to perl script?

蓝咒 提交于 2019-12-06 12:13:07
问题 I am using Getopt::Long to pass options to my Perl script. But I want to do something like this : perl myScript mandatoryArgument1 -optionalArgument1=someValue I want the script to throw an error if mandatoryArgument1 is missing. How can this be done? 回答1: The good Getopt::Long does not have a mechanism for that. It specifically processes options. However, as it does its work it removes those options from @ARGV so once it's finished you can check whether the expected arguments are there. See

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

痞子三分冷 提交于 2019-12-04 12:47:32
#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) { case 'h': std::cout << "You hit help" << std::endl; break; case 'v': std::cout << "You hit version" <

Getopt::Long getting a string with spaces into a variable

坚强是说给别人听的谎言 提交于 2019-12-01 11:20:38
I'm making a perl script which uses Getopt::Long to parse command line arguments. However, I have an argument which can accept a string (with spaces). How can I get the whole string into a variable. For example: ./script.pl --string=blah blah blah blah yup --another-opt I need "blah blah blah blah yup" in variable $string. I know Getopt::Long supports multiple inputs for one argument when you know how many you will have (which I do not). Is this possible? You need to either put quotes around the argument: ./script.pl --string="blah blah blah blah yup" --another-opt or escape the spaces: .

Getopt::Long getting a string with spaces into a variable

余生长醉 提交于 2019-12-01 08:40:46
问题 I'm making a perl script which uses Getopt::Long to parse command line arguments. However, I have an argument which can accept a string (with spaces). How can I get the whole string into a variable. For example: ./script.pl --string=blah blah blah blah yup --another-opt I need "blah blah blah blah yup" in variable $string. I know Getopt::Long supports multiple inputs for one argument when you know how many you will have (which I do not). Is this possible? 回答1: You need to either put quotes

Using getopt in C with non-option arguments

天大地大妈咪最大 提交于 2019-11-30 06:52:53
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)atol(optarg); } case 'd': { d = (int)atol(optarg); } case 'b': buf = 1; break; case 't': time = 1;

How can I allow undefined options when parsing args with Getopt

佐手、 提交于 2019-11-30 04:47:27
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 gives Unknown option: whatever remaining options: You need to configure "pass_through" option via

How to support both short and long options at the same time in bash? [duplicate]

做~自己de王妃 提交于 2019-11-28 22:39:32
This question already has an answer here: Using getopts to process long and short command line options 32 answers I want to support both short and long options in bash scripts, so one can: $ foo -ax --long-key val -b -y SOME FILE NAMES is it possible? Brian Clements getopt supports long options. http://man7.org/linux/man-pages/man1/getopt.1.html Here is an example using your arguments: #!/bin/bash OPTS=`getopt -o axby -l long-key: -- "$@"` if [ $? != 0 ] then exit 1 fi eval set -- "$OPTS" while true ; do case "$1" in -a) echo "Got a"; shift;; -b) echo "Got b"; shift;; -x) echo "Got x"; shift;;

How to support both short and long options at the same time in bash? [duplicate]

我怕爱的太早我们不能终老 提交于 2019-11-27 14:25:28
问题 This question already has an answer here: Using getopts to process long and short command line options 32 answers I want to support both short and long options in bash scripts, so one can: $ foo -ax --long-key val -b -y SOME FILE NAMES is it possible? 回答1: getopt supports long options. http://man7.org/linux/man-pages/man1/getopt.1.html Here is an example using your arguments: #!/bin/bash OPTS=`getopt -o axby -l long-key: -- "$@"` if [ $? != 0 ] then exit 1 fi eval set -- "$OPTS" while true ;