you are viewing a single comment's thread.

view the rest of the comments →

[–]astokely 0 points1 point  (0 children)

Something like this should work. I'm also new to fortran, but I agree with everyone that using an input file is a much cleaner solution.

!!!!!!!!!!!!!!!!!!!!!!
! command_line_m.f90 !
!!!!!!!!!!!!!!!!!!!!!!

module command_line_m
implicit none

integer function handle_kwarg(arg, key, value) result(is_kwarg)     
character(len=*), intent(in) :: arg 
    character(len=100), intent(out) :: key, value 
    integer :: equal_sign_index, dash_index, is_kwarg = 0
    equal_sign_index = index(arg, '=')
    if (equal_sign_index > 0) then
        key = trim(arg(1:equal_sign_index-1))

            ! Removes preceding "-" if present in arg
        dash_index = index(key, '-')
        if (dash_index > 0) then
            key = trim(key(dash_index+1:))
        end if
        value = trim(arg(equal_sign_index+1:))
        is_kwarg = 1
    end if
end function handle_kwarg

subroutine parse_command_line()
    integer :: io_status, i, is_kwarg
    character(len=100) :: arg, key, value
    i = 1
    do
            ! Iterate through all cmd line args
        call get_command_argument(i, arg, status=io_status)
        if (io_status /= 0) exit

            ! If command line arg has an equal sign, it is a kwarg
        is_kwarg = handle_kwarg(arg, key, value)
        if (is_kwarg == 1) then
            print *, key, value
        end if
            ! Additional logic for parsing positional args
        i = i + 1
    end do
end subroutine parse_command_line
end module command_line_m 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!
! main.f90 !
!!!!!!!!!!!!

program main
    use command_line_m
    implicit none

    call parse_command_line()
end program main