src/gray_cli.f90: simplify argument handling

This commit is contained in:
Michele Guerini Rocco 2023-09-13 16:47:25 +02:00
parent a27e56933c
commit 8425e5e286
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -112,7 +112,6 @@ contains
! local variables
character(len=:), allocatable :: argument, temp
logical :: skip_next = .false.
integer :: i, nargs
integer :: error, commas
@ -123,14 +122,11 @@ contains
opts%units = [ucenr, usumm]
nargs = command_argument_count()
do i = 1, nargs
call get_command_string(i, argument)
i = 1
! skip one cycle if the last argument was a value
if (skip_next) then
skip_next = .false.
cycle
end if
do
if (i > nargs) exit
call get_next_command(i, argument)
! parse an argument (and possibly a value)
select case (argument)
@ -151,23 +147,19 @@ contains
opts%quiet = .true.
case ('-o', '--output-dir')
call get_command_string(i + 1, opts%output_dir)
skip_next = .true.
call get_next_command(i, opts%output_dir)
case ('-p', '--params-file')
call get_command_string(i + 1, opts%params_file)
skip_next = .true.
call get_next_command(i, opts%params_file)
case ('-c', '--config-file')
call get_command_string(i + 1, opts%config_file)
skip_next = .true.
call get_next_command(i, opts%config_file)
case ('-s', '--sum')
call get_command_string(i + 1, opts%sum_filelist)
skip_next = .true.
call get_next_command(i, opts%sum_filelist)
case ('-u', '--units')
call get_command_string(i + 1, temp)
call get_next_command(i, temp)
! resize the array
commas = count([(temp(i:i) == ',', i = 1, len(temp))])
@ -183,12 +175,11 @@ contains
call exit(1)
end if
deallocate(temp)
skip_next = .true.
case ('-g', '--gray-param')
! these overrides are parsed later since they need to
! be applied to the final gray_parameters structure
skip_next = .true.
i = i + 1
case default
print '(a,a,/)', 'Unknown option: ', argument
@ -217,24 +208,20 @@ contains
! local variables
character(len=:), allocatable :: argument, temp, id, val
logical :: skip_next = .false.
integer :: i, nargs
integer :: sep
nargs = command_argument_count()
do i = 1, nargs
call get_command_string(i, argument)
i = 1
! skip one cycle if the last argument was a value
if (skip_next) then
skip_next = .false.
cycle
end if
do
if (i > nargs) exit
call get_next_command(i, argument)
! parse gray parameters
select case (argument)
case ('-g', '--gray-param')
call get_command_string(i + 1, temp)
call get_next_command(i, temp)
! split at "=" (id=value)
sep = index(temp, '=')
@ -262,7 +249,6 @@ contains
end select
deallocate(temp)
skip_next = .true.
! skip everything else
case default
@ -276,13 +262,13 @@ contains
end subroutine parse_param_overrides
subroutine get_command_string(i, arg)
subroutine get_next_command(i, arg)
! Reads a CLI argument into a deferred-length string
implicit none
! subroutine arguments
integer, intent(in) :: i
integer, intent(inout) :: i
character(len=:), allocatable, intent(inout) :: arg
! local variables
@ -292,6 +278,7 @@ contains
call get_command_argument(i, length=len) ! get the arg length
allocate(character(len) :: arg) ! allocate memory
call get_command_argument(i, arg) ! copy
i = i + 1 ! increment counter
end subroutine