Getopt- Passing string parameter for argument

前端 未结 2 1841
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 18:33

I have a program which takes in multiple command line arguments so I am using getopt. One of my arguments takes in a string as a parameter. Is there anyway to obtain that st

相关标签:
2条回答
  • 2020-12-23 18:48

    Read man getopt http://linux.die.net/man/3/getopt

    optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned in optarg, otherwise optarg is set to zero.

    A sample code:

    #include <stdio.h>
    #include <unistd.h>
    
    int main (int argc, char *argv[])
    {
      int opt;
      while ((opt = getopt (argc, argv, "i:o:")) != -1)
      {
        switch (opt)
        {
          case 'i':
                    printf ("Input file: \"%s\"\n", optarg);
                    break;
          case 'o':
                    printf ("Output file: \"%s\"\n", optarg);
                    break;
        }
      }
      return 0;
    }
    

    Here in the optstring is "i:o:" the colon ':' character after each character in the string tells that those options will require an argument. You can find argument as a string in the optarg global var. See manual for detail and more examples.

    For more than one character option switches, see the long options getopt_long. Check the manual for examples.

    EDIT in response to the single '-' long options:

    From the man pages

    getopt_long_only() is like getopt_long(), but '-' as well as "--" can indicate a long option. If an option that starts with '-' (not "--") doesn't match a long option, but does match a short option, it is parsed as a short option instead.

    Check the manual and try it.

    0 讨论(0)
  • 2020-12-23 18:52

    To specify that a flag requires an argument, add a ':' right after the flag in the short_opt variable. To use long arguments, use getopt_long().

    Here is a quick example program:

    #include <getopt.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[]);
    
    int main(int argc, char * argv[])
    {
       int             c;
       const char    * short_opt = "hf:";
       struct option   long_opt[] =
       {
          {"help",          no_argument,       NULL, 'h'},
          {"file",          required_argument, NULL, 'f'},
          {NULL,            0,                 NULL, 0  }
       };
    
       while((c = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1)
       {
          switch(c)
          {
             case -1:       /* no more arguments */
             case 0:        /* long options toggles */
             break;
    
             case 'f':
             printf("you entered \"%s\"\n", optarg);
             break;
    
             case 'h':
             printf("Usage: %s [OPTIONS]\n", argv[0]);
             printf("  -f file                   file\n");
             printf("  -h, --help                print this help and exit\n");
             printf("\n");
             return(0);
    
             case ':':
             case '?':
             fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
             return(-2);
    
             default:
             fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
             fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
             return(-2);
          };
       };
    
       return(0);
    }
    
    0 讨论(0)
提交回复
热议问题