cli_help_text Function

private function cli_help_text(this) result(text)

The help text, as a string. Nothing is printed; see the module note.

Entries appear in registration order, so the text is stable across runs and across compilers.

Type Bound

cli_t

Arguments

Type IntentOptional Attributes Name
class(cli_t), intent(in) :: this

Return Value character(len=:), allocatable


Calls

proc~~cli_help_text~~CallsGraph proc~cli_help_text cli_t%cli_help_text proc~any_of_kind any_of_kind proc~cli_help_text->proc~any_of_kind proc~entry_label entry_label proc~cli_help_text->proc~entry_label

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private :: i
character(len=:), private, allocatable :: label
integer(kind=default_int), private :: this_width
integer(kind=default_int), private :: width

Source Code

   function cli_help_text(this) result(text)
      !! The help text, as a string. Nothing is printed; see the module note.
      !!
      !! Entries appear in registration order, so the text is stable across
      !! runs and across compilers.
      class(cli_t), intent(in) :: this
      character(len=:), allocatable :: text

      integer(default_int) :: i, width, this_width
      character(len=:), allocatable :: label

      text = "Usage: "//char(this%program_name)
      do i = 1_default_int, this%count
         if (this%entries(i)%kind /= KIND_OPTION .and. &
             this%entries(i)%kind /= KIND_FLAG) cycle
         text = text//" [options]"
         exit
      end do
      do i = 1_default_int, this%count
         if (this%entries(i)%kind /= KIND_POSITIONAL) cycle
         if (this%entries(i)%required) then
            text = text//" <"//char(this%entries(i)%name)//">"
         else
            text = text//" ["//char(this%entries(i)%name)//"]"
         end if
      end do
      text = text//new_line("a")

      if (len(char(this%summary)) > 0) then
         text = text//new_line("a")//char(this%summary)//new_line("a")
      end if

      ! One pass to find the column, one to write it, so the descriptions line
      ! up whatever the names are.
      width = 0_default_int
      do i = 1_default_int, this%count
         this_width = len(entry_label(this%entries(i)), kind=default_int)
         width = max(width, this_width)
      end do

      if (any_of_kind(this, KIND_POSITIONAL)) then
         text = text//new_line("a")//"Arguments:"//new_line("a")
         do i = 1_default_int, this%count
            if (this%entries(i)%kind /= KIND_POSITIONAL) cycle
            label = entry_label(this%entries(i))
            text = text//"  "//label//repeat(" ", width - len(label))// &
                   "  "//char(this%entries(i)%help)//new_line("a")
         end do
      end if

      text = text//new_line("a")//"Options:"//new_line("a")
      do i = 1_default_int, this%count
         if (this%entries(i)%kind == KIND_POSITIONAL) cycle
         label = entry_label(this%entries(i))
         text = text//"  "//label//repeat(" ", width - len(label))// &
                "  "//char(this%entries(i)%help)//new_line("a")
      end do
      text = text//"  "//"-h, --help"//repeat(" ", max(0_default_int, width - 10_default_int))// &
             "  Show this help"//new_line("a")
   end function cli_help_text