解析命令行参数? [英] Parsing command-line arguments?

查看:148
本文介绍了解析命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试着写一个程序,可以通过字符C.比较两个文件,​​一行行,一字一句,或字符它必须能够在命令行选项-l -i -w阅读要么 - ...
如果选项是-l它由行文件行进行比较。
如果选项是-w它由字的文件字进行比较。
如果选择是 - 它会自动假定下一arg是第一个文件名。
如果选项-i它不区分大小写的方式比较他们。
否则,它的默认值由字符文件的字符比较

它不应该无关紧要的选项多少时间被输入,只要-w和-l不会在同一时间输入的,并且没有多于或少于2个文件

我甚至不知道在哪里解析命令行参数开始。
请帮助:(

所以这就是我想出了一切code。我没有错误检查了相当尚未,但如果我在过于复杂的方式写的东西我不知道?

  / *
 *功能比较文件。
 * /
INT compare_line();
INT compare_word();
INT compare_char();
诠释case_insens();/ *
 *程序来比较两个文件中的信息和打印信息说
 *这是否是成功的。
 * /
INT主(INT ARGC,CHAR *的argv [])
{
/ *循环计数器* /
  为size_t I = 0;  / *对函数的变量* /
  INT酪= 0;
  INT行= 0;
  INT字= 0;  / *文件指针* /
  FILE * FP1,FP2 *;  / *
   *通过命令行参数选项阅读。
   * /
  对于(i = 1; I< ARGC,我++){
    的printf(的argv [%U =%S \\ n,我的argv [I]);
    如果(的argv [I] [0] ==' - '){
       如果(的argv [I] [1] =='我')
       {
           酪蛋白= 1;
       }
       如果(的argv [I] [1] =='L')
       {
           线= 1;
       }
       如果(的argv [I] [1] =='W')
       {
           字= 1;
       }
       如果(的argv [I] [1] ==' - ')
       {
           FP1 =的argv [I] [2];
           FP2 = argv的[I] [3];
       }
       其他
       {
           的printf(无效选项。);
           返回2;
       }
    }其他{
       FP1(的argv [I]);
       FP2(的argv [I] [1]);
    }
  }  / *
   *检查文件可以打开。
   * /
  如果(((FP1 = FOPEN(FP1,RB))== NULL)||((FP2 = FOPEN(FP2,RB))== NULL))
  {
      PERROR(fopen()函数);
      返回3;
  }
  其他{
        如果(酪蛋白== 1)
        {
            如果(线== 1和;&放大器;字== 1)
            {
                的printf(这是无效的。);
                返回2;
            }
            如果(行== 1安培;&安培;字== 0)
            {
                如果(compare_line(case_insens(FP1,FP2))== 0)
                        返回0;
            }
            如果(行== 0安培;&安培;字== 1)
            {
                如果(compare_word(case_insens(FP1,FP2))== 0)
                    返回0;
            }
            其他
            {
                如果(compare_char(case_insens(FP1,FP2))== 0)
                    返回0;
            }
        }
        其他
        {
            如果(线== 1和;&放大器;字== 1)
            {
                的printf(这是无效的。);
                返回2;
            }
            如果(行== 1安培;&安培;字== 0)
            {
                如果(compare_line(FP1,FP2)== 0)
                    返回0;
            }
            如果(行== 0安培;&安培;字== 1)
            {
                如果(compare_word(FP1,FP2)== 0)
                    返回0;
            }
            其他
            {
                如果(compare_char(FP1,FP2)== 0)
                    返回0;
            }
        }  }
    返回1;
    如果(((FP1 = FCLOSE(FP1))== NULL)||(((FP2 = FCLOSE(FP2))== NULL)))
        {
            PERROR(FCLOSE());
            返回3;
        }
        其他
        {
            FP1 = FCLOSE(FP1);
            FP2 = FCLOSE(FP2);
        }
}/ *
 *功能来比较两个文件线由行。
 * /
INT compare_line(FILE * FP1,FILE * FP2)
{
    / *缓冲变量,以存储文件中的行* /
    烧焦BUFF1 [LINESIZE]
    烧焦bu​​ff2 [LINESIZE]    / *检查,无论是文件*年底/
    而((的feof(FP1))及!&安培;!(的feof(FP2)))
    {
        / *一行通过文件走行* /
        与fgets(BUFF1,LINESIZE,FP1);
        与fgets(buff2,LINESIZE,FP2);
    }
    / *按行*比较文件线/
    如果(STRCMP(BUFF1,buff2)== 0)
    {
        的printf(文件相等\\ n);
        返回0;
    }
    的printf(文件不相等\\ n);
    返回1;
}/ *
 *功能来比较两个文件字的字。
 * /
INT compare_word(FILE * FP1,FILE * FP2)
{
    / *文件指针* /
    FILE * FP1,FP2 *;    / *数组存储字* /
    焦炭fp1words [LINESIZE]
    焦炭fp2words [LINESIZE]    如果(的strtok(FP1,)== NULL ||的strtok(FP2,)== NULL)
    {
        的printf(文件是空的不能比的\\ n。);
        返回0;
    }
    其他
    {
        fp1words = strtok的(FP1,);
        fp2words = strtok的(FP2,);        如果(fp1words == fp2words)
        {
            的fputs(fp1words);
            的fputs(fp2words);
            的printf(文件相等\\ n);
            返回0;
        }
    }
    返回1;
}/ *
 *功能由性格比较两个文件的字符。
 * /
INT compare_char(FILE * FP1,FILE * FP2)
{
    / *变量的字符存储两个文件* /
    INT℃;
    INT D组;    / *缓冲区变量来存储字符* /
    烧焦BUFF1 [LINESIZE]
    烧焦bu​​ff2 [LINESIZE]    而(((C =龟etc(FP1))= EOF)及!&放大器;!(((D =龟etc(FP2))= EOF)))
    {
        如果(C = D)
        {
            如果((的fscanf(FP1,%C,BUFF1))==(的fscanf(FP2,%C,buff2)))
            {
                的printf(文件具有同等的字符\\ n);
                返回1;
                打破;
            }
        }    }
        返回0;
}/ *
 *功能在不区分大小写的方式比较两个文件。
 * /
INT case_insens(FILE * FP1,FILE * FP2,为size_t N)
{
    / *的文件指针。* /
    FILE * FP1,FP2 *;    / *变量要经过文件。* /
    为size_t I = 0;    / *数组存储文件信息。* /
    焦炭fp1store [LINESIZE]
    焦炭fp2store [LINESIZE]    而(的feof(FP1)及!&安培;!的feof(FP2))
    {
         对于(i = 0; I< N;我++)
         {
                的fscanf(FP1,%S,fp1store);
                的fscanf(FP2,%S,fp2store);                fp1store = tolower的(fp1store);
                fp2store = tolower的(fp2store);                返回1;
         }
    }
    返回0;
}


解决方案

据我所知,这三个最流行的方式如何解析命令行参数在C是:


  • 的Getopt 的#include< unistd.h中> 从POSIX C库),它可以解决的简单的参数解析的任务。如果你是一个有点熟悉庆典,庆典的getopt的内置基于的Getopt从GNU libc中。

  • Argp 的#include< argp.h> 从GNU C库),它可以解决更多的复杂的任务的和需要照顾的东西一样,例如:

    • - - 帮助帮助信息的,其中的电子邮件地址

    • -V - 版本版本信息

    • - 使用用法消息


  • 做自己动手,我不建议将给予别人的程序,因为有太多可能出错或降低品质。 - 忘记了流行的错误。停止选项解析就是一个例子

GNU C库文件具有的Getopt和Argp一些很好的例子。

使用实施例的的Getopt

 的#include< stdbool.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    布尔isCaseInsensitive = FALSE;
    INT选择;
    枚举{CHARACTER_MODE,WORD_MODE,LINE_MODE}模式= CHARACTER_MODE;    而((选择= getopt的(ARGC,ARGVILW))!= -1){
        开关(OPT){
        案例'我':isCaseInsensitive = TRUE;打破;
        案例'L':模式= LINE_MODE;打破;
        案例'W':模式= WORD_MODE;打破;
        默认:
            fprintf中(标准错误,用法:%s的[-ilw] [文件...] \\ n,argv的[0]);
            出口(EXIT_FAILURE);
        }
    }    //现在OPTIND(声明为extern INT以<&unistd.h中GT)是第一个非选项的参数的索引。
    //如果是> = ARGC,没有非选项参数。    // ...
}

例子使用的 Argp

 的#include< argp.h>
#包括LT&;&stdbool.h GT;为const char * argp_program_version =PROGRAMNAME PROGRAMVERSION
为const char * argp_program_bug_address =< your@email.address>;
静态文档的char [] =你的程序的描述。
静态CHAR args_doc [] =[文件名] ...;
静态结构argp_option选项[] = {
    {线,L,0,0,比较线,而不是字符。},
    {字,W,0,0,比较字代替字符},
    {NOCASE,'我',0,0,比较不区分大小写的,而不是区分大小写的。},
    {0}
};结构参数{
    枚举{CHARACTER_MODE,WORD_MODE,LINE_MODE}模式;
    布尔isCaseInsensitive;
};静态error_t parse_opt(INT键,字符* ARG,结构argp_state *状态){
    结构参数*参数=语句>输入;
    开关(键){
    案例'L':arguments->模式= LINE_MODE;打破;
    案例'W':arguments->模式= WORD_MODE;打破;
    案例'我':arguments-> isCaseInsensitive = TRUE;打破;
    案例ARGP_KEY_ARG:返回0;
    默认:返回ARGP_ERR_UNKNOWN;
    }
    返回0;
}静态结构argp argp = {选项,parse_opt,args_doc,文档,0,0,0};INT主(INT ARGC,CHAR *的argv [])
{
    结构参数的参数;    arguments.mode = CHARACTER_MODE;
    arguments.isCaseInsensitive = FALSE;    argp_parse(安培; argp,的argc,argv的,0,0,&放大器;参数);    // ...
}

示例的做它自己

 的#include< stdbool.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    布尔isCaseInsensitive = FALSE;
    枚举{CHARACTER_MODE,WORD_MODE,LINE_MODE}模式= CHARACTER_MODE;
    为size_t OPTIND;
    为(OPTIND = 1; OPTIND&所述;的argc和放大器;&放大器;的argv [OPTIND] [0] ==' - '; OPTIND ++){
        开关(的argv [OPTIND] [1]){
        案例'我':isCaseInsensitive = TRUE;打破;
        案例'L':模式= LINE_MODE;打破;
        案例'W':模式= WORD_MODE;打破;
        默认:
            fprintf中(标准错误,用法:%s的[-ilw] [文件...] \\ n,argv的[0]);
            出口(EXIT_FAILURE);
        }
    }    // * argv的点到其余非选项参数。
    //如果* argv的是NULL,没有非选项参数。    // ...
}

免责声明:我是新来Argp,示例可能包含错误

Hi I'm trying to write a program that can compare two files line by line, word by word, or character by character in C. It has to be able to read in command line options "-l -w -i or --"... if the option is -l it compares the files line by line. if the option is -w it compares the files word by word. if the options is -- it automatically assumes that the next arg is the first filename. and if the option is -i it compares them in a case insensitive manner. Otherwise it defaults to comparing the files character by character

It's not supposed to matter how many time the options are input as long as -w and -l aren't inputted at the same time and there are no more or less than 2 files.

I don't even know where to begin with parsing the command line arguments. PLEASE HELP :(

So this is the code that I came up with for everything. I haven't error checked it quite yet, but I was wondering if I'm writing things in an overcomplicated manner?

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

解决方案

To my knowledge, the three most popular ways how to parse command line arguments in C are:

  • Getopt (#include <unistd.h> from the POSIX C Library), which can solve simple argument parsing tasks. If you're a bit familiar with bash, the getopt built-in of bash is based on Getopt from the GNU libc.
  • Argp (#include <argp.h> from the GNU C Library), which can solve more complex tasks and takes care of stuff like, for example:
    • -?, --help for help message, including email address
    • -V, --version for version information
    • --usage for usage message
  • Doing it yourself, which I don't recommend for programs that would be given to somebody else, as there is too much that could go wrong or lower quality. The popular mistake of forgetting about '--' to stop option parsing is just one example.

The GNU C Library documentation has some nice examples for Getopt and Argp.

Example for using 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.

    // ...
}

Example for using 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);

    // ...
}

Example for Doing it Yourself

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

    // ...
}   

Disclaimer: I am new to Argp, the example might contain errors.

这篇关于解析命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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