argc/argv P.J. Drongowski 17 March 2005 ******************************************** Command line arguments ******************************************** int main(int argc, char *argv[]) { ... } * The operating system and C++ runtime environment set-up the arguments to the function main * By convention, the arguments are called "argc" and "argv" + argc: Number of command line arguments including the program name + argv: Contains pointers to the individual arguments * The individual arguments are represented as strings; argv is a one dimensional array of char pointers where each pointer points to an individual argument * Example: g++ -c file.cpp argc = 3 argv[0] -> "g++" argv[1] -> "-c" argv[2] -> "file.cpp * UNIX conventions + Options: '-' followed by a single character + An option may take a take a parameter which follows the option character + Options usually appear before other arguments on command line, but this is not always required * The main function usually processes the command line * Idiom (from Practical C++ Programming) while (argc > 1) { if (argv[1][0] == '-') { switch( argv[1][1]) { case 'v': { ... break ; } case 'u': { ... break ; } default: { cerr << "Unknown option: " << argv[1][1] << endl ; break ; } } } ++argv ; --argc ; } * The standard library includes a C language routine called "getopt" which assists command line processing (See "man 3 getopt") * There is also a C++ version of the GNU getopt function called "GetOpt" *************************************************** simple.cpp *************************************************** #include #include #include using namespace std ; int main(int argc, char *argv[]) { ifstream inputFile ; if (argc != 2) { cerr << "Too few arguments" << endl ; cerr << "Usage: simple file" << endl ; exit(EXIT_FAILURE) ; } inputFile.open(argv[1], ios::in) ; if (! inputFile) { cerr << "*error* Couldn't open file: " << argv[1] << endl ; cerr << "Usage: simple file\n" ; exit(EXIT_FAILURE) ; } inputFile.close() ; exit(EXIT_SUCCESS) ; } *************************************************** options.cpp *************************************************** #include #include #include using namespace std ; void printUsage() { cerr << "Usage: options [-aio] file" << endl ; } int main(int argc, char *argv[]) { ifstream inputFile ; bool a_option = false, i_option = false, o_option = false ; char *option ; int count ; if (argc < 2) { cerr << "Too few arguments" << endl ; printUsage() ; exit(EXIT_FAILURE) ; } for (count = 1 ; count < argc ; count++) { if (argv[count][0] == '-') { switch( argv[count][1] ) { case 'a' : { cout << "Option a\n" ; a_option = true ; break ; } case 'i' : { cout << "Option i\n" ; i_option = true ; break ; } case 'o' : { cout << "Option o\n" ; o_option = true ; break ; } default : { cerr << "Unknown option: " << argv[count] << endl ; break ; } } } else { break ; } } if (count == argc) { cerr << "Missing file name" << endl ; printUsage() ; exit(EXIT_FAILURE) ; } inputFile.open(argv[count], ios::in) ; if (! inputFile) { cerr << "*error* Couldn't open file: " << argv[count] << endl ; printUsage() ; exit(EXIT_FAILURE) ; } inputFile.close() ; exit(EXIT_SUCCESS) ; }