Skip to content

Commit

Permalink
add try_parse type check
Browse files Browse the repository at this point in the history
  • Loading branch information
0382 committed May 12, 2024
1 parent 407f77c commit 1f3d979
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/argparse-f.f90
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ subroutine argp_parse(this)
&"option '"//trim(this%options(i)%long_name)//"' requires a "//trim(this%options(i)%value_type)//" value")
end if
this%options(i)%value = tokens(j + 1)
call argp_try_parse_option(this, this%options(i))
token_parsed_num = 2
exit
end if
Expand All @@ -221,7 +222,7 @@ subroutine argp_parse(this)
end if
! short circuit option
if (idx <= this%sc_option_size) then
if(this%sc_options(idx)%short_name(2:2) == tok(i:i)) then
if (this%sc_options(idx)%short_name(2:2) == tok(i:i)) then
if (associated(this%sc_options(idx)%callback, dummy_print_help_wrapper)) then
call this%print_help()
else
Expand All @@ -232,7 +233,7 @@ subroutine argp_parse(this)
end if
! normal option
if (idx <= this%option_size) then
if(this%options(idx)%short_name(2:2) == tok(i:i)) then
if (this%options(idx)%short_name(2:2) == tok(i:i)) then
if (this%options(idx)%value_type == "logical") then
this%options(idx)%value = 'T'
else
Expand All @@ -257,6 +258,7 @@ subroutine argp_parse(this)
tok = tokens(j)
if (try_parse_named_argument(tok, this%named_arguments(i))) then
token_parsed_num = 1
call argp_try_parse_argumrnt(this, this%named_arguments(i))
exit
end if
end do
Expand All @@ -268,6 +270,7 @@ subroutine argp_parse(this)
! start parse position argument
if (argc /= this%argument_size) then
call this%print_help()
print '(A)', repeat('-', 80)
print '(A,I0,A,I0)', "position argument number missmatching, give ", argc, ", but need ", this%argument_size
if (argc /= 0) then
write (*, '("unparsed arguments:")', advance='no')
Expand All @@ -280,6 +283,7 @@ subroutine argp_parse(this)
end if
do i = 1, this%argument_size
this%arguments(i)%value = tokens(i)
call argp_try_parse_argumrnt(this, this%arguments(i))
end do
deallocate (tokens)
end subroutine argp_parse
Expand Down Expand Up @@ -788,6 +792,26 @@ pure integer function argp_find_option(this, name) result(ans)
error stop "(get error) option not found: "//trim(name)
end function argp_find_option

subroutine argp_try_parse_option(this, opt)
class(argparser), intent(inout) :: this
type(option), intent(inout) :: opt
integer :: state, iret
real :: rret
real(kind=8) :: dret
state = 0
if (opt%value_type == "integer") then
read (unit=opt%value, fmt=*, iostat=state) iret
else if (opt%value_type == "real") then
read (unit=opt%value, fmt=*, iostat=state) rret
else if (opt%value_type == "double") then
read (unit=opt%value, fmt=*, iostat=state) dret
end if
if (state /= 0) then
call argp_parse_error(this, &
&"option '"//trim(opt%long_name)//"' need a "//trim(opt%value_type)//" value, but got '"//trim(opt%value)//"'")
end if
end subroutine argp_try_parse_option

pure subroutine argp_check_option_type(this, idx, type)
class(argparser), intent(in) :: this
integer, intent(in) :: idx
Expand Down Expand Up @@ -869,6 +893,26 @@ pure integer function argp_find_argument(this, name) result(ans)
error stop "(get error) argument not found: "//trim(name)
end function argp_find_argument

subroutine argp_try_parse_argumrnt(this, arg)
class(argparser), intent(inout) :: this
type(argument), intent(inout) :: arg
integer :: state, iret
real :: rret
real(kind=8) :: dret
state = 0
if (arg%value_type == "integer") then
read (unit=arg%value, fmt=*, iostat=state) iret
else if (arg%value_type == "real") then
read (unit=arg%value, fmt=*, iostat=state) rret
else if (arg%value_type == "double") then
read (unit=arg%value, fmt=*, iostat=state) dret
end if
if (state /= 0) then
call argp_parse_error(this, &
&"argument '"//trim(arg%name)//"' need a "//trim(arg%value_type)//" value, but got '"//trim(arg%value)//"'")
end if
end subroutine argp_try_parse_argumrnt

pure subroutine argp_check_argument_type(this, idx, type)
class(argparser), intent(in) :: this
integer, intent(in) :: idx
Expand Down Expand Up @@ -999,7 +1043,9 @@ subroutine argp_parse_error(this, message)
class(argparser), intent(in) :: this
character(len=*), intent(in) :: message
call this%print_help()
stop trim(message)
print '(A)', repeat("-", 80)
print '("Error: ", A)', trim(message)
stop
end subroutine argp_parse_error

pure subroutine split(line, sep, result)
Expand Down

0 comments on commit 1f3d979

Please sign in to comment.