Arguments

Looking at software from a very broad perspective, a program’s main functionality is to accept input, take action based on that, and provide output. The main 3 input methods that can be detected are:

  • Reading in from a buffer (i.e. direct console input or file)

  • Capturing mouse and keyboard events (i.e. graphical user interfaces or video games)

  • Command-line arguments

Let’s assume we want to write a program that can iterate recursively through a folder (recursively iterating also goes into the subfolders), and counts the number of files with a user-specified extension. In this case, we have three pieces of information: the folder name, the extension(s), and whether it should run recursively or not. So how do we choose a suitable input method?

Using buffers for input can be really handy when dealing with large data sets, or when input validation is required. In our case, we will have to prompt the user for the required pieces of information, which can become tedious on the user (see Figure 1). Using a file for that kind of input is also overkill, as it’s not user-friendly. Adding to that, file input is processor-heavy, and can hinder performance if overused.

Figure 1

Figure 1

Creating a user interface can be a good option, as it’s very user-friendly, and easily re-runnable. A couple of downsides with using a GUI for this type of utility is: it’s more overhead on the programmer, and it’s not as efficient as running a command-line program. Aside from that, depending on the programming language, the programmer might not be able to use it to develop a user interface (C/C++).

With that said, we reach the third input method: command-line arguments. Running our program in the console, we have the flexibility to enter whatever we want, and those arguments will be passed in to our main() function. Arguments are white space delimited, so adding extra spaces between arguments will be ignored. Having said that, you must enclose path names in quotes if they contain any spaces, as it will be broken down if you don’t.

Figure 2

Figure 2

So how exactly does that work?

To begin with, we need to tell our program that we are expecting command-line arguments, and we do that by modifying main().

In Java and C#, it’s the following: (In Java it’s String)

static void main(string[] args) {}

So by running the following command in the command prompt:

arguments.exe "C:\local" ".c|.cpp" -r

we will find the following in our arguments array:

Annotation 2019-06-29 200658.png

Notice that the first element of args was not something we passed in. By default, our exe location is always, ALWAYS the first argument in the arguments array.

This then allows us to do the following:

// Java
for(int i = 0; i < args.length; ++i) 
   
 

In C/C++, it’s a little different:

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

Note that these parameters can be called anything you like, it’s just common practice to use argc and argv.

The C/C++ committee was nice enough to calculate the number of arguments we have, and put it in argc. The actual values of these arguments are stored in an array of char pointers, which is the C equivalent of strings (Figure 4). This makes it easier to write the following:

for(int i = 0; i < argc; ++i) {
   printf("%s\n", argv[i]);
}

Since C# and Java offer a length member in arrays, we don’t really need that extra argc parameter.

 
Figure 4

Figure 4

Visual Studio and Eclipse offer command-line debugging support by taking the following steps:

Eclipse

java.jpg

Visual Studio

Untitled-1.jpg

When modifying any of the project’s properties, make sure these changes are done on the configuration and the platform you are currently running your program in. In the case of the previous screenshot, the listed command arguments are specific to Debug x86. Switching to Release and/or x64 platform will change/clear out the command arguments, as you can have separate configurations for the different available configurations and platforms.

References

Lippman, Stanley; Lajoie, Josee; Moo, Barbara - C++ Primer

Schildt, Herbert - The Complete C Reference