Thread: command line options

  1. #1
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186

    command line options

    I want to make a program where you have options when you run the program, for example:

    your at the dos prompt:

    D:\program -f -h
    D:\program -f index.htm

    Something like that. is there a site that gives a tutorial on how to do that. Thanks!
    the best things in life are simple.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Not sure about a tut, but do a search for Argc & Argv, they're not too hard to use. If you have any books on C, just have a look for them in the index. If you're still stuck I'll post you some code (or I'm sure someone else will).

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Declare your main function to pass two parameters, int argc and char *argv[]. You can then use the strings in argv that you passed from the command line.

    C:\programName arg1 arg2 arg3
    When you start your program use this line:

    int main ( int argc, char *argv[] )

    From here argv holds 4 strings, "programName", "arg1", "arg2", and "arg3". You can now use them for processing in your program.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Thanks! I found some things, but could you still post some code? Thanks!
    the best things in life are simple.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A program that takes your first and last name as arguments and prints them to the screen.
    Code:
    // Pseudocode
    #include <iostream>
    using namespace std;
    
    int main ( int argc, char **argv )
    {
      if ( argc == 3 ) {
        cout<<"Your first name is "<<argv[1]<<endl;
        cout<<"Your last name is "<<argv[2]<<endl;
      }
      return EXIT_SUCCESS;
    }
    Input:
    C:\program Julienne Walker

    Output:
    Your first name is Julienne
    Your last name is Walker

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Thanks everyone! That helped!
    the best things in life are simple.

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, 01: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, 04:06 PM
  3. Options Dialog
    By Morgul in forum Game Programming
    Replies: 3
    Last Post: 11-16-2005, 12:15 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM