Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Parse Command Line Arguments in C++


Arrays, C++, Strings
David Egan

Command line arguments are available by adding two parameters

Run Programme: ./prog -a -b -cde

This passes four arguments to the programme:

  • ./prog
  • -a
  • -b
  • -cde

This data is available within main() as the second input parameter (by convention, argv[]). The first input parameter to main holds the number of passed arguments (in this case, four) and by convention is called argc.

The argv[] parameter is a C-style array of primitive strings (char * objects)- and may be represented as char * argv[] or char ** argv.

Note that main(argc, char ** argv) and main(argc, char * argv[]) are equivalent:

  • char ** argv: declare argv as pointer to pointer to char (A pointer to char *)
  • char * argv[]: declare argv as array of pointer to char (A primitive array of char * objects)

They are equivalent because a pointer to char * is the same as an array of char *.

In the current example, argc is 4 and the four elements of argv are pointers to the arrays of characters passed in as arguments.

          +---+         +---+---+---+---+---+---+---+
argv ---->| 0 |-------->| . | / | p | r | o | g | \0|
          +---+         +---+---+---+---+---+---+---+
          | 1 |-------->| - | a | \0|
          +---+         +---+---+---+
          | 2 |-------->| - | b | \0|
          +---+         +---+---+---+---+---+
          | 3 |-------->| - | c | d | e | \0|
          +---+         +---+---+---+---+---+
          | 4 |--------> NULL
          +---+

Parse Command Line Arguments: Demo

#include <iostream>

/**
 * Demo of parsing command-line argument array.
 */
int main(int argc, char **argv)
{
    char c;
    const char * prog = argv[0];

    // Extract arguments from argument array.
    // -------------------------------------------------------------------------
    // Because of the parentheses, argv (a pointer) is incremented first, then
    // dereferenced. The loop conditional is also dependent on the first element
    // of the new pointed-to element being the char '-', which denotes the start
    // of an option. Note that argv is incremented first, which excludes the first
    // element (the command).
    while((++argv)[0] && argv[0][0] == '-')
    {
        // Set appropriate actions
        // ---------------------------------------------------------------------
        // Walk down the string (char array) pointed to by the current argv[0]
        // pointer. The expression `*++(argv[0])` increments argv[0] and provides
        // a pointer to the first non '-' character. Dereferences the pointer to
        // provide the value. Loops to the end of the end of the char array, so
        // multiple options can be read (i.e. `command -abc`).
        // `while(c = *++argv[0])` is the same as `while((c = *++argv[0]) != 0)`
        while(c = *++argv[0])
        {
            switch(c) {
                case 'a':
                std::cout << "Set flag a" << std::endl; break;
                case 'b':
                std::cout << "Set flag b" << std::endl; break;
                case 'c':
                std::cout << "Set flag c" << std::endl; break;
                case 'd':
                std::cout << "Set flag d" << std::endl; break;
                case 'e':
                std::cout << "Set flag e" << std::endl; break;
                case 'h':
                std::cout << "Provide some help!" << std::endl; break;
                default:
                std::cout << "Option " << c << " is unknown." << std::endl;
                std::cout << "usage: " << prog << " <-abcde -h>" << std::endl;
                return 1;
            }
        }
    }
    return 0;
}

comments powered by Disqus