我可以获得的最简单的getopt程序

我可以获得的最简单的getopt程序

本文介绍了我可以获得的最简单的getopt程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对此链接关于如何使用 getopt()的方法,我正在尝试举一个小例子。

After doing some reading on this link on how to use getopt(), I'm trying to get a small example.

我想要什么,就像这样:

What I want, is something like:

./prog -v      # show me prog version
./prog -f filename  # just show me the filename I entered from the command line

输入的文件名,这就是我到目前为止所写的内容:

Here is what I wrote so far:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int
main(int argc, *argv[]) {
     char VER[] = "0.1.1";
     int opt;
     opt = getopt(argc, argv, "vf:");
     char *filename;

      while (opt != -1) {
           switch(opt) {
            case 'v':
                printf("version is %s", VER);
                break;
            case 'f':
                filename = optarg;
                break;
            }
     }
    printf("The filename was %s", filename);
    return 0;
}

我用以下代码编译代码:

I compile the code with:

$ gcc prog.c -o prog -Wall -Wextra

当我使用 -v 选项运行它时,我似乎不明白,它永远不会停止打印
和 -f filename 会停在那里,并且从不打印我输入的文件名。

I can't seem to understand when I run it with -v option it never stops printing the versionand with -f filename it stops there and never prints the filename I entered.

推荐答案

它不会不要停止,因为您只调用一次 getopt()。可能的解决方法:

It doesn't stop because you only call getopt() once. A possible fix:

#include <stdio.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
    char VER[] = "0.1.1";
    int opt;
    const char *filename = "unspecified";

    while ((opt = getopt(argc, argv, "vf:")) != -1)
    {
        switch (opt)
        {
            case 'v':
                printf("version is %s\n", VER);
                break;
            case 'f':
                filename = optarg;
                break;
            default:
                fprintf(stderr, "Usage: %s [-v][-f file]\n", argv[0]);
                return(1);
        }
    }
    printf("The filename was %s\n", filename);
    return 0;
}

请注意,我已经确保 filename 已初始化, printf()输出以换行符结尾,并且报告了错误情况。

Note that I've made sure that filename is initialized, that printf() outputs end with a newline, and that the error cases are reported.

这是另一个稍微复杂一些的示例程序:

Here's another, slightly more complex, example program:

/* Example 1 - using POSIX standard getopt() */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    int opt;
    int i;
    int bflag = 0;
    int aflag = 0;
    int errflag = 0;
    char *ifile = 0;
    char *ofile = 0;

    while ((opt = getopt(argc, argv, ":abf:o:")) != -1)
    {
        switch (opt)
        {
        case 'a':
            if (bflag)
                errflag++;
            else
                aflag++;
            break;
        case 'b':
            if (aflag)
                errflag++;
            else
                bflag++;
            break;
        case 'f':
            ifile = optarg;
            break;
        case 'o':
            ofile = optarg;
            break;
        case ':':   /* -f or -o without operand */
            fprintf(stderr, "Option -%c requires an operand\n", optopt);
            errflag++;
            break;
        case '?':
        default:
            fprintf(stderr, "Unrecognized option: -%c\n", optopt);
            errflag++;
            break;
        }
    }

    if (errflag)
    {
        fprintf(stderr, "Usage: %s [-a|-b][-f in][-o out] [file ...]\n", argv[0]);
        exit(2);
    }

    printf("Flags: a = %d, b = %d\n", aflag, bflag);
    if (ifile != 0)
        printf("Input: %s\n", ifile);
    if (ofile != 0)
        printf("Output: %s\n", ofile);
    printf("Argc = %d, OptInd = %d\n", argc, optind);
    for (i = optind; i < argc; i++)
        printf("File: %s\n", argv[i]);
    return(EXIT_SUCCESS);
}

它基于Sun手册中的示例。 -a -b 选项是互斥的。它说明了POSIX getopt()的(局限性)启用了可选参数(选项的前导串)。它还在末尾输出其输入。

It is based on an example from a Sun manual. The -a and -b options are mutually exclusive. It illustrates (the limitations of) POSIX getopt() with 'optional arguments' enabled (the leading : on the option string). It also prints out its inputs at the end.

这篇关于我可以获得的最简单的getopt程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:52