Command Line Subcommands in Co-array Fortran
Here’s the translation of the Go code to Co-array Fortran, with explanations in Markdown format suitable for Hugo:
Our program demonstrates the use of subcommands with their own set of flags. This is similar to how tools like git
have different subcommands (e.g., git commit
, git push
) with their own options.
program main
use iso_fortran_env
implicit none
character(len=10) :: subcommand
logical :: foo_enable
character(len=20) :: foo_name
integer :: bar_level
integer :: i, num_args
character(len=20), allocatable :: args(:)
! Get command-line arguments
num_args = command_argument_count()
allocate(args(num_args))
do i = 1, num_args
call get_command_argument(i, args(i))
end do
! Check if a subcommand was provided
if (num_args < 1) then
print *, "expected 'foo' or 'bar' subcommands"
call exit(1)
end if
subcommand = args(1)
select case (subcommand)
case ("foo")
! Parse foo subcommand flags
foo_enable = .false.
foo_name = ""
do i = 2, num_args
select case (args(i))
case ("-enable")
foo_enable = .true.
case ("-name")
if (i < num_args) then
foo_name = args(i+1)
i = i + 1
end if
end select
end do
print *, "subcommand 'foo'"
print *, " enable:", foo_enable
print *, " name:", trim(foo_name)
print *, " tail:", args(i:)
case ("bar")
! Parse bar subcommand flags
bar_level = 0
do i = 2, num_args
select case (args(i))
case ("-level")
if (i < num_args) then
read(args(i+1), *) bar_level
i = i + 1
end if
end select
end do
print *, "subcommand 'bar'"
print *, " level:", bar_level
print *, " tail:", args(i:)
case default
print *, "expected 'foo' or 'bar' subcommands"
call exit(1)
end select
deallocate(args)
end program main
To compile and run the program:
$ gfortran -o command_line_subcommands command_line_subcommands.f90
$ ./command_line_subcommands foo -enable -name joe a1 a2
subcommand 'foo'
enable: T
name: joe
tail: a1 a2
$ ./command_line_subcommands bar -level 8 a1
subcommand 'bar'
level: 8
tail: a1
Note that Co-array Fortran doesn’t have a built-in flag parsing library like Go’s flag
package. In this example, we’ve implemented a simple command-line argument parsing system. For more complex needs, you might want to use a third-party argument parsing library or implement a more robust system.
Also, Co-array Fortran doesn’t have a direct equivalent to Go’s flag.NewFlagSet
. Instead, we’re using a select case
statement to handle different subcommands and their respective flags.
The program structure remains similar: we check for the subcommand, parse its specific flags, and then perform the subcommand’s action. However, the implementation details differ due to the different nature of Fortran compared to Go.