将 std::string 作为输入传递给 getopt()

Passing std::string as input to getopt()

我的程序基本上是这样的:

#include <iostream>
#include <unistd.h> // getopt
#include <vector>
#include <string>
#include <list>
#include <sstream>

using namespace std;

//a parameter structure to store parameters provided through console
typedef struct pairwise_param {
    double alpha;
    double beta;
} param;

//parse the parameter values
void param_getopt(param *pm, int argc, char **argv) {
    int opt;

    while((opt = getopt(argc, argv, "a:b:")) != -1) {
        switch(opt) {
        case 'a':
            pm->alpha  = atof(optarg);
            break;

        case 'b':
            pm->beta   = atof(optarg);
            break;
        }
    }
}

int main(int argc, char* argv[]) {
    //initialize param structure
    param pm;

    //pass command line arguments to param
    param_getopt(&pm, argc, argv);

    //do something to the parameters
    std::cout << "Alpha: " << pm.alpha  << std::endl;
    std::cout << "Beta: " << pm.beta  << std::endl;


    return(0);
}

通过制作头文件并将主函数更改为其他名称,例如int main(int argc, char* argv[]) -> int maincomp(int argc, char* argv[]) 我想从另一个程序调用新函数 maincomp() 但我不想传递命令行参数,而是想通过 std::string.

传递参数

我想我可以做这样的事情,但 getopt() 似乎确实有一些问题,我不完全确定为什么。当前,在调用 getopt() 之后使用 std::cout 写入控制台的任何内容都不会显示。看起来当前传递给 getopt() 的内容未正确类型转换。因此,我的问题是一种类型应该如何转换 std::string 以符合 getopt(int argc, char * const argv[])char * const argv[] 输入要求?

int main(int argc, char* argv[]) {
    //create string and pass it to maincomp
    std::string cmd = "-a2.3 -b3.2";
    std::istringstream ss(cmd);
    std::string arg;
    std::list<std::string> ls;
    std::vector<char*> newargv;

    while (ss >> arg) {
        ls.push_back(arg);
        newargv.push_back(const_cast<char*>(ls.back().c_str()));
    }

    newargv.push_back(0);

    int out = maincomp(newargv.size(), &newargv[0]);

    return(out);
}

完整代码: https://onlinegdb.com/tjMC-LwiP

使用 wordsexp.h 通过正确解析 getopt() 的字符串解决了问题。

本质上:

//create string and pass it to maincomp    
std::string cmd = "-a2.3 -b3.2";

//convert string with wordexp
wordexp_t newargv;
newargv.we_offs = 1;
wordexp(cmd.c_str(), &newargv, WRDE_DOOFFS);
//create a new argc
int newargc = newargv.we_wordc + newargv.we_offs;
    
//pass to maincomp function
int out = maincomp(newargc, newargv.we_wordv);

完整代码:

#include <iostream>
#include <unistd.h> // getopt
#include <vector>
#include <string>
#include <wordexp.h>


typedef struct pairwise_param {
  double alpha; double beta;
} param;

void param_getopt(param *pm, int argc, char **argv){
    int opt;
    while((opt=getopt(argc,argv,"a:b:"))!=-1){
        switch(opt){
            case 'a': pm->alpha  = atof(optarg);              break;
            case 'b': pm->beta   = atof(optarg);              break;
        }
    }
}

int maincomp(int argc, char* argv[]){
  //initialize param structure
  param pm;

  //pass command line arguments to param
  param_getopt(&pm, argc, argv);
  
  std::cout << "Alpha: " << pm.alpha  << std::endl;
  std::cout << "Beta: " << pm.beta  << std::endl;


  return(0);
}

int main(int argc, char* argv[]) {
    //create string and pass it to maincomp    
    std::string cmd = "-a2.3 -b3.2";

    //convert string with wordexp
    wordexp_t newargv;
    newargv.we_offs = 1;
    wordexp(cmd.c_str(), &newargv, WRDE_DOOFFS);
    int newargc = newargv.we_wordc+newargv.we_offs;

    //pass to maincomp function
    int out = maincomp(newargc, newargv.we_wordv);

    return(out);
}

产生预期的输出:

Alpha: 2.3
Beta: 3.2