getopt

Processing multiple values for one single option using getopt/optparse?

南楼画角 提交于 2019-11-27 13:33:22
问题 Is it possible to fetch multiple values for one option using getopt or optparse, as shown in the example below: ./hello_world -c arg1 arg2 arg3 -b arg4 arg5 arg6 arg7 Please note that the number of actual values for each option (-c, -b) could be either 1 or 100. I do not want to use: ./hello_world -c "arg1 arg2 arg3" -b "arg4 arg5 arg6 arg7" It seems to me that this may not be possible (and perhaps in violation of POSIX), please correct me if I'm wrong. I've seen examples where all the non

getopt does not parse optional arguments to parameters

纵然是瞬间 提交于 2019-11-27 07:12:16
In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./respond --praise John Kudos to John $ ./respond --blame John You suck ! $ ./respond --blame You suck ! Here is the test code. #include <stdio.h> #include <getopt.h> int main(int argc, char ** argv ) { int getopt_ret, option_index; static struct option long_options[] = { {"praise", required_argument, 0, 'p'}, {"blame", optional_argument, 0, 'b'}, {0, 0, 0, 0} }; while (1) { getopt_ret = getopt_long( argc,

GetOpt library for C#

浪尽此生 提交于 2019-11-27 05:34:08
问题 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? 回答1: Here is a .NET Implementation of getopt: http://www.codeplex.com/getopt 回答2: Miguel de Icaza raves about Mono.Options. You can use the nuget package, or just copy the single C# source file into your project. 回答3: For posterity: CommandParser is another one with a

C getopt multiple value

梦想的初衷 提交于 2019-11-27 04:05:23
My argument is like this ./a.out -i file1 file2 file3 How can I utilize getopt() to get 3 (or more) input files? I'm doing something like this: while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){ case 'i': input = optarg; break; ... } I get just the file1 ; how to get file2 , file3 ? If you must, you could start at argv[optind] and increment optind yourself. However, I would recommend against this since I consider that syntax to be poor form. (How would you know when you've reached the end of the list? What if someone has a file named with a - as the first character?) I think that it would be

getopt.h: Compiling Linux C-Code in Windows

三世轮回 提交于 2019-11-27 03:57:32
I am trying to get a set of nine *.c files (and nine related *.h files) to compile under Windows. The code was originally designed in Linux to take command line arguments using the standard GNU-Linux/C library "getopt.h". And that library does not apply to building the C-code in Windows. I want to ignore what my code does right now and ask the following question. For those of you familiar with this C-library "getopt.h": will it be possible to build and run my code in Windows if it depends on POSIX-style command-line arguments? Or will I have to re-write the code to work for Windows, passing

how to take integers as command line arguments?

自古美人都是妖i 提交于 2019-11-27 01:34:54
问题 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 '?'

getopt fails to detect missing argument for option

我的未来我决定 提交于 2019-11-26 23:03:56
问题 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

getopt does not parse optional arguments to parameters

落爺英雄遲暮 提交于 2019-11-26 17:36:58
问题 In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./respond --praise John Kudos to John $ ./respond --blame John You suck ! $ ./respond --blame You suck ! Here is the test code. #include <stdio.h> #include <getopt.h> int main(int argc, char ** argv ) { int getopt_ret, option_index; static struct option long_options[] = { {"praise", required_argument, 0, 'p

Why use argparse rather than optparse?

南笙酒味 提交于 2019-11-26 15:37:27
I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse . Why has yet another command-line parsing module been created? Why should I use it instead of optparse ? Are there new features that I should know about? Nicholas Knight As of python 2.7 , optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page ( https://code.google.com/archive/p/argparse/ ): handling positional arguments supporting sub-commands allowing alternative

python接收命令行参数

僤鯓⒐⒋嵵緔 提交于 2019-11-26 14:02:20
Python 提供了 getopt 模块来获取命令行参数。 $ python test.py arg1 arg2 arg3 Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表。 len(sys.argv) 是命令行参数个数。 注:sys.argv[0] 表示脚本名。 getopt模块 getopt模块是专门处理命令行参数的模块,用于获取命令行选项和参数,也就是sys.argv。命令行选项使得程序的参数更加灵活。支持短选项模式(-)和长选项模式(–)。 该模块提供了两个方法及一个异常处理来解析命令行参数。 getopt.getopt 方法 getopt.getopt 方法用于解析命令行参数列表,语法格式如下: getopt.getopt(args, options[, long_options]) 方法参数说明: args: 要解析的命令行参数列表。 options: 以列表的格式定义,options后的冒号(:)表示该选项必须有附加的参数,不带冒号表示该选项不附加参数。 long_options: 以字符串的格式定义,long_options 后的等号(=)表示如果设置该选项,必须有附加的参数,否则就不附加参数。 该方法返回值由两个元素组成: 第一个是 (option, value) 元组的列表。 第二个是参数列表