C getopt -<整数>

发布于 2024-09-14 07:26:34 字数 152 浏览 7 评论 0原文

如何从命令行参数“tail -10”中获取选项-10。 getopt 函数查找 '1' 字符。但是我如何访问字符串“10”

如果这可以通过 getopt_long 完成,那么一个例子会有所帮助。谢谢。

How do I get the option -10 from command line arguments- "tail -10". getopt function finds '1' character. But how do I access the string "10"?

If this can be done by getopt_long, an example would help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法少女 2024-09-21 07:26:34

除非您打算将 -1 作为以 0 作为参数的选项,否则答案是您不这样做。 getopt 仅用于处理符合标准 POSIX 实用程序选项语法的选项。可以使用 GNU getopt_long 来实现此目的,或者您可以编写自己的 argv 解析器(这很简单)。

编辑:实际上我想我误读了你想要的内容。如果您希望将 - 后跟任何数字解释为具有该数值的选项,我认为没有任何版本的 getopt 可以工作。您无法将每个数字特殊情况下作为选项,如果您只是告诉 getopt 所有数字都是带有参数的选项字符,-123 将被解释为参数为 23-1 选项(这很好,您可以从那里解释它),但是一个单独的 -1 将导致下一个 argv 元素被 eaten 作为 -1 的参数,这是很难或不可能恢复的。

Unless you intend for -1 to be an option with 0 as its argument, the answer is you don't. getopt is only made for processing options that fit the standard POSIX utilities' option syntax. It may be possible to use GNU getopt_long for this purpose, or you could just write your own argv parser (it's easy).

Edit: Actually I think I misread what you want. If you want - followed by any number to be interpreted as an option with that numeric value, I don't think there's any version of getopt that will work. There's no way you can special-case every single number as an option, and if you simply tell getopt that all of the digits are option characters that take arguments, -123 will be interpreted as a -1 option with an argument of 23 (which is fine, you can interpret it from there), but a lone -1 will cause the next argv element to get eaten as an argument to -1, which is difficult or impossible to recover from.

动次打次papapa 2024-09-21 07:26:34

这里发生的事情是,c 语言绝对没有关于处理命令行选项的正确方法的标准。

此外,unix 世界在相当长的一段时间内没有这样的标准(相信 中有一个关于它的完整部分) Unix 仇恨者手册(PDF 链接))。

因此,人们编写了临时处理机制。这些最终发展成为一个通用标准,getopt 诞生了(在贝尔实验室)。后来我们得到了 GNU getoptgetopt_long。然而,这些并不是必需的,有些程序仍然以自己的方式管理事物。

但在事情稳定下来之前(有时是之后),人们会在处理选项的“通常”方式中添加新功能,只要它们看起来是个好主意。现在,很有可能 tail 会希望比任何其他功能更频繁地调整行数,因此使其简单 很少的击键来调整在当时看来是个好主意......

What is going on here is that c the language comes with absolutely no standard about the proper way to handle command line options.

Moreover, the unix world had no such standard for quite some time ( believe there is a whole section about it in the Unix Haters Handbook (PDF link)).

So, people wrote ad hoc handling mechanisms. These eventually evolved toward a common(ish) standard and getopt was born (at Bell labs). Later we got the GNU getopt and getopt_long. These are not required, however, and some programs still manage things their own way.

But before things settled down (and occasionally after), people added new features to the "usual" way of handling options whenever they seemed like a good idea. Now, it's a pretty good bet that tail will want to have the number of lines adjusted more often than any other feature, so making it easy and few keystrokes to adjust must have seemed like a good idea at the time...

怀中猫帐中妖 2024-09-21 07:26:34

我从来没有那么喜欢 getopt,所以我做了平常的事情并重新发明了轮子。我将我的解决方案称为 argopt。您可以在 https://github.com/colding/argopt 获取源代码和手册页。我认为它比 getopt 更容易使用。

I never did like getopt that much, so I did the usual thing and reinvented the wheel. I call my solution argopt. You can get the source and manual page at https://github.com/colding/argopt. It is, I think, much easier to use than getopt.

焚却相思 2024-09-21 07:26:34

在可能的情况下,我建议更改命令参数语法以避免数字参数,方法是将数字作为选项参数传递(例如 -n -5),或者仅在 -- 之后允许它 选项分隔符。

对于无法更改参数语法的情况,BUGS BSD getopt(3) 手册页 的部分记录了一种实现此目的的方法。我调整了他们的解决方案以避免 optreset,即 POSIX 未指定glibc 不支持

int ch;
long num;
char *numarg;

while ((ch = getopt(argc, argv, "0123456789")) != -1) {
    switch (ch) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        numarg = argv[optind - 1];
        if (numarg[0] == '-' && numarg[1] == ch && numarg[2] == '\0') {
            num = '0' - ch;
        } else if ((numarg = argv[optind]) != NULL && numarg[1] == ch) {
            char *ep;
            int numoptind = optind;

            num = strtol(numarg, &ep, 10);
            if (*ep != '\0') {
                fprintf(stderr, "illegal number -- %s\n", numarg);
                return EXIT_FAILURE;
            }

            /* Advance getopt internal state to next argument. */
            while (optind == numoptind) {
                ch = getopt(argc, argv, "0123456789");
                assert(ch >= '0' && ch <= '9');
            }
        } else {
            fprintf(stderr, "number after other options -- %s\n", numarg);
            return EXIT_FAILURE;
        }
        break;

    default:
        /* Unrecognized option character.  Error printed by getopt. */
        return EXIT_FAILURE;
    }
}

您可以将 getopt 替换为 getopt_long,并根据需要添加其他短或长选项。

Where possible, I would recommend changing the command argument syntax to avoid numeric arguments, either by passing the number as an option argument (e.g. -n -5) or allowing it only after the -- option delimiter.

For cases where changing the argument syntax is not possible, the BUGS section of the BSD getopt(3) man page documents a way to do this. I've adjusted their solution to avoid optreset, which is not specified by POSIX and not supported by glibc:

int ch;
long num;
char *numarg;

while ((ch = getopt(argc, argv, "0123456789")) != -1) {
    switch (ch) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        numarg = argv[optind - 1];
        if (numarg[0] == '-' && numarg[1] == ch && numarg[2] == '\0') {
            num = '0' - ch;
        } else if ((numarg = argv[optind]) != NULL && numarg[1] == ch) {
            char *ep;
            int numoptind = optind;

            num = strtol(numarg, &ep, 10);
            if (*ep != '\0') {
                fprintf(stderr, "illegal number -- %s\n", numarg);
                return EXIT_FAILURE;
            }

            /* Advance getopt internal state to next argument. */
            while (optind == numoptind) {
                ch = getopt(argc, argv, "0123456789");
                assert(ch >= '0' && ch <= '9');
            }
        } else {
            fprintf(stderr, "number after other options -- %s\n", numarg);
            return EXIT_FAILURE;
        }
        break;

    default:
        /* Unrecognized option character.  Error printed by getopt. */
        return EXIT_FAILURE;
    }
}

You can replace getopt with getopt_long and add additional short or long options as necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文