Skip to content

Commit

Permalink
Merge branch 'release/1.1.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
szaghi committed Jul 8, 2019
2 parents 7feb688 + 5ef92b6 commit 38908ab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/flap_command_line_argument_t.F90
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module flap_command_line_argument_t
procedure, public :: raise_error_nargs_insufficient !< Raise error insufficient number of argument values passed.
procedure, public :: raise_error_value_missing !< Raise error missing value.
procedure, public :: raise_error_switch_unknown !< Raise error switch_unknown.
procedure, public :: raise_error_duplicated_clas !< Raise error duplicated CLAs passed.
generic, public :: get => &
get_cla, &
get_cla_list !< Get CLA value(s).
Expand Down Expand Up @@ -124,6 +125,7 @@ module flap_command_line_argument_t
integer(I4P), parameter :: ERROR_STORE_STAR_NARGS = 20 !< Action store* not allowed for list-values CLA.
integer(I4P), parameter :: ERROR_STORE_STAR_ENVVAR = 21 !< Action store* not allowed for environment variable CLA.
integer(I4P), parameter :: ERROR_ACTION_UNKNOWN = 22 !< Unknown CLA (switch name).
integer(I4P), parameter :: ERROR_DUPLICATED_CLAS = 23 !< Duplicated CLAs passed, passed multiple instance of the same CLA.

contains
! public methods
Expand Down Expand Up @@ -244,6 +246,15 @@ subroutine raise_error_switch_unknown(self, switch, pref)
call self%errored(pref=pref, error=ERROR_UNKNOWN, switch=switch)
endsubroutine raise_error_switch_unknown

subroutine raise_error_duplicated_clas(self, switch, pref)
!< Raise error duplicated CLAs passed.
class(command_line_argument), intent(inout) :: self !< CLA data.
character(*), optional, intent(in) :: switch !< CLA switch name.
character(*), optional, intent(in) :: pref !< Prefixing string.

call self%errored(pref=pref, error=ERROR_DUPLICATED_CLAS, switch=switch)
endsubroutine raise_error_duplicated_clas

subroutine sanitize_defaults(self)
!---------------------------------------------------------------------------------------------------------------------------------
!< Sanitize defaults values.
Expand Down Expand Up @@ -553,6 +564,8 @@ subroutine errored(self, error, pref, switch, val_str, log_value)
'" has "'//action_store_star//'" action that is not allowed for environment variable option!'
case(ERROR_ACTION_UNKNOWN)
self%error_message = prefd//': named option "'//trim(adjustl(self%switch))//'" has unknown "'//self%act//'" action!'
case(ERROR_DUPLICATED_CLAS)
self%error_message = prefd//': switch "'//trim(adjustl(switch))//'" has been passed more than once!'
endselect
call self%print_error_message
endif
Expand Down
16 changes: 16 additions & 0 deletions src/lib/flap_command_line_arguments_group_t.f90
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ subroutine parse(self, args, pref)
class(command_line_arguments_group), intent(inout) :: self !< CLAsG data.
character(*), optional, intent(in) :: pref !< Prefixing string.
character(*), intent(in) :: args(:) !< Command line arguments.
type(command_line_argument) :: cla !< CLA data.
character(500) :: envvar !< Environment variables buffer.
integer(I4P) :: arg !< Argument counter.
integer(I4P) :: a !< Counter.
Expand All @@ -297,6 +298,11 @@ subroutine parse(self, args, pref)
if (.not.self%cla(a)%is_positional) then
if (trim(adjustl(self%cla(a)%switch ))==trim(adjustl(args(arg))).or.&
trim(adjustl(self%cla(a)%switch_ab))==trim(adjustl(args(arg)))) then
if (self%cla(a)%is_passed) then
! current CLA has been already passed, raise an error
call self%cla(arg)%raise_error_duplicated_clas(pref=pref, switch=trim(adjustl(args(arg))))
self%error = self%cla(arg)%error
endif
found_val = .false.
if (self%cla(a)%act==action_store) then
if (allocated(self%cla(a)%envvar)) then
Expand Down Expand Up @@ -413,6 +419,16 @@ subroutine parse(self, args, pref)
endif
enddo
if (.not.found) then ! current argument (arg-th) does not correspond to a named option
if (arg>self%Na) then ! has been passed too much CLAs
! place the error into a new positional dummy CLA
call cla%assign_object(self)
cla%is_passed = .true.
cla%m_exclude = ''
call self%add(pref=pref, cla=cla)
call self%cla(self%Na)%raise_error_switch_unknown(pref=pref, switch=trim(adjustl(args(arg))))
self%error = self%cla(self%Na)%error
return
endif
if (.not.self%cla(arg)%is_positional) then ! current argument (arg-th) is not positional... there is a problem!
call self%cla(arg)%raise_error_switch_unknown(pref=pref, switch=trim(adjustl(args(arg))))
self%error = self%cla(arg)%error
Expand Down
23 changes: 23 additions & 0 deletions src/tests/flap_test_duplicated_clas.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
!< A testing program for FLAP, Fortran command Line Arguments Parser for poor people
program flap_test_duplicated_clas
!< A testing program for FLAP, Fortran command Line Arguments Parser for poor people
!<
!<### Compile
!< See [compile instructions](https://github.com/szaghi/FLAP/wiki/Download-compile).
!<
!<###Usage Compile
!< See [usage instructions](https://github.com/szaghi/FLAP/wiki/Testing-Programs).

use flap, only : command_line_interface
use penf

implicit none
type(command_line_interface) :: cli !< Command Line Interface (CLI).
real(R8P) :: rval !< Real value.
integer(I4P) :: error !< Error trapping flag.

call cli%init(progname='test_duplicated_clas', description='Test passed duplicated CLAS')
call cli%add(switch='--i', switch_ab='-i', help='input', required=.true., act='store', error=error) ; if (error/=0) stop
call cli%get(switch='-i', val=rval, error=error) ; if (error/=0) stop
print '(A)' ,'Input = '//trim(str(n=rval))
endprogram flap_test_duplicated_clas

0 comments on commit 38908ab

Please sign in to comment.