Parsing command-line arguments in C?

后端 未结 12 793
眼角桃花
眼角桃花 2020-11-22 16:23

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

12条回答
  •  無奈伤痛
    2020-11-22 17:09

        /*
          Here's a rough one not relying on any libraries.
          Example:
          -wi | -iw //word case insensitive
          -li | -il //line case insensitive
          -- file  //specify the first filename (you could just get the files
          as positional arguments in the else statement instead)
          PS: don't mind the #define's, they're just pasting code :D
        */
        #ifndef OPT_H
        #define OPT_H
    
        //specify option requires argument
        #define require \
          optarg = opt_pointer + 1; \
          if (*optarg == '\0') \
          { \
            if (++optind == argc) \
              goto opt_err_arg; \
            else \
              optarg = argv[optind]; \
          } \
          opt_pointer = opt_null_terminator;
    
        //start processing argv
        #define opt \
        int   optind                 = 1; \
        char *opt_pointer            = argv[1]; \
        char *optarg                 = NULL; \
        char  opt_null_terminator[2] = {'\0','\0'}; \
        if (0) \
        { \
          opt_err_arg: \
            fprintf(stderr,"option %c requires argument.\n",*opt_pointer); \
            return 1; \
          opt_err_opt: \
            fprintf(stderr,"option %c is invalid.\n",*opt_pointer); \
            return 1; \
        } \
        for (; optind < argc; opt_pointer = argv[++optind]) \
          if (*opt_pointer++ == '-') \
          { \
            for (;;++opt_pointer) \
              switch (*opt_pointer) \
              {
    
        //stop processing argv
        #define done \
              default: \
                if (*opt_pointer != '\0') \
                  goto opt_err_opt; \
                else \
                  goto opt_next; \
                break; \
              } \
            opt_next:; \
          }
        #endif //opt.h
    
        #include 
        #include "opt.h"
        int
        main (int argc, char **argv)
        {
          #define by_character 0
          #define by_word      1
          #define by_line      2
          int cmp = by_character;
          int case_insensitive = 0;
          opt
          case 'h':
            puts ("HELP!");
            break;
          case 'v':
            puts ("fileCMP Version 1.0");
            break;
          case 'i':
            case_insensitive = 1;
            break;
          case 'w':
            cmp = by_word;
            break;
          case 'l':
            cmp = by_line;
            break;
          case '-':required
            printf("first filename: %s\n", optarg);
            break;
          done
          else printf ("Positional Argument %s\n", argv[optind]);
          return 0;
        }
    

提交回复
热议问题