Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ getopt命令行获取值c++;_C++ - Fatal编程技术网

C++ getopt命令行获取值c++;

C++ getopt命令行获取值c++;,c++,C++,如何获取getopt中的标志值,我已经尝试过用谷歌搜索它,但我得到的只是如何切换案例和设置标志。我得到了下面的代码,我试图做的是有三个标志,-a,-b,-c,但只传递了两个标志,如/filename-a somevalue-c anothervalue或/filename-b somevalue-c anothervalue有帮助吗 int main(int argc, char *argv[]) { int flagA = 0; int flagB = 0; while (

如何获取getopt中的标志值,我已经尝试过用谷歌搜索它,但我得到的只是如何切换案例和设置标志。我得到了下面的代码,我试图做的是有三个标志,-a,-b,-c,但只传递了两个标志,如
/filename-a somevalue-c anothervalue
/filename-b somevalue-c anothervalue
有帮助吗

int main(int argc, char *argv[])
{
   int flagA = 0;
   int flagB = 0;

   while (1) {
    char c;

    c = getopt (argc, argv, "abc:");
    if (c == -1) {
        break;
    }
    switch (c) {
    case 'a':
        flagA = 1;
         //cout<<optarg<<endl; //I tried printing the value but it only prints the second  flag value
        break;
    case 'b':
        flagB = 1;
        cout<<optarg<<endl;
        break;
    case 'c':
        cout<<optarg<<endl;
        break;
   case '?':
    default:
       cout<<"Usage: %s [-a] [-b <something>].\n", argv[0]<<endl;
    }
   if(flagA > 0){
    //do something using the values of flagA and flagC 
   }
   else if(flagB > 0){
      //do something using the values of flagB and flagC

   }
 }
 return 0;
 }
intmain(intargc,char*argv[])
{
int-flagA=0;
int-flagB=0;
而(1){
字符c;
c=getopt(argc,argv,abc:);
如果(c==-1){
打破
}
开关(c){
案例“a”:
flagA=1;

//cout如果要将
optarg
与标志一起使用,则在选项字符串中,标志后面必须有

        c = getopt(argc, argv, "a:b:c:");
您的用法输出语句不应该为您编译。此外,您还试图将C样式格式说明符混合到C++输出流中,这是不正确的。
            cout << "Usage: " << argv[0] << " [-a] [-b <something>].\n";

cout代码有多个问题,但关键问题在于调用
getopt()
。您有选项控制字符串
“abc:
,这意味着
-a
-b
都不接受选项

假设您使用的是POSIX标准版本而不是GNU,那么当您运行以下任一操作时:

./filename -a somevalue -c anothervalue
./filename -b somevalue -c anothervalue
第一个标志后的
somevalue
表示选项已完成,其余的参数为非选项参数。GNU
getopt()
,在没有环境变量
POSIXLY\u CORRECT
的情况下,将排列参数列表,以便还可以识别
-c另一个值(参数的排列方式是,
somevalue
以结尾结束)

您的代码编写得更好:

#define _XOPEN_SOURCE 600 
#include <iostream>
#include <unistd.h>
using namespace std;

int main(int argc, char *argv[])
{
    int flagA = 0;
    int flagB = 0;
    int opt;
    char *c_opt = 0;

    while ((opt = getopt(argc, argv, "abc:")) != -1)
    {
        switch (opt)
        {
        case 'a':
            flagA = 1;
            cout << "A: " << flagA << "\n";
            break;
        case 'b':
            flagB = 1;
            cout << "B: " << flagB << "\n";
            break;
        case 'c':
            c_opt = optarg;
            cout << "C: " << c_opt << "\n";
            break;
        default:
            cerr << "Usage: " << argv[0] << " [-a] [-b] [-c <something>]\n";
            return -1;
        }
    }

    if (flagA)
    {
        // do something using the values of flagA and c_opt (if set)
    }
    else if (flagB)
    {
        // do something using the values of flagB and c_opt (if set)
    }
    else if (c_opt)
    {
        // do something using just c_opt (neither flagA nor flagB is set)
    }
    else
    {
        // do something if no options are specified (report error?)
    }

    for (int i = optind; i < argc; i++)
        cout << "File: " << i << ": " << argv[i] << "\n";
    return 0;
}
BSD ish
getopt()
与POSIX标准非常接近,因此我得到的结果是:

$ ./opt -a 5 -c something
A: 1
File: 2: 5
File: 3: -c
File: 4: something
$

:

我想让
-a
-b
选项取一个参数。我想做的是我有两个函数取两个参数,所以如果设置了
-a
,那么我想调用函数
-a
,否则如果
-b
则调用函数
b
。我需要这两个值来调用函数

鉴于这一澄清,代码可能如下所示:

#define _XOPEN_SOURCE 600
#include <cstdlib>
#include <iostream>
#include <unistd.h>
using namespace std;

static void usage(const char *arg0)
{
    cerr << "Usage: " << arg0 << " [-a filename | -b filename] -c <something>\n";
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
    char *a_opt = 0;
    char *b_opt = 0;
    char *c_opt = 0;
    int opt;

    while ((opt = getopt(argc, argv, "a:b:c:")) != -1)
    {
        switch (opt)
        {
        case 'a':
            a_opt = optarg;
            cout << "A: " << a_opt << "\n";
            break;
        case 'b':
            b_opt = optarg;
            cout << "B: " << b_opt << "\n";
            break;
        case 'c':
            c_opt = optarg;
            cout << "C: " << c_opt << "\n";
            break;
        default:
            cerr << "Unknown option: " << optopt << "\n";
            usage(argv[0]);
            break;
        }
    }

    if (optind != argc || c_opt == 0 || (a_opt && b_opt) || (a_opt == 0 && b_opt == 0))
        usage(argv[0]);
    else if (a_opt)
        cout << "A&C: " << a_opt << " " << c_opt << "\n";
    else
        cout << "B&C: " << b_opt << " " << c_opt << "\n";

    return 0;
}

谢谢你的帮助,但是当我运行./filename-a 5-c“some text”时,它只打印5.correct,但我想要的是标志的值,而不是同时显示这两个值。
coutYou没有要求一个工作程序,只有你做错了什么,这个答案说明了这一点。当我执行
/filename-a 5-c时,这不起作用“sometext”
我得到了
用法:./project[-a][-b][-c][/code>我也在代码块上..Ubuntu,我不知道我怎么知道哪一个是标准的。(1)Ubuntu使用GNU
getopt()
(2)您是否希望
-a
选项接受参数?如果应该有参数,请在选项字符串中使用
a:
;否则,像现在一样只使用
a
。类似地,对于
-b
,您的问题在显示所需内容时并不完全一致。我希望-a/-b选项接受参数。我将编辑我的问题,但什么我想做的是,我有两个函数,它们有两个参数。因此,如果设置了
-a
,那么我想调用函数a,如果设置了
-b
,则调用另一个。我需要两个值来调用函数。
#define _XOPEN_SOURCE 600
#include <cstdlib>
#include <iostream>
#include <unistd.h>
using namespace std;

static void usage(const char *arg0)
{
    cerr << "Usage: " << arg0 << " [-a filename | -b filename] -c <something>\n";
    exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
    char *a_opt = 0;
    char *b_opt = 0;
    char *c_opt = 0;
    int opt;

    while ((opt = getopt(argc, argv, "a:b:c:")) != -1)
    {
        switch (opt)
        {
        case 'a':
            a_opt = optarg;
            cout << "A: " << a_opt << "\n";
            break;
        case 'b':
            b_opt = optarg;
            cout << "B: " << b_opt << "\n";
            break;
        case 'c':
            c_opt = optarg;
            cout << "C: " << c_opt << "\n";
            break;
        default:
            cerr << "Unknown option: " << optopt << "\n";
            usage(argv[0]);
            break;
        }
    }

    if (optind != argc || c_opt == 0 || (a_opt && b_opt) || (a_opt == 0 && b_opt == 0))
        usage(argv[0]);
    else if (a_opt)
        cout << "A&C: " << a_opt << " " << c_opt << "\n";
    else
        cout << "B&C: " << b_opt << " " << c_opt << "\n";

    return 0;
}
$ ./opt -a option -c something
A: option
C: something
A&C: option something
$