Here’s the translation of the Go code to Fortran, with explanations in Markdown format suitable for Hugo:
Our program demonstrates the use of command-line subcommands, each with its own set of flags. This is similar to how tools like git have different subcommands (e.g., git commit, git push) with their own specific options.
To compile and run the program:
First, invoke the foo subcommand:
Now try bar:
But bar won’t accept foo’s flags:
Note that Fortran doesn’t have built-in flag parsing like Go’s flag package. This example implements a simple custom parsing logic. For more complex command-line argument parsing in Fortran, you might want to use external libraries or implement a more robust parsing system.
Next, we’ll look at environment variables, another common way to parameterize programs.