Thread: Command line input

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Command line input

    Hi,
    I don't know how to fix this:

    input: prog -a-a
    output:
    a
    prog: unknown option -- -
    a

    Code:
    while (( c = getopt (argc, argv, "ab")) != -1)
     {
        switch (c)
        {
           case 'a':
           printf ("a...\n");
           break;
    
           case 'b':
           printf("b...\n");
           break;
    
           case '?':
           default:
           usage();
           break;
        }
     }

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Dont u think there's something wrong with using argc and argv that way...
    main() takes in two arguments.One is an integer that specifies the number of arguments at the prompt
    and the other argument is a char *argv[] in which each element of the array is a pointer to a different char array....

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by kris.c
    Dont u think there's something wrong with using argc and argv that way...
    main() takes in two arguments.One is an integer that specifies the number of arguments at the prompt
    and the other argument is a char *argv[] in which each element of the array is a pointer to a different char array....
    so...

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I think that the usage of getopt() is correct ...

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, but u r giving

    prog -a-a.. at the prompt,arent u?? and if u r just specifying the argument what is that " - " doing there?

  6. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    ok , I understand ur problem, this is what u might have intended to do :

    prog -a -a

    this gives u the reqd o/p..
    Last edited by kris.c; 07-20-2006 at 05:37 AM.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by kris.c
    ok , I understand ur problem, this is what u might have intended to do :

    prog -a -a

    this gives u the reqd o/p..
    I am working with all possibilities that the user may write:
    prog -a-a -- which displays what I wrote in my first post
    How to cover this situation?

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    No help!

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    getopt() is designed to be used a particular way. If you want to parse command line options in some other fashion then you should create your own parser.

    IMHO, telling the user that -a-a isn't valid is handling it.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Thanks. I got what you mean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM