Command line options???

This is a discussion on Command line options??? within the C Programming forums, part of the General Programming Boards category; How do I define the options I want to get from the command line? Any ideas?...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1

    Command line options???

    How do I define the options I want to get from the command line? Any ideas?

  2. #2
    Registered User
    Join Date
    May 2006
    Location
    Troy
    Posts
    14
    You can specify the names of the array which stores command line arguments in your definition of the main procedure. E.g.
    Code:
    int main(int argc, char ** argv) { ...
    Then argc tells you how many arguments you have, and argv is an array of memory addresses, which point to individual command line argument strings. So if you run a program with the command
    Code:
    ./foo bar a b -c
    then argc will be 5, and argv[0], argv[1], ..., argv[4] will point to the charactar arrays "./foo", "bar", "a", "b", and "-c", respectively. Yes, the program name gets passed as the first command line argument, and it's usually ignored.

    You can define the options you want by writing code that does things, like complaining about missing options and exiting, based on the command line arguments.

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Note: argv[0] on Windows will point to the absolute path of the exectuable whereas in unix argv[0] will be how the program was called.

  4. #4
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Check this method out

    I got this method from the supertux source code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
      /* Parse command line arguments */
      int i;
      for (i = 1;i < argc, ++i)
      {
        if (strcmp(argv[i], "--test") == 0)
        {
          printf("Test Succesful\n");
          printf("The string you entered is %s\n", argv[++i]);
        }
        else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
        {
          printf("Usage %s [options]\n", argv[0]);
          printf("Options:\n");
          printf("  --test str     Display the the value str\n");
          printf("  -h, --help     Display this help message\n");
        }
        else
        {
          printf("Unknown option %s\n", argv[i]);
          printf("Use %s --help for a list of command line arguments\n", argv[0]);
          return -1;
        }
      }
      /* End of Command Line Parser */
      return 0;
    }
    Just add whatever commands you want the user to enter.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by OnionKnight
    Note: argv[0] on Windows will point to the absolute path of the exectuable whereas in unix argv[0] will be how the program was called.
    Actually it may point to it. In fact, it probably will point to it. However, it is not guarinteed. If the name is not available, argv[0][0] will be '\0'.


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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Can argv[0] be NULL? (Instead of pointing to ""?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If argc is 0, given that argv[argc] is a null pointer, then argv[0] would be a null pointer.

    http://web.archive.org/web/200502070...t.html#2.1.2.2
    If they are defined, the parameters to the main function shall obey the following constraints:

    * The value of argc shall be nonnegative.

    * argv[argc] shall be a null pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Okay, thanks. I always use
    Code:
    print_usage(argc ? argv[0] : "*unknown*");
    but I see I don't have to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 02:24 PM
  2. What's the best way to handle many program options?
    By stickmangumby in forum C Programming
    Replies: 19
    Last Post: 06-06-2007, 05:06 PM
  3. Explorer options
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 04-02-2005, 09:23 AM
  4. Disabling the main frame menu options
    By MPC_Engr in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2003, 09:04 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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