在C/C++中创建unix/linux命令行工具的最佳实践是什么?

Vel*_*tas 7 c unix linux parameters command-line-arguments

我目前的任务是为内部开发团队创建一些命令行帮助程序实用程序.但是,我想知道创建unix命令行工具的最佳实践.我已经尝试查看git源代码,以获取如何相应地读取参数和显示消息的示例.不过,我正在寻找一个明确的模板创建工具,安全地读取参数,并显示标准的"帮助"信息如果不正确的参数,用户类型或--help我想说明的帮助信息.是否有一个标准库用于读取-abcFGH--parameter切换哪个进程根据传递的参数启动?

命令行:

git
Run Code Online (Sandbox Code Playgroud)

要么

git --help
Run Code Online (Sandbox Code Playgroud)

输出:

usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
       [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
       [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
       [-c name=value] [--help]
       <command> [<args>]
...
Run Code Online (Sandbox Code Playgroud)

命令行:

MyTool CommandName --CommandArgs
Run Code Online (Sandbox Code Playgroud)

输出:

无论具体命令是什么.


到目前为止我的工作:

码:

int main(int argc, char **argv)
{
    if(argc < 2)
    helpMessage();
    char* commandParameter = argv[1];
    if (strncmp(argv [1],"help", strlen(commandParameter)) == 0)
        helpMessage();
    else if (strncmp(argv [1],"pull", strlen(commandParameter)) == 0)
        pull();
    else
        helpMessage();
}
Run Code Online (Sandbox Code Playgroud)

什么是理想的将是这样的:

码:

int main(int argc, char **argv)
{
    MagicParameters magicParameters = new MagicParameters(argv);
    switch(magicParameters[1])
    {
        case command1:
            Command1();
            break;
        case ...

        case help:
        default:
            HelpMessage();
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 8

getopt_long()是你正在寻找的,这是最简单用法的一个例子:

   static const struct option opts[] = {
        {"version",   no_argument,    0, 'v'},
        {"help",      no_argument,    0, 'h'},
        {"message", required_argument, 0, 'm'},
        /* And so on */
        {0,      0,                   0,  0 }   /* Sentiel */
    };
    int optidx;
    char c;

    /* <option> and a ':' means it's marked as required_argument, make sure to do that.
     * or optional_argument if it's optional.
     * You can pass NULL as the last argument if it's not needed.  */
    while ((c = getopt_long(argc, argv, "vhm:", opts, &optidx)) != -1) {
        switch (c) {
            case 'v': print_version(); break;
            case 'h': help(argv[0]); break;
            case 'm': printf("%s\n", optarg); break;
            case '?': help(argv[0]); return 1;                /* getopt already thrown an error */
            default:
                if (optopt == 'c')
                    fprintf(stderr, "Option -%c requires an argument.\n",
                        optopt);
                else if (isprint(optopt))
                    fprintf(stderr, "Unknown option -%c.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character '\\x%x'.\n",
                        optopt);
               return 1;
        }
    }
    /* Loop through other arguments ("leftovers").  */
    while (optind < argc) {
        /* whatever */;
        ++optind;
    }
Run Code Online (Sandbox Code Playgroud)