parse Subroutine

subroutine parse(name)

PUT EVERYTHING TO DO WITH COMMAND PARSING HERE FOR CLARITY

Arguments

Type IntentOptional AttributesName
character(len=*) :: name

Contents

Source Code


Variables

TypeVisibility AttributesNameInitial
character(len=:), public, allocatable:: help_text(:)
character(len=:), public, allocatable:: version_text(:)

Source Code

subroutine parse(name)
!! PUT EVERYTHING TO DO WITH COMMAND PARSING HERE FOR CLARITY
use M_CLI2, only : set_args, get_args, get_args_fixed_length
use M_CLI2, only : get_subcommand
use M_CLI2, only : CLI_RESPONSE_FILE
character(len=*)              :: name    ! the subcommand name
character(len=:),allocatable  :: help_text(:), version_text(:)
   CLI_RESPONSE_FILE=.true.

! define version text
   version_text=[character(len=80) :: &
      '@(#)PROGRAM:     demo6            >', &
      '@(#)DESCRIPTION: My demo program  >', &
      '@(#)VERSION:     1.0 20200715     >', &
      '@(#)AUTHOR:      me, myself, and I>', &
      '@(#)LICENSE:     Public Domain    >', &
      '' ]

    ! general help for "demo6 --help"
    help_text=[character(len=80) :: &
     ' allowed subcommands are          ', &
     '   * run  -l -L -title -x -y -z   ', &
     '   * test -l -L -title            ', &
     '' ]

   ! find the subcommand name by looking for first word on command
   ! not starting with dash
   name = get_subcommand()

   select case(name)

   case('run')
    help_text=[character(len=80) :: &
     '                                  ', &
     ' Help for subcommand "run"        ', &
     '                                  ', &
     '' ]
    call set_args('-x 1 -y 2 -z 3 --title "my title" -l F -L F',help_text,version_text)
    call get_args('x',x)
    call get_args('y',y)
    call get_args('z',z)
    call get_args_fixed_length('title',title)
    call get_args('l',l)
    call get_args('L',l_)

   case('test')
    help_text=[character(len=80) :: &
     '                                  ', &
     ' Help for subcommand "test"       ', &
     '                                  ', &
     '' ]
    call set_args('--title "my title" -l F -L F --testname "Test"',help_text,version_text)
    call get_args_fixed_length('title',title)
    call get_args('l',l)
    call get_args('L',l_)
    call get_args_fixed_length('testname',testname)

   case default
      call set_args(' ',help_text,version_text) ! process help and version

      write(*,'(*(a))')'unknown or missing subcommand [',trim(name),']'
      write(*,'(a)')[character(len=80) ::  &
     ' allowed subcommands are          ', &
     '   * run  -l -L -title -x -y -z   ', &
     '   * test -l -L -title            ', &
     '' ]
     stop

   end select

end subroutine parse