save_bash_completion Subroutine

private subroutine save_bash_completion(self, bash_file, error)

Save bash completion script (for named CLAs only).

Type Bound

command_line_interface

Arguments

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

CLI data.

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

Output file name of bash completion script.

integer(kind=I4P), intent(out), optional :: error

Error trapping flag.


Calls

proc~~save_bash_completion~~CallsGraph proc~save_bash_completion flap_command_line_interface_t::command_line_interface%save_bash_completion proc~signature flap_command_line_arguments_group_t::command_line_arguments_group%signature proc~save_bash_completion->proc~signature proc~signature~2 flap_command_line_argument_t::command_line_argument%signature proc~signature->proc~signature~2 cton cton proc~signature~2->cton proc~has_choices flap_command_line_argument_t::command_line_argument%has_choices proc~signature~2->proc~has_choices str str proc~signature~2->str

Called by

proc~~save_bash_completion~~CalledByGraph proc~save_bash_completion flap_command_line_interface_t::command_line_interface%save_bash_completion program~flap_save_bash_completion flap_save_bash_completion program~flap_save_bash_completion->proc~save_bash_completion

Source Code

  subroutine save_bash_completion(self, bash_file, error)
  !< Save bash completion script (for named CLAs only).
  class(command_line_interface), intent(in)  :: self      !< CLI data.
  character(*),                  intent(in)  :: bash_file !< Output file name of bash completion script.
  integer(I4P), optional,        intent(out) :: error     !< Error trapping flag.
  character(len=:), allocatable              :: script    !< Script text.
  integer(I4P)                               :: g         !< CLAs groups counter.
  integer(I4P)                               :: u         !< Unit file handler.

  script = '#/usr/bin/env bash'
  if (size(self%clasg,dim=1)>1) then
    script = script//new_line('a')//'_completion()'
    script = script//new_line('a')//'{'
    script = script//new_line('a')//'  cur=${COMP_WORDS[COMP_CWORD]}'
    script = script//new_line('a')//'  prev=${COMP_WORDS[COMP_CWORD - 1]}'
    ! script = script//new_line('a')//'  if [[ $prev == "--help" || $prev == "-h" || $prev == "--version" || $prev == "-v" ]] ; then'
    ! script = script//new_line('a')//'    COMPREPLY=()'
    ! script = script//new_line('a')//'  else'
    script = script//new_line('a')//'  groups=('
    do g=1,size(self%clasg,dim=1)-1
      script = script//' "'//self%clasg(g)%group//'"'
    enddo
    script = script//' )'
    ! script = script//new_line('a')//'    base_clas=('//&
    !          self%clasg(0)%signature(bash_completion=.true., plain=.true.)//' )'
    ! do g=1,size(self%clasg,dim=1)-1
    !   script = script//new_line('a')//'    '//self%clasg(g)%group//'_clas=('//&
    !            self%clasg(g)%signature(bash_completion=.true., plain=.true.)//' )'
    ! enddo
    script = script//new_line('a')//'  for g in ${groups[@]}; do'
    script = script//new_line('a')//'    if [ "$prev" == "$g" ] ; then'
    script = script//new_line('a')//'      group=$prev '
    script = script//new_line('a')//'    fi'
    script = script//new_line('a')//'  done'
    ! script = script//new_line('a')//'  fi'
    script = script//new_line('a')//'  if [ "$group" == "'//self%clasg(1)%group//'" ] ; then'
    script = script//self%clasg(1)%signature(bash_completion=.true.)
    do g=2,size(self%clasg,dim=1)-1
      script = script//new_line('a')//'  elif [ "$group" == "'//self%clasg(g)%group//'" ] ; then'
      script = script//self%clasg(g)%signature(bash_completion=.true.)
    enddo
      script = script//new_line('a')//'  else'
      script = script//               '    '//self%signature(bash_completion=.true.)
      script = script//new_line('a')//'  fi'
      script = script//new_line('a')//'  return 0'
      script = script//new_line('a')//'}'
      script = script//new_line('a')//'complete -F _completion '//basename(self%progname)
  else
    script = script//new_line('a')//'complete -W "'//self%signature(bash_completion=.true.)//'" '//basename(self%progname)
  endif
  open(newunit=u,file=trim(adjustl(bash_file)))
  if (present(error)) then
    write(u, "(A)", iostat=error)script
  else
    write(u, "(A)")script
  endif
  close(u)
  contains
    pure function basename(progname)
      character(len=*), intent(in)  :: progname !< Program name.
      character(len=:), allocatable :: basename !< Program name without full PATH.
      integer(I4P)                  :: pos      !< Counter.

      basename = progname
      pos = index(basename, '/', back=.true.)
      if (pos>0) then
        basename = basename(pos+1:)
      else
        pos = index(basename, '\', back=.true.)
        if (pos>0) basename = basename(pos+1:)
      endif
      endfunction basename
  endsubroutine save_bash_completion