getopt - multiple instances of an option

This is a discussion on getopt - multiple instances of an option within the C Programming forums, part of the General Programming Boards category; if I'm using getopt, and I'm checking for a bunch of options, how do I differentiate between one instance of ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Question getopt - multiple instances of an option

    if I'm using getopt, and I'm checking for a bunch of options, how do I differentiate between one instance of an option and multiple instances?

    // Scan the arguments and set flags.
    opterr = FALSE;
    for (; {
    int option = getopt (argc, argv, "nxyd:@:");
    if (option == EOF) break;
    switch (option) {
    case 'd': user_dictionary = optarg;
    break;
    case 'n': default_dictionary = NULL;
    break;
    // want to check for -x or -xx
    case 'x': STUBPRINTF ("-x\n");
    break;
    case 'y': yy_flex_debug = TRUE;
    break;
    case '@': set_debugflags (optarg);
    if (strpbrk (optarg, "@y")) yy_flex_debug = TRUE;
    break;
    default : eprintf ("%: -%c: invalid option\n", optopt);
    set_exitstatus (EXIT_FAILURE);
    };
    };

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. You including <unistd.h> ?
    2. What do you mean by single or multiple instances?

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    I am including <unistd.h>

    I'm mean I want to check if there is -x or -xx. I want to do different things for those two different cases, but I'm not sure how.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    According to my knowledge as option is an integer and you can only differentiate multiple instances of an integer by dividing and getting remainder and quotients logic....

    In this case, you will have to simplify it like that....

    Problem??

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Why don't you try this..
    Code:
     
    int option = getopt (argc, argv, "nxyd:@:-x:-xx");

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    okay, I'll try it, thank you.

  7. #7
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The easiest way would be:
    Code:
    int c;
    int xflag = 0;
    
    while((c = getopt(argc, argv, "x")) != -1)
    {
      switch(c)
      {
        case 'x':
          xflag++;
          break;
      }
    }
    Now xflag contains the number of times the -x option has been given.

    Note also that getopt() is defined as returning -1 when it's done, not EOF. EOF is generally -1, but need not be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorted Linked List with multiple heads.
    By S15_88 in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 11:01 PM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  4. Classes and Destroyed Instances
    By FWGaming in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2005, 10:02 PM
  5. Disabling multiple instances of a program
    By xds4lx in forum Windows Programming
    Replies: 6
    Last Post: 03-06-2002, 01:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21