Command-line arguments are a common way to parameterize execution of programs. For example, ./program arg1 arg2 uses arg1 and arg2 as arguments to the program executable.
In Fortran, we use the command_argument_count() function to get the number of command-line arguments, and get_command_argument() to retrieve individual arguments. The first argument (index 0) is the program name.
To experiment with command-line arguments, compile the program first:
Note that Fortran arrays are typically 1-indexed, but command-line arguments are 0-indexed (with 0 being the program name). We’ve adjusted our code to account for this.
Next, we’ll look at more advanced command-line processing with additional Fortran features.