我正在尝试在C++程序中使用getopt来解析命令行参数.参数是-d xxx,-s xxx和 - ?.我无法捕获 - ?参数,我想打印标准用法消息.
while ((c = getopt (argc, argv, "?d:s:")) != -1) {
switch (c) {
case 'd':
...do stuff
break;
case 's':
... do stuff
break;
case '?':
// From example on GNU page, seems to capture -d, -s when no args provided.
// Gets here when -d or -s provided, but no arguments for these options.
default:
// shut down
}
Run Code Online (Sandbox Code Playgroud)
尽我所能,我似乎无法抓住' - ?' 选项本身.是否有一个特殊的技巧来捕捉'?' 在其自己的?我是否为getopt提供了正确的模式(即'?d:s:')目前,c被设置为'?' 无论何时提供无效选项,即使'?' 在命令行中未提供.
多谢你们.
可能的重复:
getopt_long() — 正确的使用方法?
我在 C 程序中遇到了 getopt_long 问题。代码:
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "num", 1, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};
do {
next_option = getopt_long(argc, argv, short_options,
long_options, NULL);
switch(next_option) {
case 'h':
print_usage(stdout, 0);
case 'n':
printf("num %s\n", optarg);
break;
case '?':
print_usage(stderr, 1);
break;
default:
abort();
}
} while(next_option != -1);
Run Code Online (Sandbox Code Playgroud)
这有效:
./a.out --num 3
num 3
Run Code Online (Sandbox Code Playgroud)
这有效(为什么?!):
./a.out --n 3
num 3
Run Code Online (Sandbox Code Playgroud)
这不会: …
我写了(从谷歌复制并粘贴并简化)一个C程序,使用getopt打印出从Unix命令行传入的参数值.
从Unix命令行:
./myprog -a 0 -b 1 -c 2
Run Code Online (Sandbox Code Playgroud)
我的C代码是:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int i;
while ((i = getopt(argc, argv, "abc")) != -1) {
switch (i) {
case 'a':
printf("A = %s\n", optarg);
break;
case 'b':
printf("B = %s\n", optarg);
break;
case 'c':
printf("C = %s\n", optarg);
break;
default:
break;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想编程打印出传递的每个值,例如
A = 0
B = 1
C = 2
Run Code Online (Sandbox Code Playgroud)
但它根本不打印任何东西.
我正在尝试接受命令行参数。如果我想有多个可选的命令行参数,我将如何去做?例如,您可以通过以下方式运行该程序:(每个实例都需要 a,但 -b -c -d 可以选择以任何顺序提供)
./myprogram -a
./myprogram -a -c -d
./myprogram -a -d -b
Run Code Online (Sandbox Code Playgroud)
我知道 getopt() 的第三个参数是选项。我可以将这些选项设置为“abc”,但是我设置 switch case 的方式会导致循环在每个选项处中断。
我是Perl的新手.我正在尝试理解该Getopt::Long模块,我在网上阅读了材料以及书籍.因此,这个问题对我来说没有什么是明确的.
我将以下列格式调用命令行
script -m file
Run Code Online (Sandbox Code Playgroud)
要么
script -v hostname -m file
Run Code Online (Sandbox Code Playgroud)
我怎么能用Getopt::Std或完成这个Getopt::Long?
我修改了此处给出的示例代码: getopt 的示例代码
如下,但它不起作用。我不确定我错过了什么。我在现有代码中添加了“-j”选项。最终,我想添加尽可能多的命令选项以满足我的需求。
当我提供如下输入时,它不会打印任何内容。
./pyopts.py -i dfdf -j qwqwqw -o ddfdf
Input file is "
J file is "
Output file is "
Run Code Online (Sandbox Code Playgroud)
你能告诉我这里有什么问题吗?
#!/usr/bin/python
import sys, getopt
def usage():
print 'test.py -i <inputfile> -j <jfile> -o <outputfile>'
def main(argv):
inputfile = ''
jfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hij:o:",["ifile=","jfile=","ofile="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt …Run Code Online (Sandbox Code Playgroud) 我想创建一个接受表单参数的C程序
-Ak
其中K是0-9的整数.
我将如何解析/指定此选项?
当我发送标志-h或-H时,为什么会出现分段错误.
bool parse_command(int argc, char **argv, bool *header, char **fileName)
{
if (!argv)
return false;
bool *flagh = false;
bool *flagH = false;
char *options = "Hh";
int opt = 0;
while ( (opt = getopt(argc, argv, options ) ) != -1)
{
printf("HELLO");
switch (opt)
{
case 'h':
*flagh = true;
break;
case 'H':
*flagH = true;
break;
default:
usage_p1();
return false;
}
}
printf("%d", opt);
// Implement this function
return true;
}
Run Code Online (Sandbox Code Playgroud)