Command Line Arguments in C++
In C++, the command line arguments are used to provide the input parameters and regulate the runtime behavior of a program. They offer several benefits and use cases. By providing the arguments during program execution, the users can tailor the program to their specific needs. They enable the users to pass the input data to a program. For example, you can provide a file name as an argument which specifies the file to be processed by the program.
By utilizing the command line arguments, the C++ programs become more flexible, versatile, and suitable for various scenarios. They allow the users to customize the program behavior, provide the input data, configure the options, and automate the processes, enhancing the overall usability and usefulness of the program.
This post discusses some of the clean ways to retrieve the command line arguments in C++.
Method 1: Using Argc and Argv
In C++, argc and argv are commonly used parameters in the main function to handle the command line arguments.
Here’s an example to illustrate the usage of argc and argv:
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::vector<std::string> arguments(argv + 1, argv + argc);
std::cout << "Program name: " << argv[0] << std::endl;
if (!arguments.empty()) {
for (const std::string& arg : arguments) {
std::cout << "Argument: " << arg << std::endl;
}
} else {
std::cout << "No command line arguments provided." << std::endl;
}
return 0;
}
The code includes the <iostream> header which provides the functionality for input and output operations. Two arguments define the main function: int argc and char* argv[]. The “argc” indicates how many command line arguments are given including the program name as opposed to the command line parameters which are stored in the “argv” array of character pointers.
Inside the main() function, the if-statement is used. The code checks if “argc” is greater than 1 to determine if at least one command line argument is provided (excluding the program name). If this condition is true, it means that there are command line arguments. Inside the “if” block, a “for” loop is used to iterate through the command line arguments. The loop starts from index 1 (i = 1) because argv[0] contains the program name. It continues until “i” is less than “argc”. This indicates how many command line parameters there are.
The “std::cout” object is utilized within the loop to output each command line parameter. The << operator is used to concatenate the “Argument” strings, the value of i, “: “, and the argv[i] string. Finally, “std::endl” is used to insert a newline character. In case that no command line parameters are given (i.e., argc is 1), the code executes the “else” block and prints the “No command line arguments provided” message.
Lastly, the “return 0;” statement shows that the program runs effectively.
The generated output can be viewed in the following snapshot:
Method 2: Using the Std::vector<std::string>
Another way to handle the command line arguments cleanly is by storing them in a std::vector<std::string>. This approach allows you to easily manipulate and access the arguments as strings.
The code that we provided in the following demonstrates the ways to retrieve and process the command line arguments in C++. Let’s go through it step by step:
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv + 1, argv + argc);
if (!args.empty()) {
for (size_t i = 0; i < args.size(); i++) {
std::cout << "Argument " << i + 1 << ": " << args[i] << std::endl;
}
} else {
std::cout << "No Parameters provided." << std::endl;
}
return 0;
}
Beginning with the program, we initially include the necessary header files like <iostream>, <string>, and <vector>.
The main method takes two arguments: int argc and char* argv[]. Inside the main function, a std::vector<std::string> named “argument” is created. The std::vector<std::string> declares a vector container that stores the strings. In this case, it holds the command line arguments. The “args” is the name of the vector that is being created. The (argv + 1, argv + argc) specifies the range of values to be used for initialization. Here, “argv + 1” represents the starting position of the range and “argv + argc” represents the ending position (exclusive).
Using “argv + 1”, we skip the first element of the “argv” array which typically represents the program name, and start populating the vector with the command line arguments. Whereas the “argc” variable represents the number of the command line arguments, so “argv + argc” points to the position just after the last command line argument in the “argv” array. Therefore, this line of code creates the “args” vector and initializes it with the command line arguments that are provided in the “argv” array, excluding the program name.
The “if (!ar.empty()) {“ checks if the “args” vector is not empty. The “!” operator negates the result of args.empty(), so the condition is true if “args” is not empty. Inside the “if” block, a “for” loop is used to iterate over the elements of the vector. It starts from index 0 and continues until “i” is less than args.size() which represents the number of command line arguments that are stored in the vector.
Then, we have the std::cout << “Argument ” << i + 1 << “: ” << args[i] << std::endl; code line which outputs each command line argument along with its index. The “std::cout” object is used to print the “Argument ” strings, the value of “i + 1” (to display the index starting from 1), “: “, and the value of args[i] (the command line argument itself). If the “args” vector is empty, it means that no command line arguments are provided and the code inside the “else” block is executed. Whereas, inside the “else” block, the std::cout << “No Parameters provided.” << std::endl; outputs a message which indicates that no command line arguments are given.
To summarize, this code retrieves the command line arguments and stores them in a std::vector<std::string>. It then outputs the program name and each command line argument. If there are no command line parameters, it displays a message which indicates the absence of the command line arguments.
The produced output is as follows:
Conclusion
Retrieving the command line arguments in C++ is the discussion topic for this post. Two methods are provided to achieve this. The first method involves the utilization of “argc” and “argv” for the retrieval of the command line argument. Moreover, we also elaborated on another method which uses the std::vector<std::string> in C++ for command line arguments. Each line of code is thoroughly explained to help the reader comprehend the implemented programs.