Checking for flag input through argv

This is a discussion on Checking for flag input through argv within the C Programming forums, part of the General Programming Boards category; I'm trying to figure how the best way of checking for certain flags. I have a program that can be ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Checking for flag input through argv

    I'm trying to figure how the best way of checking for certain flags. I have a program that can be ran standard without the -a or -b flag. If the user wants to run the -a flag, he/she must include 4 values after it. The -a flag can be called anywhere but must have the 4 values follwoing the call. The -b flag can be called anywhere as long as it does not conflict with flag -a option. A filename can also be included. I know argc counts the number of params main starts with. If certain flags are called, there should be certain checks to make sure the correct amount of input is included. I'm not quite sure how to check for this right now. I haven't worked with multiple arguments from main before and checking for all cases. Any help would be great. Thank you. Here is just a sample program of what I tested so far to see what values are stored where. You can assumed argv[0] is the calling program, so values from arg[1] and above. At most argc can at most 7. Any more than that would be terminiation of the program.


    Code:
    #include <stdio.h>
    
    main(int argc, char** argv)
    {
        int i;
    
        printf("argc = %d\n", argc);
    
        for (i = 0; i < argc; i++)
            printf("argv[%d] = \"%s\"\n", i, argv[i]);
    
    
        printf("\n");
    
        // form of input
        // prog fname -a val1 val2 val3 val4
        // or
        // prog -a val1 val2 val3 val4 fname
        //
        // also -b flag can be called and it can be placed anywhere
    
       return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I doubt you want "-b" just anywhere. But in the event you actually do, it's still a simple task:
    Code:
    while not done:
        pull off an argument
        test to see if it's "-b"
            if so, set some variable saying that -b has been found
        test to see if it's "-a"
            if so, fill values 1 through 4 with the next 4 or 5 values:
                pull off an argument
                test to see if it's "-b"
                    if not, fill it in valueN
                    if so, set the "-b found" variable
    You can stream-line this a bit, but that's the basics of it. All you have to do is think it through.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    make your algorithm to check for '-' char that will let your prog know that the next char is a flag a or b. You need to check every char in the command. switchcase() would work. there are many ways.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Is this statement legal to check for the actual argument?

    if(argv[2] == "-a")

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You'll need strcmp to compare strings. You might want to also look into strstr and some of the other string functions.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Would this work?

    Code:
    for (i = 1; i < argc; i++) {
    
            /* Check for a switch (leading "-"). */
    
            if (argv[i][0] == '-') {
    
                /* Use the next character to decide what to do. */
    
                switch (argv[i][1]) {
    
                    case 'a':
                            printf("flag a was called\n");
                            break;
    
                    case 'b':
                            printf("flag b was called\n");
                            break;
                }
            }
        }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > Would this work?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Checking to see if the input has all of the compare line
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2003, 05:12 PM
  4. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 10:00 AM

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