get_args_from_string Subroutine

private subroutine get_args_from_string(self, args, ai)

Get CLAs from string.

Type Bound

command_line_interface

Arguments

Type IntentOptional Attributes Name
class(command_line_interface), intent(inout) :: self

CLI data.

character(len=*), intent(in) :: args

String containing command line arguments.

integer(kind=I4P), intent(out), allocatable :: ai(:,:)

CLAs grouped indexes.


Calls

proc~~get_args_from_string~~CallsGraph proc~get_args_from_string flap_command_line_interface_t::command_line_interface%get_args_from_string proc~get_clasg_indexes flap_command_line_interface_t::command_line_interface%get_clasg_indexes proc~get_args_from_string->proc~get_clasg_indexes proc~tokenize flap_utils_m::tokenize proc~get_args_from_string->proc~tokenize proc~is_defined_group flap_command_line_interface_t::command_line_interface%is_defined_group proc~get_clasg_indexes->proc~is_defined_group

Called by

proc~~get_args_from_string~~CalledByGraph proc~get_args_from_string flap_command_line_interface_t::command_line_interface%get_args_from_string none~get_args flap_command_line_interface_t::command_line_interface%get_args none~get_args->proc~get_args_from_string proc~parse~2 flap_command_line_interface_t::command_line_interface%parse proc~parse~2->none~get_args proc~fake_call flap_test_group::fake_call proc~fake_call->proc~parse~2 none~get~2 flap_command_line_interface_t::command_line_interface%get proc~fake_call->none~get~2 proc~get_cla_list_varying_char~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_char proc~get_cla_list_varying_char~2->proc~parse~2 proc~get_cla_list_varying_i1p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I1P proc~get_cla_list_varying_i1p~2->proc~parse~2 proc~get_cla_list_varying_i2p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I2P proc~get_cla_list_varying_i2p~2->proc~parse~2 proc~get_cla_list_varying_i4p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I4P proc~get_cla_list_varying_i4p~2->proc~parse~2 proc~get_cla_list_varying_i8p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_I8P proc~get_cla_list_varying_i8p~2->proc~parse~2 proc~get_cla_list_varying_logical~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_logical proc~get_cla_list_varying_logical~2->proc~parse~2 proc~get_cla_list_varying_r16p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R16P proc~get_cla_list_varying_r16p~2->proc~parse~2 proc~get_cla_list_varying_r4p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R4P proc~get_cla_list_varying_r4p~2->proc~parse~2 proc~get_cla_list_varying_r8p~2 flap_command_line_interface_t::command_line_interface%get_cla_list_varying_R8P proc~get_cla_list_varying_r8p~2->proc~parse~2 proc~get_cla_list~2 flap_command_line_interface_t::command_line_interface%get_cla_list proc~get_cla_list~2->proc~parse~2 proc~get_cla~2 flap_command_line_interface_t::command_line_interface%get_cla proc~get_cla~2->proc~parse~2 program~flap_save_bash_completion flap_save_bash_completion program~flap_save_bash_completion->proc~parse~2 program~flap_test_basic flap_test_basic program~flap_test_basic->proc~parse~2 none~get_varying~2 flap_command_line_interface_t::command_line_interface%get_varying program~flap_test_basic->none~get_varying~2 program~flap_test_basic->none~get~2 program~flap_test_choices_logical flap_test_choices_logical program~flap_test_choices_logical->proc~parse~2 program~flap_test_choices_logical->none~get~2 program~flap_test_nested flap_test_nested program~flap_test_nested->proc~parse~2 program~flap_test_nested->none~get~2 program~flap_test_string flap_test_string program~flap_test_string->proc~parse~2 program~flap_test_string->none~get~2 none~get_varying~2->proc~get_cla_list_varying_char~2 none~get_varying~2->proc~get_cla_list_varying_i1p~2 none~get_varying~2->proc~get_cla_list_varying_i2p~2 none~get_varying~2->proc~get_cla_list_varying_i4p~2 none~get_varying~2->proc~get_cla_list_varying_i8p~2 none~get_varying~2->proc~get_cla_list_varying_logical~2 none~get_varying~2->proc~get_cla_list_varying_r4p~2 none~get_varying~2->proc~get_cla_list_varying_r8p~2 none~get~2->proc~get_cla_list~2 none~get~2->proc~get_cla~2 program~flap_test_group flap_test_group program~flap_test_group->proc~fake_call program~flap_save_man_page flap_save_man_page program~flap_save_man_page->none~get~2 program~flap_save_usage_to_markdown flap_save_usage_to_markdown program~flap_save_usage_to_markdown->none~get~2 program~flap_test_action_store flap_test_action_store program~flap_test_action_store->none~get_varying~2 program~flap_test_action_store->none~get~2 program~flap_test_ansi_color_style flap_test_ansi_color_style program~flap_test_ansi_color_style->none~get~2 program~flap_test_duplicated_clas flap_test_duplicated_clas program~flap_test_duplicated_clas->none~get~2 program~flap_test_group_examples flap_test_group_examples program~flap_test_group_examples->none~get~2 program~flap_test_hidden flap_test_hidden program~flap_test_hidden->none~get~2 program~flap_test_ignore_unknown_clas flap_test_ignore_unknown_clas program~flap_test_ignore_unknown_clas->none~get~2 program~flap_test_minimal flap_test_minimal program~flap_test_minimal->none~get~2 program~flap_test_nargs_insufficient flap_test_nargs_insufficient program~flap_test_nargs_insufficient->none~get~2 program~flap_test_nargs_insufficient~2 flap_test_nargs_insufficient program~flap_test_nargs_insufficient~2->none~get~2

Source Code

  subroutine get_args_from_string(self, args, ai)
  !< Get CLAs from string.
  class(command_line_interface), intent(inout) :: self   !< CLI data.
  character(*),                  intent(in)    :: args   !< String containing command line arguments.
  integer(I4P), allocatable,     intent(out)   :: ai(:,:)!< CLAs grouped indexes.
  character(len=len_trim(args))                :: argsd  !< Dummy string containing command line arguments.
  character(len=len_trim(args)), allocatable   :: toks(:)!< CLAs tokenized.
  integer(I4P)                                 :: Nt     !< Number of tokens.
  integer(I4P)                                 :: Na     !< Number of command line arguments passed.
  integer(I4P)                                 :: a      !< Counter for CLAs.
  integer(I4P)                                 :: t      !< Counter for tokens.
  integer(I4P)                                 :: c      !< Counter for characters inside tokens.
#ifndef __GFORTRAN__
  integer(I4P)                                 :: length !< Maxium lenght of arguments string.
#endif

  ! prepare CLI arguments list
  if (allocated(self%args)) deallocate(self%args)

  ! sanitize arguments string
  argsd = trim(args)
  if (index(args,"'")>0) then
    argsd = sanitize_args(argsin=argsd,delimiter="'")
  elseif (index(args,'"')>0) then
    argsd = sanitize_args(argsin=argsd,delimiter='"')
  endif

  ! tokenize arguments string; the previously sanitized white spaces inside tokens are restored
  call tokenize(strin=argsd, delimiter=' ', toks=toks, Nt=Nt)
  Na = 0
  find_number_of_valid_arguments: do t=1,Nt
    if (trim(adjustl(toks(t)))/='') then
      Na = Na + 1
      do c=1,len(toks(t))
        if (toks(t)(c:c)=="'") toks(t)(c:c)=" "
      enddo
    endif
  enddo find_number_of_valid_arguments

  if (Na > 0) then
    ! allocate CLI arguments list
#ifdef __GFORTRAN__
    allocate(self%args(1:Na))
#else
    length = 0
    find_longest_arg: do t=1,Nt
      if (trim(adjustl(toks(t)))/='') length = max(length,len_trim(adjustl(toks(t))))
    enddo find_longest_arg
    allocate(character(length):: self%args(1:Na))
#endif

    ! construct arguments list
    a = 0
    get_args: do t=1,Nt
      if (trim(adjustl(toks(t)))/='') then
        a = a + 1
        self%args(a) = trim(adjustl(toks(t)))
      endif
    enddo get_args
  endif

  call self%get_clasg_indexes(ai=ai)
  contains
    function sanitize_args(argsin, delimiter) result(sanitized)
    !< Sanitize arguments string.
    !<
    !< Substitute white spaces enclosed into string-arguments, i.e. 'string argument with spaces...' or
    !< "string argument with spaces..." with a safe equivalent for tokenization against white spaces, i.e. the finally tokenized
    !< string is string'argument'with'spaces...
    !<
    !< @note The white spaces are reintroduce later.
    character(*), intent(in)                     :: argsin    !< Arguments string.
    character(*), intent(in)                     :: delimiter !< Delimiter enclosing string argument.
    character(len=len_trim(argsin))              :: sanitized !< Arguments string sanitized.
    character(len=len_trim(argsin)), allocatable :: tok(:)    !< Arguments string tokens.
    integer(I4P)                                 :: Nt        !< Number of command line arguments passed.
    integer(I4P)                                 :: t         !< Counter.
    integer(I4P)                                 :: tt        !< Counter.

    call tokenize(strin=trim(argsin), delimiter=delimiter, toks=tok, Nt=Nt)
    do t=2, Nt, 2
      do tt=1,len_trim(adjustl(tok(t)))
        if (tok(t)(tt:tt)==' ') tok(t)(tt:tt) = "'"
      enddo
    enddo
    sanitized = ''
    do t=1, Nt
      sanitized = trim(sanitized)//" "//trim(adjustl(tok(t)))
    enddo
    sanitized = trim(adjustl(sanitized))
    endfunction sanitize_args
  endsubroutine get_args_from_string