解析命令行参数?

use*_*020 83 c command-line-arguments

嗨,我正在尝试编写一个程序,可以逐行,逐字或逐字符地比较两个文件.它必须能够读入命令行选项"-l -w -i或 - "...如果选项是-l,它会逐行比较文件.如果选项是-w,它会逐字比较文件.如果选项是 - 它会自动假定下一个arg是第一个文件名.如果选项是-i,则以不区分大小写的方式对它们进行比较.否则,它默认按字符比较文件

只要-w和-l没有同时输入且没有多于或少于2个文件,输入选项的时间并不重要.

我甚至不知道从哪里开始解析命令行参数.请帮忙 :(

所以这就是我想出的所有代码.我还没有错误检查它,但我想知道我是否以过于复杂的方式写东西?

/*
 * Functions to compare files.
 */
int compare_line();
int compare_word();
int compare_char();
int case_insens();

/*
 * Program to compare the information in two files and print message saying 
 * whether or not this was successful.
 */
int main(int argc, char* argv[])
{
/*Loop counter*/
  size_t i = 0;

  /*Variables for functions*/
  int caseIns = 0;
  int line = 0;
  int word = 0;

  /*File pointers*/
  FILE *fp1, *fp2;

  /*
   * Read through command-line arguments for options.
   */
  for (i = 1; i < argc; i++) {
    printf("argv[%u] = %s\n", i, argv[i]);
    if (argv[i][0] == '-') {
       if (argv[i][1] == 'i') 
       {
           caseIns = 1;
       }
       if (argv[i][1] == 'l')
       {
           line = 1;
       }
       if (argv[i][1] == 'w')
       {
           word = 1;
       }
       if (argv[i][1] == '-')
       {
           fp1 = argv[i][2];
           fp2 = argv[i][3];
       }
       else 
       {
           printf("Invalid option.");
           return 2;
       }
    } else {
       fp1(argv[i]);
       fp2(argv[i][1]);
    }
  }

  /*
   * Check that files can be opened.
   */
  if(((fp1 = fopen(fp1, "rb")) ==  NULL) || ((fp2 = fopen(fp2, "rb")) == NULL))
  {
      perror("fopen()");
      return 3;
  }
  else{
        if (caseIns == 1)
        {
            if(line == 1 && word == 1)
            {
                printf("That is invalid.");
                return 2;
            }
            if(line == 1 && word == 0)
            {
                if(compare_line(case_insens(fp1, fp2)) == 0)
                        return 0;
            }
            if(line == 0 && word == 1)
            {
                if(compare_word(case_insens(fp1, fp2)) == 0)
                    return 0;
            }
            else
            {
                if(compare_char(case_insens(fp1,fp2)) == 0)
                    return 0;
            }
        }
        else
        {
            if(line == 1 && word == 1)
            {
                printf("That is invalid.");
                return 2;
            }
            if(line == 1 && word == 0)
            {
                if(compare_line(fp1, fp2) == 0)
                    return 0;
            }
            if(line == 0 && word == 1)
            {
                if(compare_word(fp1, fp2) == 0)
                    return 0;
            }
            else
            {
                if(compare_char(fp1, fp2) == 0)
                    return 0;
            }
        }

  }
    return 1;
    if(((fp1 = fclose(fp1)) == NULL) || (((fp2 = fclose(fp2)) == NULL)))
        {
            perror("fclose()");
            return 3;
        }
        else
        {
            fp1 = fclose(fp1);
            fp2 = fclose(fp2);
        }
}

/*
 * Function to compare two files line-by-line.
 */
int compare_line(FILE *fp1, FILE *fp2)
{
    /*Buffer variables to store the lines in the file*/
    char buff1 [LINESIZE];
    char buff2 [LINESIZE];

    /*Check that neither is the end of file*/
    while((!feof(fp1)) && (!feof(fp2)))
    {
        /*Go through files line by line*/
        fgets(buff1, LINESIZE, fp1);
        fgets(buff2, LINESIZE, fp2);
    }
    /*Compare files line by line*/
    if(strcmp(buff1, buff2) == 0)
    {
        printf("Files are equal.\n");
        return 0;
    }
    printf("Files are not equal.\n");
    return 1;
}   

/*
 * Function to compare two files word-by-word.
 */
int compare_word(FILE *fp1, FILE *fp2)
{
    /*File pointers*/
    FILE *fp1, *fp2;

    /*Arrays to store words*/
    char fp1words[LINESIZE];
    char fp2words[LINESIZE];

    if(strtok(fp1, " ") == NULL || strtok(fp2, " ") == NULL)
    {
        printf("File is empty. Cannot compare.\n");
        return 0;
    }
    else
    {
        fp1words = strtok(fp1, " ");
        fp2words = strtok(fp2, " ");

        if(fp1words == fp2words)
        {
            fputs(fp1words);
            fputs(fp2words);
            printf("Files are equal.\n");
            return 0;
        }
    }
    return 1;
}

/*
 * Function to compare two files character by character.
 */
int compare_char(FILE *fp1,FILE *fp2)
{
    /*Variables to store the characters from both files*/
    int c;
    int d;

    /*Buffer variables to store chars*/
    char buff1 [LINESIZE];
    char buff2 [LINESIZE];

    while(((c = fgetc(fp1))!= EOF) && (((d = fgetc(fp2))!=EOF)))
    {
        if(c == d)
        {
            if((fscanf(fp1, "%c", buff1)) == (fscanf(fp2, "%c", buff2)))
            {
                printf("Files have equivalent characters.\n");
                return 1;
                break;
            }
        }

    }
        return 0;
}

/*
 * Function to compare two files in a case-insensitive manner.
 */
int case_insens(FILE *fp1, FILE *fp2, size_t n)
{
    /*Pointers for files.*/
    FILE *fp1, *fp2;

    /*Variable to go through files.*/
    size_t i = 0;

    /*Arrays to store file information.*/
    char fp1store[LINESIZE];
    char fp2store[LINESIZE];

    while(!feof(fp1) && !feof(fp2))
    {
         for(i = 0; i < n; i++)
         {
                fscanf(fp1, "%s", fp1store);
                fscanf(fp2, "%s", fp2store);

                fp1store = tolower(fp1store);
                fp2store = tolower(fp2store);

                return 1;
         }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*jer 162

据我所知,如何在C中解析命令行参数的三种最流行的方法是:

  • Getopt(#include <unistd.h>来自POSIX C库),可以解决简单的参数解析任务.如果你对bash有点熟悉,那么bash内置的getopt是基于GNU libc的Getopt.
  • Argp(#include <argp.h>来自GNU C库),它可以解决更复杂的任务并处理类似的事情,例如:
    • -?,--help用于帮助信息,包括电子邮件地址
    • -V,--version有关版本信息
    • --usage对于使用消息
  • 自己做,我不推荐给别人的程序,因为太多可能会出错或质量下降.忘记' - '来停止选项解析的流行错误只是一个例子.

GNU C Library文档有一些很好的Getopt和Argp示例.

使用Getopt的示例

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

int main(int argc, char *argv[])
{
    bool isCaseInsensitive = false;
    int opt;
    enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE;

    while ((opt = getopt(argc, argv, "ilw")) != -1) {
        switch (opt) {
        case 'i': isCaseInsensitive = true; break;
        case 'l': mode = LINE_MODE; break;
        case 'w': mode = WORD_MODE; break;
        default:
            fprintf(stderr, "Usage: %s [-ilw] [file...]\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    // Now optind (declared extern int by <unistd.h>) is the index of the first non-option argument.
    // If it is >= argc, there were no non-option arguments.

    // ...
}
Run Code Online (Sandbox Code Playgroud)

使用Argp的示例

#include <argp.h>
#include <stdbool.h>

const char *argp_program_version = "programname programversion";
const char *argp_program_bug_address = "<your@email.address>";
static char doc[] = "Your program description.";
static char args_doc[] = "[FILENAME]...";
static struct argp_option options[] = { 
    { "line", 'l', 0, 0, "Compare lines instead of characters."},
    { "word", 'w', 0, 0, "Compare words instead of characters."},
    { "nocase", 'i', 0, 0, "Compare case insensitive instead of case sensitive."},
    { 0 } 
};

struct arguments {
    enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode;
    bool isCaseInsensitive;
};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    struct arguments *arguments = state->input;
    switch (key) {
    case 'l': arguments->mode = LINE_MODE; break;
    case 'w': arguments->mode = WORD_MODE; break;
    case 'i': arguments->isCaseInsensitive = true; break;
    case ARGP_KEY_ARG: return 0;
    default: return ARGP_ERR_UNKNOWN;
    }   
    return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };

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

    arguments.mode = CHARACTER_MODE;
    arguments.isCaseInsensitive = false;

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

    // ...
}
Run Code Online (Sandbox Code Playgroud)

自己动手的例子

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

int main(int argc, char *argv[])
{   
    bool isCaseInsensitive = false;
    enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE;
    size_t optind;
    for (optind = 1; optind < argc && argv[optind][0] == '-'; optind++) {
        switch (argv[optind][1]) {
        case 'i': isCaseInsensitive = true; break;
        case 'l': mode = LINE_MODE; break;
        case 'w': mode = WORD_MODE; break;
        default:
            fprintf(stderr, "Usage: %s [-ilw] [file...]\n", argv[0]);
            exit(EXIT_FAILURE);
        }   
    }   

    // *argv points to the remaining non-option arguments.
    // If *argv is NULL, there were no non-option arguments.

    // ...
}   
Run Code Online (Sandbox Code Playgroud)

免责声明:我是Argp的新手,该示例可能包含错误.

  • 真的彻底回答,谢谢Christian(upvoted).但是,mac用户应该知道argp方法不是跨平台兼容的.正如我在[here](http://lists.apple.com/archives/unix-porting/2006/Jun/msg00019.html)中发现的那样,Argp是一个非标准化的glibc API扩展.[可在gnulib中获取](https://www.gnu.org/software/gnulib/manual/html_node/argp_002eh.html),因此可以明确地添加到项目中.但是,对于纯mac或跨平台的开发人员来说,使用getopt方法可能更简单. (9认同)
  • 对于自己动手的版本,我不喜欢这些选项允许在之后添加额外的文本,例如 -wzzz 与 -w 解析相同,而且选项必须位于文件参数之前。 (2认同)
  • @杰克你是对的。尊重发现这一点。我不记得我写的时候是否注意到了这一点。这又是一个完美的例子,说明 DIY 很容易出错,因此不应该这样做。谢谢你的告诉,我可以修复这个例子。 (2认同)

Jon*_*ler 16

使用getopt(),或许getopt_long().

int iflag = 0;
enum { WORD_MODE, LINE_MODE } op_mode = WORD_MODE;  // Default set
int opt;

while ((opt = getopt(argc, argv, "ilw") != -1)
{
    switch (opt)
    {
    case 'i':
        iflag = 1;
        break;
    case 'l':
        op_mode = LINE_MODE;
        break;
    case 'w':
        op_mode = WORD_MODE;
        break;
    default:
        fprintf(stderr, "Usage: %s [-ilw] [file ...]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
}

/* Process file names or stdin */
if (optind >= argc)
    process(stdin, "(standard input)", op_mode);
else
{
    int i;
    for (i = optind; i < argc; i++)
    {
        FILE *fp = fopen(argv[i], "r");
        if (fp == 0)
            fprintf(stderr, "%s: failed to open %s (%d %s)\n",
                    argv[0], argv[i], errno, strerror(errno));
        else
        {
            process(fp, argv[i], op_mode);
            fclose(fp);
        }
    }
 }
Run Code Online (Sandbox Code Playgroud)

请注意,您需要确定要包含哪些标头(我将其设为4),并且我编写op_mode类型的方式意味着您在函数中遇到问题process()- 您无法访问那里的枚举.最好将枚举移到函数之外; 你甚至可以创建op_mode一个没有外部链接(一种奇特的说法static)的文件范围变量,以避免将其传递给函数.此代码不-作为标准输入的同义词处理,这是读者的另一个练习.请注意,会getopt()自动--为您标记选项的结尾.

我没有在编译器之上运行任何版本的输入; 可能会有错误.


要获得额外的功劳,请写一个(库)函数:

int filter(int argc, char **argv, int idx, int (*function)(FILE *fp, const char *fn));
Run Code Online (Sandbox Code Playgroud)

它封装了getopt()循环后处理文件名选项的逻辑.它应该-作为标准输入处理.请注意,使用它表示op_mode应该是静态文件范围变量.所述filter()函数接受argc,argv,optind和的指针的处理功能.它应该返回0(EXIT_SUCCESS),如果它能够打开所有文件并且函数的所有调用报告为0,否则为1(或EXIT_FAILURE).具有这样的功能简化了编写Unix风格的"过滤器"程序,这些程序读取命令行或标准输入上指定的文件.


dav*_*idA 9

我发现Gengetopt非常有用 - 您可以使用简单的配置文件指定所需的选项,并生成一个简单包含并与您的应用程序链接的.c/.h对.生成的代码使用getopt_long,似乎可以处理大多数常见的命令行参数,并且可以节省大量时间.

gengetopt输入文件可能如下所示:

version "0.1"
package "myApp"
purpose "Does something useful."

# Options
option "filename" f "Input filename" string required
option "verbose" v "Increase program verbosity" flag off
option "id" i "Data ID" int required
option "value" r "Data value" multiple(1-) int optional 
Run Code Online (Sandbox Code Playgroud)

生成代码很容易并且吐出来cmdline.h并且cmdline.c:

$ gengetopt --input=myApp.cmdline --include-getopt
Run Code Online (Sandbox Code Playgroud)

生成的代码很容易集成:

#include <stdio.h>
#include "cmdline.h"

int main(int argc, char ** argv) {
  struct gengetopt_args_info ai;
  if (cmdline_parser(argc, argv, &ai) != 0) {
    exit(1);
  }
  printf("ai.filename_arg: %s\n", ai.filename_arg);
  printf("ai.verbose_flag: %d\n", ai.verbose_flag);
  printf("ai.id_arg: %d\n", ai.id_arg);
  int i;
  for (i = 0; i < ai.value_given; ++i) {
    printf("ai.value_arg[%d]: %d\n", i, ai.value_arg[i]);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要进行任何额外的检查(例如确保标志是互斥的),您可以使用gengetopt_args_info结构中存储的数据相当容易地完成此操作.


gvo*_*sey 6

Docopt有一个我认为非常好的 C 实现:

从描述命令行选项的手册页标准化格式,docopt 推断并创建一个参数解析器。这是从 Python 开始的;Python 版本实际上只是解析文档字符串并返回一个字典。要在 C 中做到这一点需要做更多的工作,但它使用起来很干净并且没有外部依赖。


mar*_*ssi 5

我很惊讶没有人提出詹姆斯·泰勒(James Theiler)的“选择”方案。

您可以在http://public.lanl.gov/jt/Software/找到opt。

这里有一些讨人喜欢的例子,比起其他方法,它更简单得多:

http://www.decompile.com/not_invented_here/opt/

  • opt 没有被积极维护,因为它是完整和紧凑的。对于踢球,我刚刚下载并尝试构建它(gcc-7.3)并发现该库构建并工作,但 C++ 测试可以做一些小工作。iostream.h 应该变成 iostream,并使用命名空间 std;应该添加。我会告诉詹姆斯的。这仅影响 C++ API 测试,而不影响代码本身。 (3认同)
  • @cat是什么让您认为此后需要进行更新?那只是对软件的错误态度。 (2认同)

Pod*_*Pod 0

#include <stdio.h>

int main(int argc, char **argv)
{
    size_t i;
    size_t filename_i = -1;

    for (i = 0; i < argc; i++)
    {
        char const *option =  argv[i];
        if (option[0] == '-')
        {
            printf("I am a flagged option");
            switch (option[1])
            {
                case 'a':
                    /*someting*/
                    break;
                case 'b':
                    break;
                case '-':
                    /* "--" -- the next argument will be a file.*/
                    filename_i = i;
                    i = i + 1;
                    break;
                default:
                    printf("flag not recognised %s", option);
                    break;
            }
        }
        else
        {   
            printf("I am a positional argument");
        }

        /* At this point, if -- was specified, then filename_i contains the index
         into argv that contains the filename. If -- was not specified, then filename_i will be -1*/
     }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 不; 绝对不是一个好方法...使用参数解析函数之一 - `getopt()` 或 `getopt_long()`。 (5认同)
  • 听起来像是作弊,因为这显然是一个家庭作业问题。此外,OP 很难理解字符串是什么以及如何读取字符串的各个部分。将 getopts 强加给他是一个错误。 (5认同)

归档时间:

查看次数:

133456 次

最近记录:

6 年,2 月 前