Parse Command Line Interfaces by means of a previously initialized CLAs groups list.
Note
The leading and trailing white spaces are removed from CLA values.
Note
If the args argument is passed the command line arguments are taken from it and not from the actual program CLI invocations.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(command_line_interface), | intent(inout) | :: | self |
CLI data. |
||
character(len=*), | intent(in), | optional | :: | pref |
Prefixing string. |
|
character(len=*), | intent(in), | optional | :: | args |
String containing command line arguments. |
|
integer(kind=I4P), | intent(out), | optional | :: | error |
Error trapping flag. |
subroutine parse(self, pref, args, error) !< Parse Command Line Interfaces by means of a previously initialized CLAs groups list. !< !< @note The leading and trailing white spaces are removed from CLA values. !< !< @note If the *args* argument is passed the command line arguments are taken from it and not from the actual program CLI !< invocations. class(command_line_interface), intent(inout) :: self !< CLI data. character(*), optional, intent(in) :: pref !< Prefixing string. character(*), optional, intent(in) :: args !< String containing command line arguments. integer(I4P), optional, intent(out) :: error !< Error trapping flag. integer(I4P) :: g !< Counter for CLAs group. integer(I4P), allocatable :: ai(:,:) !< Counter for CLAs grouped. if (present(error)) error = 0 if (self%is_parsed_) return ! add help, markdown and version switches if not done by user if (.not.self%disable_hv) then do g=0,size(self%clasg,dim=1)-1 if (.not.(self%is_defined(group=self%clasg(g)%group, switch='--help').and.& self%is_defined(group=self%clasg(g)%group, switch='-h'))) & call self%add(pref = pref, & group_index = g, & switch = '--help', & switch_ab = '-h', & help = 'Print this help message', & required = .false., & def = '', & act = 'print_help') if (.not.(self%is_defined(group=self%clasg(g)%group, switch='--markdown').and.& self%is_defined(group=self%clasg(g)%group, switch='-md'))) & call self%add(pref = pref, & group_index = g, & switch = '--markdown', & switch_ab = '-md', & help = 'Save this help message in a Markdown file', & required = .false., & def = '', & act = 'print_markdown') if (.not.(self%is_defined(group=self%clasg(g)%group, switch='--version').and. & self%is_defined(group=self%clasg(g)%group, switch='-v'))) & call self%add(pref = pref, & group_index = g, & switch = '--version', & switch_ab = '-v', & help = 'Print version', & required = .false., & def = '', & act = 'print_version') enddo endif ! add hidden CLA '--' for getting the rid of eventual trailing CLAs garbage do g=0,size(self%clasg,dim=1)-1 if (.not.self%is_defined(group=self%clasg(g)%group, switch='--')) & call self%add(pref = pref, & group_index = g, & switch = '--', & required = .false., & hidden = .true., & nargs = '*', & def = '', & act = 'store') enddo ! parse passed CLAs grouping in indexes if (present(args)) then call self%get_args(args=args, ai=ai) else call self%get_args(ai=ai) endif ! check CLI consistency call self%check(pref=pref) if (self%error>0) then if (((self%error==ERROR_UNKNOWN).and.(.not.self%ignore_unknown_clas)).or.(self%error/=ERROR_UNKNOWN)) then if (present(error)) error = self%error return else self%error_unknown_clas = ERROR_UNKNOWN_CLAS_IGNORED endif endif ! parse CLI do g=0,size(ai,dim=1)-1 if (ai(g,1)>0) then call self%clasg(g)%parse(args=self%args(ai(g,1):ai(g,2)), ignore_unknown_clas=self%ignore_unknown_clas, & pref=pref, error_unknown_clas=self%error_unknown_clas) else call self%clasg(g)%sanitize_defaults endif self%error = self%clasg(g)%error if (self%error < 0) exit if (self%error > 0) then if (((self%error==ERROR_UNKNOWN).and.(.not.self%ignore_unknown_clas)).or.(self%error/=ERROR_UNKNOWN)) then if (present(error)) error = self%error exit else self%error_unknown_clas = ERROR_UNKNOWN_CLAS_IGNORED endif endif enddo if (self%error>0) then if (((self%error==ERROR_UNKNOWN).and.(.not.self%ignore_unknown_clas)).or.(self%error/=ERROR_UNKNOWN)) then if (present(error)) error = self%error return else self%error_unknown_clas = ERROR_UNKNOWN_CLAS_IGNORED endif endif ! trap the special cases of version/help printing if (self%error == STATUS_PRINT_V) then call self%print_version(pref=pref) stop elseif (self%error == STATUS_PRINT_H) then do g=0,size(ai,dim=1)-1 if(self%clasg(g)%error == STATUS_PRINT_H) then write(self%usage_lun,'(A)') self%usage(pref=pref, g=g) stop endif enddo elseif (self%error == STATUS_PRINT_M) then call self%save_usage_to_markdown(trim(self%progname)//'.md') stop endif ! check if all required CLAs have been passed do g=0, size(ai,dim=1)-1 call self%clasg(g)%is_required_passed(pref=pref) self%error = self%clasg(g)%error if (self%error>0) then if (((self%error==ERROR_UNKNOWN).and.(.not.self%ignore_unknown_clas)).or.(self%error/=ERROR_UNKNOWN)) then if (present(error)) error = self%error exit else self%error_unknown_clas = ERROR_UNKNOWN_CLAS_IGNORED endif endif enddo if (self%error>0) then if (((self%error==ERROR_UNKNOWN).and.(.not.self%ignore_unknown_clas)).or.(self%error/=ERROR_UNKNOWN)) then if (present(error)) error = self%error return else self%error_unknown_clas = ERROR_UNKNOWN_CLAS_IGNORED endif endif ! check mutually exclusive interaction call self%check_m_exclusive(pref=pref) self%is_parsed_ = .true. ! check if the only error found is for unknown passed CLAs and if it is ignored by the user if (self%error==ERROR_UNKNOWN.and.self%error_unknown_clas==ERROR_UNKNOWN_CLAS_IGNORED) self%error = ERROR_UNKNOWN_CLAS_IGNORED if (present(error)) error = self%error endsubroutine parse