在C/C ++中创建unix/linux命令行工具的最佳实践是什么? [英] What is the best practice for creating a unix/linux command-line tool in C/C++?

查看:99
本文介绍了在C/C ++中创建unix/linux命令行工具的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的任务是创建一些命令行帮助程序实用程序,以供内部开发团队使用.但是,我想知道创建UNIX命令行工具的最佳实践.我尝试查看 git源代码,以获取有关如何读取参数并相应显示消息的示例.但是,我正在寻找一个清晰的模板来创建工具,安全地读取参数并在用户键入不正确的参数或--help时显示标准的帮助"消息,我想显示帮助消息.是否有一个标准库可以读取-abcFGH--parameter并根据传递的参数切换哪个进程开始?

I currently am tasked with creating some command-line helper utilities for our internal development team to use. However, I want to know the best practice for creating unix command-line tools. I have tried viewing git source code for an example of how to read parameters and display messages accordingly. However, I'm looking for a clear template for creating a tool, reading parameters safely, and displaying the standard "help" messages if a user types in an incorrect parameter or --help I want to show the help message. Is there a standard library for reading -abcFGH and --parameter and switching which process starts based upon the passed parameter?

命令行:

git

git --help

输出:

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>]
...

命令行:

MyTool CommandName --CommandArgs

输出:

该特定命令的作用.

到目前为止我正在做什么:

What I have working so far:

代码:

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();
}

理想的外观如下:

代码:

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

        case help:
        default:
            HelpMessage();
            break;
    }
}

推荐答案

getopt_long()是您要寻找的,这是最简单的用法示例:

getopt_long() is what you're looking for, here's an example of the simplest usage:

   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;
    }

这篇关于在C/C ++中创建unix/linux命令行工具的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆