标签: getopt

C,如何在getopt函数中指定optstring?

我不确定如何正确使用C optstring中的getopt函数.

应该如何格式化该字符串?我看到了字母彼此相邻的例子,有时用分号分隔,有时用两个分号分隔.

这是什么意思?

c getopt

21
推荐指数
2
解决办法
2万
查看次数

在使用Getopt解析args时,如何允许未定义的选项

如果我有一个命令行,如:

my_script.pl -foo -WHATEVER
Run Code Online (Sandbox Code Playgroud)

我的脚本知道--foo,我希望Getopt设置变量$opt_foo,但我不知道任何事情-WHATEVER.我如何告诉Getopt解析我告诉它的选项,然后在字符串变量或列表中获取其余参数?

一个例子:

use strict;
use warnings;

use Getopt::Long;

my $foo; 

GetOptions('foo' => \$foo); 

print 'remaining options: ', @ARGV;
Run Code Online (Sandbox Code Playgroud)

然后,发行

perl getopttest.pl -foo -WHATEVER

Unknown option: whatever
remaining options:

perl getopt getopt-long

19
推荐指数
1
解决办法
1万
查看次数

C getopt多值

我的论点是这样的

./a.out -i file1 file2 file3
Run Code Online (Sandbox Code Playgroud)

如何利用getopt()3个(或更多)输入文件?我正在做这样的事情:

while ((opt = getopt(argc, argv, "i:xyz.."))!= -1){
  case 'i':
     input = optarg; 
     break;
  ...
}
Run Code Online (Sandbox Code Playgroud)

我得到了file1; 怎么弄file2,file3

c input getopt

19
推荐指数
4
解决办法
3万
查看次数

如何将整数作为命令行参数?

我已经阅读了一个getopt()示例,但它没有显示如何接受整数作为参数选项,就像cvalue在示例的代码中一样:

 #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 …
Run Code Online (Sandbox Code Playgroud)

c getopt command-line-arguments

19
推荐指数
4
解决办法
8万
查看次数

getopt无法检测选项的缺失参数

我有一个程序,它采用各种命令行参数.为了简化起见,我们会说,它需要3个标志,-a,-b,和-c,并使用下面的代码来分析我的论点:

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

注意:a,和b后面的参数标志.

但是,如果我调用我的程序,我会遇到一个问题

./myprog -a -b parameterForB
Run Code Online (Sandbox Code Playgroud)

我忘记了parameterForA,返回-b参数ForA(由optarg表示),而参数FORB 被认为是没有参数的选项,而optind被设置为argv中parameterForB的索引.

在这种情况下,所需的行为':'是在没有找到参数后返回-a,并Missing option.打印到标准错误.但是,这仅发生在-a传递给程序的最后一个参数的事件中.

我想问题是:有getopt()没有办法假设没有选项可以开始-

c c++ getopt

17
推荐指数
2
解决办法
6万
查看次数

如何使用getopt在UNIX中创建多字符参数?

我正在尝试创建一个getopt命令,这样当我将"-ab"参数传递给脚本时,该脚本会将-ab视为单个参数.

#!/bin/sh
args=`getopt "ab":fc:d $*`
set -- $args
for i in $args
do
case "$i" in
        -ab) shift;echo "You typed ab $1.";shift;;
        -c) shift;echo "You typed a c $1";shift;;
esac
done
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用.有人可以提供任何帮助吗?

unix getopt

17
推荐指数
2
解决办法
2万
查看次数

Getopt不包括在内?隐式声明函数'getopt'

我想使用getopt,但它不会起作用.

它给了我

gcc -g -Wall -std=c99 -ftrapv -O2 -Werror -Wshadow -Wundef -save-temps -Werror-implicit-function-declaration   -c -o src/main.o src/main.c
src/main.c: In function ‘main’:
src/main.c:13:2: error: implicit declaration of function ‘getopt’ [-Werror=implicit-function-declaration]
src/main.c:23:14: error: ‘optarg’ undeclared (first use in this function)
src/main.c:23:14: note: each undeclared identifier is reported only once for each function it appears in
src/main.c:26:9: error: ‘optopt’ undeclared (first use in this function)
src/main.c:28:5: error: implicit declaration of function ‘isprint’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: implicit declaration of function ‘abort’ [-Werror=implicit-function-declaration]
src/main.c:36:5: error: …
Run Code Online (Sandbox Code Playgroud)

c linux getopt

15
推荐指数
1
解决办法
2万
查看次数

在C中使用带有非选项参数的getopt

我正在C中创建一个处理大量命令行参数的小程序,所以我决定使用getopt为我排序.

但是,我希望两个非选项参数(源文件和目标文件)是必需的,因此在调用程序时必须将它们作为参数,即使没有标志或其他参数.

这是我用标志处理参数的简化版本:

while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) {
    switch (c) {
        case 'i': {
            i = (int)atol(optarg);
        }
        case 'd': {
            d = (int)atol(optarg);
        }
        case 'b':
            buf = 1;
            break;
        case 't':
            time = 1;
            break;
        case 'w':
            w = (int)atol(optarg);
            break;
        case 'h':
            h = (int)atol(optarg);
            break;
        case 's':
            s = (int)atol(optarg);
            break;
        default:
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何编辑它以便还处理非选项参数?

我还希望能够在选项之前之后获得非选项,那么如何处理呢?

c arguments getopt getopt-long argv

14
推荐指数
2
解决办法
2万
查看次数

getopt_long_only分段错误

我正在尝试使用getopt_long_only来解析命令行.我的应用程序读取了一些命令行选项.

例如"app --alpha = 1 --beta = 2 --cecil = 3"

只要传入有效的命令行参数,getopt_long_only就可以正常工作.但是如果在末尾和其他不适当的位置调用带有无效"单个虚线"选项的应用程序,则会发生seg故障崩溃.这里发生了什么?似乎getopt_long_only对错误的参数没有弹性.或者我调用函数错了?

例:

> ./app --beta=1 -?
starting
index = 1   ret=0  optarg=1
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

下面的代码(C++:app.cc)

#include <stdio.h>
#include <getopt.h>

void ProcessCommandLineArgs(int argc, char** argv)
{

    option longopts[] = {
        {"alpha", optional_argument, 0, 0},
        {"beta",  optional_argument, 0, 0},
        {"cecil", optional_argument, 0, 0}
    };

    int index;
    int ret;
    bool fParseError = false;

    while (true)
    {
        ret = ::getopt_long_only(argc, argv, "", longopts, &index);

        if (ret < 0)
        {
            break;
        }

        if ((ret …
Run Code Online (Sandbox Code Playgroud)

linux command-line getopt

13
推荐指数
1
解决办法
2126
查看次数

陷阱getopt无效选项

我正在使用getopt(不getops)为我的bash脚本提供处理选项和开关(long -option和short -o表单)的能力.

我希望能够捕获无效选项并处理它们,通常会回应用户应该尝试cmd --help然后退出脚本.

事实是,getopt捕获了无效选项,它本身输出一条消息,例如"getopt:invalid option - 'x'"

这是我用来设置我的getopt参数的模式:

set -- $(getopt -o $SHORT_OPTIONS -l $LONG_OPTIONS -- "$@")
Run Code Online (Sandbox Code Playgroud)

$ LONG_OPTIONS和$ SHORT_OPTIONS都是以逗号分隔的选项列表.

以下是我处理选项的方法:

 while [ $# -gt 0 ]
    do
        case "$1" in
            -h|--help)
                cat <<END_HELP_OUTPUT

    Help
    ----

    Usage: ./cmd.sh 

    END_HELP_OUTPUT

                shift;
                exit
                ;;
            --opt1)
                FLAG1=true
                shift
                ;;
            --opt2)
                FLAG2=true
                shift
                ;;
            --)
                shift
                break
                ;;
            *)
                echo "Option $1 is not a valid option."
                echo "Try './cmd.sh --help for more information."
                shift
                exit …
Run Code Online (Sandbox Code Playgroud)

bash shell case getopt

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

getopt ×10

c ×6

getopt-long ×2

linux ×2

arguments ×1

argv ×1

bash ×1

c++ ×1

case ×1

command-line ×1

command-line-arguments ×1

input ×1

perl ×1

shell ×1

unix ×1