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