Skip to main content

Command Palette

Search for a command to run...

How to Use Command Line Arguments Effectively

Published
3 min read

The most basic command-line argument program is

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  return 0;
}

To run this program, we can follow the usual steps for running a C++ program.

What does this program do? It simply demonstrates where and how to write command-line arguments.

Why use command-line arguments?

We need command-line arguments to automate tasks. For example, if you want to start a service like systemd does when the OS boots, you need to specify all the options for the commands. Relying on user input for this would be impractical, so command-line arguments were introduced to handle these tasks efficiently.

ls -a

Here, ls is an executable file, and -a is a command-line argument. Imagine if we had to provide -a as user input instead of a command-line argument. It would be chaotic due to the inefficiency in automating tasks.

What do argc and argv mean?

  • argc: Argument count, it tells us the total number of arguments.

  • argv: It provides the values of all the arguments present.

Now, a basic question: what would you get when you print argc directly?

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  cout << "The value of argc: " << argc << "\n";
  return 0;
}

What would be the output of the above program?

Think before looking at the answer to this question.

The value of argc: 1

Why is the value of this program 1?

When we run this program...

./main

We have exactly one argument we are providing, so the value of argc is 1.

Now, let's print the argv values too.

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  cout << "The value of argc: " << argc << "\n";

  for (int i{0}; i < argc; i++) {
    cout << "argv[" << i << "]: " << argv[i] << "\n";
  }
  return 0;
}

So, what output would we get here?

The value of argc: 1
argv[0]: ./build/main

From this, we can understand that argc is like the length of an array, and argv is like a list that holds all the values entered in the terminal when running a program (to keep it simple).

Now, let's say that we did

ls "-a"

Instead of running the program with just ./main, if we run it with additional arguments like ./main -a, the value of argc would change. argc would now be 2, reflecting the two arguments: ./main and -a. The argv array would hold both these values, showing how command-line arguments are passed to a program.

ls -a

What would happen?

Try it in your terminal and see what happens.

The short answer is that we will get the same output as we would for ls -a.

Now, what about this one?

ls '-a'

Again, it gave us the same output. Why?

Is this behavior acceptable?

The answer is yes. This is how argv takes the values. Whether the arguments are in single quotes or double quotes, they work the same way.

For the last program, let's see how it would look with several arguments, using both single and double quotes.

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  cout << "The value of argc is: " << argc << "\n";

  for (int i{0}; i < argc; i++) {
    cout << "argv[" << i << "]: " << argv[i] << "\n";
  }
  return 0;
}

For this program, I ran it like this:

./build/main hello 4 "hello" 'hello' 3.2

Here, we have a variety of types, and this is the output we get:

The value of argc is: 6
argv[0]: ./build/main
argv[1]: hello
argv[2]: 4
argv[3]: hello
argv[4]: hello
argv[5]: 3.2

This demonstrates how to handle command-line arguments and use them in our program.