Command Line Flags in Fortran
Here’s the translation of the Go code example to Fortran, with explanations in Markdown format suitable for Hugo:
Command-line flags are a common way to specify options for command-line programs. For example, in wc -l
the -l
is a command-line flag.
Fortran doesn’t have a built-in module for parsing command-line arguments like Go’s flag
package. However, we can use the intrinsic get_command_argument
subroutine to achieve similar functionality. Here’s an example of how to implement basic command-line flag parsing in Fortran:
To experiment with the command-line flags program, first compile it and then run the resulting executable directly.
Try out the built program by giving it values for all flags:
Note that if you omit flags, they automatically take their default values:
Trailing positional arguments can be provided after any flags:
Unlike Go’s flag
package, this basic implementation doesn’t automatically generate help text or handle errors for undefined flags. You would need to implement these features manually if required.
Also, note that in this Fortran implementation, the order of the flags matters, and all flags must be preceded by their respective identifiers (e.g., -word
, -numb
, etc.). This differs from Go’s more flexible flag parsing.