C++: Command-Line Arguments

Bjarne-stroustrup
 

Retrieve the list of command-line arguments given to the program. For programs that only print the arguments when run directly, see Scripted main.

See also Program name.

For parsing command line arguments intelligently, see Parsing command-line arguments.

Example command line:

myprogram -c "alpha beta" -h "gamma"

Command line arguments are passed the same way as in C.
This example uses iostream. Traditional C I/O also works.

#include <iostream>

int main(int argc, char* argv[])
{
	std::cout << "This program is named " << argv[0] << std::endl;
	std::cout << "There are " << argc-1 << " arguments given." << std::endl;
	for (int i = 1; i < argc; ++i)
	std::cout << "the argument #" << i << " is " << argv[i] << std::endl;

	return 0;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.