getopt

How to call correctly getopt function

 ̄綄美尐妖づ 提交于 2019-12-04 13:22:17
Errors while calling int getopt function from http://code.google.com/p/darungrim/source/browse/trunk/ExtLib/XGetopt.cpp?r=17 `check.cpp: In function ‘int main()’:` check.cpp:14:55: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive] /usr/include/getopt.h:152:12: error: initializing argument 2 of ‘int getopt(int, char* const*, const char*)’ [-fpermissive] #include <iostream> #include <cstring> #include <string> #ifdef USE_UNISTD #include <unistd.h> #else #include "XGetopt.h" #endif using namespace std; int main() { string text="-f input.gmn -output.jpg"; int argc=text

In C++, how to use only long options with a required argument?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 07:02:21
In a C++ program, I would like to have a "long-only" option with a required argument. Below is my minimal example using getopt_long() , but it's not working: #include <getopt.h> #include <cstdlib> #include <iostream> using namespace std; void help (char ** argv) { cout << "`" << argv[0] << "` experiments with long options." << endl; } void parse_args (int argc, char ** argv, int & verbose, int & param) { int c = 0; while (1) { static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"verbose", required_argument, 0, 'v'}, {"param", required_argument, 0, 0} }; int option_index = 0

“for i” without “in [sequence]” ending while using getopt

匆匆过客 提交于 2019-12-04 05:17:53
I've found example script for using getopt command in shell. #!/bin/bash args=$(getopt ab $*) set -- $args for i; do case "$i" in -a)shift; echo "it was a";; -b)shift; echo "it was b";; esac; done It work well, but I don't understand where is variable $i assigned. How it knows that it must iterate through $arg. Can you explain this? As shown here , for defaults to $@ if no in seq is given. The for i assigns your $i variable. 来源: https://stackoverflow.com/questions/16102089/for-i-without-in-sequence-ending-while-using-getopt

Can OptionParser skip unknown options, to be processed later in a Ruby program?

房东的猫 提交于 2019-12-03 11:11:44
Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options? For example: $ myscript.rb --subsys1opt a --subsys2opt b Here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where 'a' is processed first, followed by 'b' in separate OptionParser object; each time picking options only relevant for that context. A final phase could check that there is nothing unknown left after each part processed theirs. The use cases are: In a loosely coupled front-end program, where various

【原创】memcached 中的命令行参数解析

妖精的绣舞 提交于 2019-12-03 04:40:40
本文主要是以 memcached 源码为例,讲解如何在 linux 下解析命令行参数。 安装 memcached 后,查看其可用选项: [root@Betty ~]# memcached -h memcached 1.4.14 -p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -s <file> UNIX socket path to listen on (disables network support) -a <mask> access mask for UNIX socket, in octal (default: 0700) -l <addr> interface to listen on (default: INADDR_ANY, all addresses) <addr> may be specified as host:port. If you don't specify a port number, the value you specified with -p or -U is used. You may specify multiple addresses separated

How to use getopt/OPTARG in Python? How to shift arguments if too many arguments (9) are given?

丶灬走出姿态 提交于 2019-12-03 03:39:59
问题 How to use getopt/optarg in Python? 回答1: This is an example of how I do it, I usually use the same basic template: import sys import getopt try: opts, args = getopt.getopt(sys.argv[1:], 'm:p:h', ['miner=', 'params=', 'help']) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit(2) elif opt in ('-m', '--miner'): miner_name = arg elif opt in ('-p', '--params'): params = arg else: usage() sys.exit(2) I don't think there is any 9

getopt fails to detect missing argument for option

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: 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 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 (represented by optarg) is returned as -b and parameterForB is

sys.exit(main(sys.argv[1:]))

匿名 (未验证) 提交于 2019-12-03 00:41:02
sys.argv sys.argv[]说白了就是一个从程序外部获取参数的桥梁。 首先我们需要import sys,sys是python3的一个标准库,也就是一个官方的模块。封装了一些系统的信息和接口,然后再说说argv这个变量。「argv」是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序。因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list),也就是说sys.argv其实可以看作是一个列表,所以才能用[]提取其中的元素。其第一个元素是程序本身,即sys.argv[0]是当前所执行的脚本,index 1以后的才是所传入的参数,用sys.argv[1:]可以获取到所有的参数,并且输出到一个列表里面。而切片获取的参数类型为字符串,即sys.argv传入的参数为字符串类型,如果想做一些条件判断的话需要转成所需要的数据类型。 main() "" Module docstring. This serves as a long usage message. """ import sys import getopt def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except

getopt-解析命令行参数

匿名 (未验证) 提交于 2019-12-03 00:14:01
getopt 模块用于解析 sys中的命令行参数。支持unix的 getopt() 函数的功能,并且提供了一个函数 getopt(args,options[,long_options]) 解析命令行参数,要去掉开头的运行程序引用。所以通常将args赋值为 sys.argv[1:] 。options参数可以跟一串字母,每个字母表示一个选项,含有后续值的选项后面加个冒号。 long_options,格式是字符串列表,选项名中不需要包含前导的"--"。长选项需要在参数后附带"=",然后跟一个值。如果只希望接受长选项,可以把options设置成空字符串。长选项会尽可能长的识别, 返回值两个元素:一个(option,value)列表,剩余的未解析参数。每个(option,value)对返回时,选项名包含前导的"-"或"--",选项参数作为第二个参数,或者为空字符串。它们在返回列表中出现的顺序与在命令行出现的顺序相同。长短选项混合。 更多技术资讯可关注:gzicast 来源:博客园 作者: 爱哭鼻子的小忧伤 链接:https://www.cnblogs.com/heimaguangzhou/p/11670725.html

python中命令行参数

匿名 (未验证) 提交于 2019-12-02 22:54:36
python中的命令行参数 python中有一个模块sys,sys.argv这个属性提供了对命令行参数的访问。命令行参数是调用某个程序时除程序名外的其他参数。 sys.argv是命令行参数的列表 len(sys.argv)是命令行参数的个数 下面我们用一个简单的例子来说明一下。 #!/usr/bin/python #coding:utf-8 import sys ##加载sys这个模块。 for i in range ( len ( sys . argv )): print "第%d个参数是:%s" % ( i , sys . argv [ i ]) print 运行上面的脚本: python argv.py 1 2 3 结果如下: 第0个参数是:argv.py 第1个参数是:1 第2个参数是:2 第3个参数是:3 从上面的脚本运行结果我们能看得出来第一个参数是脚本名本身也就是第0个参数。其余的一次类推。 有了这个sys.argv参数我们就可以向脚本传递一些我们想要用的参数。 另一个比较常用的模块os.path可以完成对路径的操作。它提供的函数可以完成管理和操作文件路径中的各个部分,获取文件或子目录信息,文件路径查询操作。 下面列出一些os模块中常用的函数: 函数名:                                  作用: os.mkfifo(‘path