8.2.4. Command Line Arguments

Time: 00:05:28 | Download: Large, Large (CC) Small | Streaming Streaming (CC) | Slides (PDF)

Command-line arguments were a staple of the old command-line interfaces (CLI) of DOS, Unix, and similar operating systems. Although most users now interact with computers through a graphical user interface (GUI), most modern operating systems still provide a CLI. Windows has two CLIs: the "Command Prompt" and the "Power Shell," and Linux has a "Terminal." A full command line includes a command's name (often a program a user wishes to run) and some optional command arguments. Suppose we rewrite the name_box program from the last demonstration as a CLI program. The command line would give the name of the program and could provide the full name around which the user wants to draw the box:

A screen capture of a Windows Command prompt window. The window displays a string of characters, the prompt, indicating that it is ready for user input. In this screen capture, the user has entered the command and arguments name_box Cranston Q. Snort A screen capture of a Linux terminal window. The window displays a string of characters, the prompt, indicating that it is ready for user input. In this screen capture, the user has entered the command and arguments name_box Cranston Q. Snort
Windows cmd.exeLinux MATE Terminal
Command line windows. Contemporary desktop operating systems typically implement command-line interfaces (CLI) as windows, whose appearances are remarkably similar. Each command line has a command name and zero or more command arguments. The CLI parses the command into its constituent parts or tokens based on the intervening white space or delimiter characters. Both examples illustrate a command line where name_box is the command (i.e., the program) that the user wishes to run, and Cranston, Q., and Snort are the command arguments (i.e., the data input).

Today, many will argue that the widespread use of GUIs leaves the old CLIs obsolete and a waste of time. If you are an end-user who only runs application programs, you will likely never need to use and, therefore, never need to know about the command line. If, on the other hand, you are a software developer, then you should consider the following:

  1. Launching a program by clicking on an icon does use the command line. Each icon contains command-line information, but it's hidden in the icon's details. Hiding CLI information in an icon makes it easier for everyday users to run programs, but it implies that application developers must understand the CLI even in a predominantly GUI environment.
  2. Whenever the operating system spawns a new process (better known as running a program), it locates the executable file (the program) and passes information into it through the CLI. You must know about command-line arguments if you create a program that launches another program, which is not uncommon in "real world" programming.
  3. The operating system automatically starts some programs (e.g., file and web servers) when the computer boots up, and they run without communicating directly with a user. These programs do not have or need a GUI.
  4. Although the list of computer maintenance and administration programs is continuously shrinking, there are still important there are still some that do not have a GUI.

In summary, if you only use a computer to surf the web, interact with friends through social media, run office automation software, post pictures of your grandkids, etc., you probably don't need to know about the command line. However, if you are a computer professional, knowing how to use the CLI and command-line arguments is a necessary, basic skill.

Processing Command Line Arguments

In C++, accessing the command line arguments begins with adding two arguments to function main. Remember, it is the operating system that "calls" your program, and so it is the operating system that will pass data into main through the arguments. Note that the order in which the arguments appear is important. Like any function, you may choose the names for your function arguments; however, argc and argv are traditional names, and I recommend that you use them unless there is a compelling reason to choose different names.

C:\Users\dab\>name_box Cranston Q. Snort
C:\Users\dab\>.\name_box Cranston Q. Snort
(a)(b)
main(int argc, char* argv[])
(c)
The picture illustrates the relationship between argc, argv, and the command "name_box Cranston Q. Snort". The command has four elements, including the command name, so argc=4. argv is an array of pointers: argv[0] points to name_box\0, argv[1] points to Cranston\0, argv[2] points to Q.\0, argv[3] points to Snort\0, and argv[4] is set to nullptr.
(d)
Processing command line input. There are two sides to processing command line input: (a) The command line that a user enters that begins the process and (b) the program interface that receives the data. Part (c) of the illustration is an abstract representation of part (b) in the specific case of the data entered in part (a).
  1. Command Prompt: A command line consisting of a program name and three input strings. The CLI is just another program: It scans the entered command line, looking for spaces that separate the different parts of the command line. It discards the spaces and places each part of the command line in a separate element of argv array as illustrated in (c)
  2. Power Shell: Similar to running from a command prompt but requires ".\" before the command or program name
  3. Function main modified to accept command line arguments. argc, short for argument count, is the number of command-line arguments, which, in C++, includes the program's name. argv, short for argument vector, is an array of C-strings or character pointers. (In this context, "vector" is just another name for an array.) It is up to the program to determine what the strings mean or how to use them. Perhaps they are the names of files that the program will process or the address of a website that the user wishes to visit
  4. The command line input is passed into a program (e.g., name_box) through the arguments defined in function main: argc and argv. The figure illustrates that the program name is always the first argument at index location 0 (Java does not do this). Most programs ignore their name and begin processing the arguments with the one located at index 1. (Some operating systems allow giving a program more than one name, and the program can test argv[0] to determine the name used to start it and alter its behavior accordingly.)
Suppose you are using an IDE like Visual Studio. In that case, you can enter the command-line arguments through an IDE option, allowing you to test a command-line program without opening a command prompt window. The name_box demonstration in the next section includes instructions for doing this.

Command Line Arguments: Example Code

(a)
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	cout << "argc is " << argc << endl;
	for (int i = 0; i < argc; i++)
		cout << "argv[" << i << "] is " << argv[i] << endl;

	return 0;
}
(b)
public class CommandLine
{
	public static void main(String[] args)
	{
		System.out.println("args.length is " + args.length);
		for (int i = 0; i < args.length; i++)
			System.out.printf("args[%d] is %s%n", i, args[i]);
	}
}
Accessing command line arguments. The simple programs listed here illustrate the syntax needed to access the command line arguments.
  1. C++
  2. Java