Command-line arguments are a common way to parameterize execution of programs. For example, ./my_program arg1 arg2 uses arg1 and arg2 as arguments to the my_program executable.
In Fortran, we use the intrinsic procedures command_argument_count() and get_command_argument() to access command-line arguments. The first argument (index 0) is typically the program name, similar to other languages.
To experiment with command-line arguments, it’s best to compile the program first:
In this example, we’ve demonstrated how to access all arguments, arguments without the program name, and how to access a specific argument by index.
Next, we’ll look at more advanced command-line processing with options parsing libraries available in Fortran.