getopt

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 get a value from optarg

ぃ、小莉子 提交于 2019-11-30 02:27:34
Hi I am writing a simple client-server program. In this program I have to use getopt() to get the port number and ip address like this: server -i 127.0.0.1 -p 10001 I do not know how can I get values from optarg, to use later in the program. Kornel Kisielewicz How about like this: char buf[BUFSIZE+1]; snprintf(buf,BUFSIZE,"%s",optarg); Or in a more complete example: #include <stdio.h> #include <unistd.h> #define BUFSIZE 16 int main( int argc, char **argv ) { char c; char port[BUFSIZE+1]; char addr[BUFSIZE+1]; while(( c = getopt( argc, argv, "i:p:" )) != -1 ) switch ( c ) { case 'i': snprintf(

Is there a package to process command line options in R?

回眸只為那壹抹淺笑 提交于 2019-11-29 21:45:51
问题 Is there a package to process command-line options in R? I know commandArgs , but it's too basic. Its result is basically the equivalent to argc and argv in C , but I'd need something on top of that, just like boost::program_options in C++ , or GetOptions::Long in perl . In particular, I'd like to specify in advance what options are allowed and give an error message if the user specifies something else. The call would be like this (with user options --width=32 --file=foo.txt): R --vanilla -

Cross-platform getopt for a shell script

匆匆过客 提交于 2019-11-29 02:51:15
问题 I've just found out that getopt is not cross-platform (in particular for FreeBSD and Linux). What is the best workaround for this issue? 回答1: Use getopts (with an "s"). According to Bash FAQ 35: Unless it's the version from util-linux, and you use its advanced mode, never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed. The POSIX shell (and others) offer getopts which is safe to use instead. 回答2: There are

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 take integers as command line arguments?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 06:53:52
I've read a getopt() example but it doesn't show how to accept integers as argument options, like cvalue would be in the code from the example: #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main (int argc, char **argv) { int aflag = 0; int bflag = 0; char *cvalue = NULL; int index; int c; opterr = 0; while ((c = getopt (argc, argv, "abc:")) != -1) switch (c) { case 'a': aflag = 1; break; case 'b': bflag = 1; break; case 'c': cvalue = optarg; break; case '?': if (optopt == 'c') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint

GetOpt library for C#

别等时光非礼了梦想. 提交于 2019-11-28 05:20:19
I'm looking for a getopt library for c#. So far I found a few ( phpguru , XGetOptCS , getoptfordotnet ) but these look more like unfinished attempts that only support a part of C's getopt. Is there a full getopt c# implementation? Here is a .NET Implementation of getopt: http://www.codeplex.com/getopt Miguel de Icaza raves about Mono.Options . You can use the nuget package , or just copy the single C# source file into your project. For posterity: CommandParser is another one with a BSD license leppie Here is something I wrote, it works rather nice, and has quite a lot of features for the tiny

getopt fails to detect missing argument for option

不羁的心 提交于 2019-11-27 21:19:54
I have a program which takes various command line arguments. For the sake of simplification, we will say it takes 3 flags, -a , -b , and -c , and use the following code to parse my arguments: int c; while((c = getopt(argc, argv, ":a:b:c")) != EOF) { switch (c) { case 'a': cout << optarg << endl; break; case 'b': cout << optarg << endl; break; case ':': cerr << "Missing option." << endl; exit(1); break; } } note: a, and b take parameters after the flag. But I run into an issue if I invoke my program say with ./myprog -a -b parameterForB where I forgot parameterForA, the parameterForA

Linux 命令行参数解析

与世无争的帅哥 提交于 2019-11-27 15:37:09
在linux中,经常需要各种命令,通常情况下都会带各种参数,而这些参数是如何解析的呢? 通常使用GNU C提供的函数getopt、getopt_long、getopt_long_only函数来解析命令行参数。 使用他们需要引用头文件getopt.h。 原文地址: https://www.cnblogs.com/NickQ/p/11368656.html 1. getopt()函数 getopt()用来解析命令行选项参数的,但是只能解析短选项: -d 100,不能解析长选项:--prefix;其原型: int getopt(int argc, char * const argv[],const char *optstring); argc和argv和main函数的两个参数一致,分别表示命令行参数个数和保存参数的字符串数组; optstring,是短选项的规则声明,形式如 "a:b::cd:" ,分别表示程序支持的命令行短选项有-a、-b、-c、-d,冒号含义如下: 只有一个字符,不带冒号——只表示选项, 如-c 2. 一个字符,后接一个冒号——表示选项后面带一个参数,如-a 100 或 -a100 3. 一个字符,后接两个冒号——表示选项后面带一个可选参数,即参数可有可无,如果带参数,则选项与参数直接不能有空格形式,应该型如-b200 这里就能解释为什么有时候带参数需要空格

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 ;