getopt

How to call correctly getopt function

蹲街弑〆低调 提交于 2019-12-09 18:57:10
问题 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

What's the difference between argp and getopt?

柔情痞子 提交于 2019-12-08 16:05:08
问题 I think the title is self explanatory. I am making a program and I was wondering what I should use of the two and why. 回答1: argp may be more flexible / powerful / etc, but getopt is part of the POSIX standard. Thats a choice you've to make based on whether you expect your program to be portable. 回答2: From the Argp manual: Argp provides features unavailable in the more commonly used getopt interface. These features include automatically producing output in response to the ‘--help’ and ‘-

How to get advanced usage of getop() in php like python argparse

扶醉桌前 提交于 2019-12-08 03:39:46
问题 I commonly use argparse module in Python to detect options/parameters, print usage, etc. I am trying to get the same results in PHP from native code or using some lightweight library/framework without writing a lot of wrapping lines. From my research, I've just found getop()'s native PHP implementation, that is quite like getop()'s C implementation and very limited as a framework for certain uses (exclusive options, prefix options, parameter conflict handler, choices, metavars, etc). I would

Getopt shift optarg

北城余情 提交于 2019-12-07 06:38:57
问题 I need to call my program like this: ./program hello -r foo bar I take hello out of argv[1], but i am having trouble with value bar, also should i change "r:" to something else? while((c = getopt(argc, argv, "r:")) != -1){ switch(i){ ... case 'r': var_foo = optarg; //shell like argument shift here? var_bar = optarg; break; ...} I know I could do this with passing through argv, but is there a way to do it with getopt similar way as in bash? Thanks. 回答1: bar is not an option argument in the

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

getopt/getopts:Bash中命令行选项/参数处理

六月ゝ 毕业季﹏ 提交于 2019-12-06 08:31:31
0.引言 写程序的时候经常要处理命令行参数,本文描述在Bash下的命令行处理方式。 选项与参数: 如下一个命令行: ./test.sh -f config.conf -v --prefix=/home 我们称-f为选项,它需要一个参数,即config.conf, -v 也是一个选项,但它不需要参数。 --prefix我们称之为一个长选项,即选项本身多于一个字符,它也需要一个参数,用等号连接,当然等号不是必须的,/home可以直接写在--prefix后面,即--prefix/home,更多的限制后面具体会讲到。 在bash中,可以用以下三种方式来处理命令行参数,每种方式都有自己的应用场景。 * 手工处理方式 * getopts * getopt 下面我们依次讨论这三种处理方式。 1. 手工处理方式 在手工处理方式中,首先要知道几个变量,还是以上面的命令行为例: * $0 : ./test.sh,即命令本身,相当于C/C++中的argv[0] * $1 : -f,第一个参数. * $2 : config.conf * $3, $4 ... :类推。 * $# 参数的个数,不包括命令本身,上例中$#为4. * $@ :参数本身的列表,也不包括命令本身,如上例为 -f config.conf -v --prefix=/home * $* :和$@相同,但"$*" 和 "$@"(加引号

shell中getopt/getopts的使用

隐身守侯 提交于 2019-12-05 12:37:41
getopts配合case来进行操作时有两个隐含变量:一个是OPTARG,用来取当前选项的值,另外一个是OPTIND,代表当前选项在参数列表中的位移。OPTIND是一个特殊的变量,它的初始值是1,每次getopts处理完一个命令参数后就递增它,得到getopts要处理的下一个参数。 下面的例子可参考: >cattest4 #!/bin/bash while getopts "ab:cd:" Option # b and d take arguments # do case $Option in a) echo -e "a = $OPTIND";; b) echo -e "b = $OPTIND $OPTARG";; c) echo -e "c = $OPTIND";; d) echo -e "d = $OPTIND $OPTARG";; esac done shift $(($OPTIND - 1)) >sh test4 -a -b foo -cd bar a = 2 b = 4 foo c = 4 d = 6 bar >sh test4 -ab foo a = 1 b = 3 foo >sh test4 -a -c a = 2 c = 3 getopts(Shell内置命令) 处理命令行参数是一个相似而又复杂的事情,为此,C提供了getopt/getopt_long等函数,C+

Getopt shift optarg

依然范特西╮ 提交于 2019-12-05 10:35:36
I need to call my program like this: ./program hello -r foo bar I take hello out of argv[1], but i am having trouble with value bar, also should i change "r:" to something else? while((c = getopt(argc, argv, "r:")) != -1){ switch(i){ ... case 'r': var_foo = optarg; //shell like argument shift here? var_bar = optarg; break; ...} I know I could do this with passing through argv, but is there a way to do it with getopt similar way as in bash? Thanks. Kerrek SB bar is not an option argument in the eyes of getopt . Rather, GNU getopt rearranges the positional arguments so that at the end of the

Python3之命令行参数处理

核能气质少年 提交于 2019-12-05 09:47:49
sys模块 常用单元 getopt模块 optparse模块 argparse模块 toc sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import sys ,再输入 dir(sys) 查看sys模块所包含的全部程序单元(包括变量、函数等),或者 [i for i in dir(sys) if not i.startswith('_')] 过滤掉隐藏的单元 常用单元 sys.argv :以列表的方式获取运行 Python 程序的命令行参数存放其中。其中 sys.argv[0] 通常就是指该 Python程序本身, sys.argv[1] 代表第一个参数, sys.argv[2] 代表第二个参数,以此类推。类似于 Shell 中的 $0、$1、$2 import sys print('程序名称为:{},第一个参数为:{},第二个参数为:{}'.format(sys.argv[0], sys.argv[1], sys.argv[2])) sys.byteorder :显示本地字节序的指示符。如果本地字节序的大端模式,则该属性返回big,否则返回little sys.copyright :该属性返回与 Python 解释器有关的版权信息 sys

Can Python's argparse permute argument order like gnu getopt?

风流意气都作罢 提交于 2019-12-05 02:03:43
GNU getopt, and command line tools that use it, allow options and arguments to be interleaved, known as permuting options (see http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt ). Perl's Getopt::Long module also supports this (with qw(:config gnu_getopt)). argparse seems to not support (or even mention) permuting options. There are many SO questions related to arg/opt order, but none seem answer this question: Can argparse be made to permute argument order like getopt? The use case is a prototypical command line signature like GNU sort: sort [opts] [files] in