cli_get_logical Subroutine

private subroutine cli_get_logical(this, name, value, err)

Read a value as logical.

Accepts true, false, yes, no, on, off, 1 and 0 in any mixture of cases. A flag always reads back cleanly, since its stored value is one of the first two.

Type Bound

cli_t

Arguments

Type IntentOptional Attributes Name
class(cli_t), intent(in) :: this
character(len=*), intent(in) :: name

Long name of a declared entry.

logical, intent(out) :: value

Parsed value; false when the conversion failed.

type(error_t), intent(inout), optional :: err

Set on an undeclared name or an unrecognised spelling.


Calls

proc~~cli_get_logical~~CallsGraph proc~cli_get_logical cli_t%cli_get_logical proc~cli_find cli_t%cli_find proc~cli_get_logical->proc~cli_find proc~cli_get_string cli_t%cli_get_string proc~cli_get_logical->proc~cli_get_string proc~error_has_error error_t%error_has_error proc~cli_get_logical->proc~error_has_error proc~fail~2 fail proc~cli_get_logical->proc~fail~2 proc~cli_get_string->proc~cli_find proc~cli_get_string->proc~fail~2 proc~error_set error_t%error_set proc~fail~2->proc~error_set

Variables

Type Visibility Attributes Name Initial
integer(kind=default_int), private :: i
character(len=:), private, allocatable :: lowered
type(string_type), private :: text

Source Code

   subroutine cli_get_logical(this, name, value, err)
      !! Read a value as `logical`.
      !!
      !! Accepts `true`, `false`, `yes`, `no`, `on`, `off`, `1` and `0` in any
      !! mixture of cases. A flag always reads back cleanly, since its stored
      !! value is one of the first two.
      class(cli_t), intent(in) :: this
      character(len=*), intent(in) :: name
         !! Long name of a declared entry.
      logical, intent(out) :: value
         !! Parsed value; false when the conversion failed.
      type(error_t), intent(inout), optional :: err
         !! Set on an undeclared name or an unrecognised spelling.

      type(string_type) :: text
      character(len=:), allocatable :: lowered
      integer(default_int) :: i

      value = .false.
      call this%get_string(name, text, err)
      if (present(err)) then
         if (err%has_error()) return
      else
         if (this%find(name) == 0_default_int) return
      end if

      lowered = char(text)
      do i = 1_default_int, len(lowered, kind=default_int)
         if (lowered(i:i) >= "A" .and. lowered(i:i) <= "Z") then
            lowered(i:i) = achar(iachar(lowered(i:i)) + 32)
         end if
      end do

      select case (lowered)
      case ("true", "yes", "on", "1")
         value = .true.
      case ("false", "no", "off", "0", "")
         value = .false.
      case default
         call fail(err, ERROR_PARSE, "pic_cli: "//name//" is not a logical: "//lowered)
      end select
   end subroutine cli_get_logical