通过传递命令行参数自动保存操作

Automatically saving the operation through passing the command line argument

我的工具中已经有像普通工具一样的保存功能。现在我想添加自动保存功能,它必须在不按 ctrl+S 的情况下保存在工具中所做的所有操作(我们称之为覆盖),必须自动完成。我需要编写一个 auotsave 函数,并且必须作为命令行参数传递。有人可以帮助我吗?更多详细信息:当我在 Linux 中正确打开我的工具时,我们通常会像 cd home/...bla bla../ 和 ToolName 这样传递。我需要使用自动保存功能作为命令行参数。例如:当我将参数作为 -s 传递以自动保存函数时,当我尝试传递 like....... ToolName -s....... 它必须自动保存数据或操作这是在我的工具中执行的。寻找您宝贵的答案

int main( int argc, char* argv[] )
{
    const std::string OPT = "-A";
    bool hasOption =  false;
    for ( int i = 1; i < argc; ++i ) {
        if ( OPT == argv[i] ) {
              hasOption = true;
              break;
        }
    }

    if ( hasOption ) {

    }
    return 0;
 }

如果只有这个参数,可以用下面的:

int main(int argc, char *argv[]){
//...
  // argv[0] contains the program name
  if(argc >= 2 && strncmp(argv[1], "-s", 2){
    // start a thread that saves and sleeps in a loop
  }
//...
return 0;
}

否则你必须像这样遍历参数:

int i;
for(i=1; i<argc; ++i){
  if(strncmp(argv[i], "-s", 2){
     // start a thread that saves and sleeps in a loop
  }
}

还有用于参数解析的库,但我怀疑您是否只需要一个参数。