在启动时为程序添加参数

时间:2022-08-24 12:43:20

I'm currently trying to make a small application that performs different duties. Right now I have a console app pop up and ask what I want to do, but sometimes I would rather just launch it with something like MyApp.exe -printdocuments or some such thing.

我目前正在尝试制作一个执行不同职责的小型应用程序。现在我有一个控制台应用程序弹出并询问我想做什么,但有时我宁愿用MyApp.exe -printdocuments或类似的东西启动它。

Are there any tutorials out there that can show me a simple example of this?

是否有任何教程可以向我展示一个简单的例子?

5 个解决方案

#1


In C++, your main() function can have argc and argv parameters, which contain the arguments passed on the command line. The argc is the count of arguments (including the executable name itself), and argv is an array of pointers to null-terminated strings of length argc.

在C ++中,main()函数可以包含argc和argv参数,这些参数包含在命令行上传递的参数。 argc是参数计数(包括可执行文件名本身),而argv是指向以null结尾的长度为argc的字符串的指针数组。

For example, this program prints its arguments:

例如,该程序打印其参数:

#include <stdio.h>

int main(int argc, char *argv[])
{
    for (int i = 0; i < argc; i++) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
    return 0;
}

Any C or C++ tutorial will probably have more information on this.

任何C或C ++教程都可能有更多相关信息。

#2


You can use boost::program_options to do this, If you don't want to use boost library, you must parse main function arguments by yourself.

您可以使用boost :: program_options执行此操作,如果您不想使用boost库,则必须自己解析主函数参数。

#3


getopt is a posix function (implementations for windows exist) that can help you parse your arguments:

getopt是一个posix函数(存在windows的实现),可以帮助你解析你的参数:

#include <unistd.h> // getopt

// call with my_tool [-n] [-t <value>]
int main(int argc, char *argv[])
{
    int opt;
    int nsecs;
    bool n_given = false, t_given = false;
    // a colon says the preceding option takes an argument
    while ((opt = ::getopt(argc, argv, "nt:")) != -1) {
        switch (opt) {
        case 'n':
            n_given = true;
            break;
        case 't':
            nsecs = boost::lexical_cast<int>(optarg);
            t_given = true;
            break;
        default: /* '?' */
            std::cerr << "Usage: "
                      << argv[0] << " [-t <value>] [-n]\n";
            return 1;
        }
    }
    return 0;
}

#4


You would do well to use a library for this. Here are a few links that might get you started on command line arguments with c++.

你最好使用一个库。这里有一些链接可能会让你开始使用c ++的命令行参数。

  1. gperf

#5


your entry point method i.e. in C++ your main method needs to look like

您的入口点方法,即在C ++中您的主要方法需要看起来像

int main ( int argc, char *argv[] );

you can read this article and accomplish what you are trying to do

你可以阅读这篇文章并完成你想要做的事情

#1


In C++, your main() function can have argc and argv parameters, which contain the arguments passed on the command line. The argc is the count of arguments (including the executable name itself), and argv is an array of pointers to null-terminated strings of length argc.

在C ++中,main()函数可以包含argc和argv参数,这些参数包含在命令行上传递的参数。 argc是参数计数(包括可执行文件名本身),而argv是指向以null结尾的长度为argc的字符串的指针数组。

For example, this program prints its arguments:

例如,该程序打印其参数:

#include <stdio.h>

int main(int argc, char *argv[])
{
    for (int i = 0; i < argc; i++) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
    return 0;
}

Any C or C++ tutorial will probably have more information on this.

任何C或C ++教程都可能有更多相关信息。

#2


You can use boost::program_options to do this, If you don't want to use boost library, you must parse main function arguments by yourself.

您可以使用boost :: program_options执行此操作,如果您不想使用boost库,则必须自己解析主函数参数。

#3


getopt is a posix function (implementations for windows exist) that can help you parse your arguments:

getopt是一个posix函数(存在windows的实现),可以帮助你解析你的参数:

#include <unistd.h> // getopt

// call with my_tool [-n] [-t <value>]
int main(int argc, char *argv[])
{
    int opt;
    int nsecs;
    bool n_given = false, t_given = false;
    // a colon says the preceding option takes an argument
    while ((opt = ::getopt(argc, argv, "nt:")) != -1) {
        switch (opt) {
        case 'n':
            n_given = true;
            break;
        case 't':
            nsecs = boost::lexical_cast<int>(optarg);
            t_given = true;
            break;
        default: /* '?' */
            std::cerr << "Usage: "
                      << argv[0] << " [-t <value>] [-n]\n";
            return 1;
        }
    }
    return 0;
}

#4


You would do well to use a library for this. Here are a few links that might get you started on command line arguments with c++.

你最好使用一个库。这里有一些链接可能会让你开始使用c ++的命令行参数。

  1. gperf

#5


your entry point method i.e. in C++ your main method needs to look like

您的入口点方法,即在C ++中您的主要方法需要看起来像

int main ( int argc, char *argv[] );

you can read this article and accomplish what you are trying to do

你可以阅读这篇文章并完成你想要做的事情