M_CLI2

subroutineset_modeimpureelemental

impure elemental subroutine set_mode(key, mode, env_prefix)

NAME

set_mode(3) - [ARGUMENTS:M_CLI2] turn on optional modes+
(LICENSE:PD)

SYNOPSIS

subroutine set_mode(key,mode[,env_prefix])

 character(len=*),intent(in) :: key
 logical,intent(in),optional :: mode
 character(len=*),intent(in),optional :: env_prefix

DESCRIPTION

 Allow optional behaviors.

OPTIONS

KEY    name of option

The following KEY values are allowed:

o  response_file - enable use of response file

o  auto_response_file - enable use of response file
   but also act as if @$0 was entered on the command line
   where $0 is the basename of the file being executed

o  ignorelongcase - ignore case in long key names. So the user
   does not have to remember if the option is --CurtMode or --curtmode
   or --curtMode .

o  ignoreallcase - ignore case in long and short key names.
   This is similar to Powershell, which is case-insensitive.

o  dashunder  - treat dash in keyword as an underscore.
   So the user should not have to remember if the option is
   --ignore_case or --ignore-case.

o  nodashunder  - ignore dash and underscore in keywords.

o  strict - allow Boolean keys to be bundled, but requires
   a single dash prefix be used for short key names and long names
   must be prefixed with two dashes.

o  lastonly  - when multiple keywords occur keep the rightmost
   value specified instead of appending the values together.

o  env_fallback  - if a parameter is not specified check a
   corresponding environment variable for each option. The case
   of the options is ignored so all options should have long
   names, as there will be no way to distinguish between two
   short names composed of the same letter but different case.

MODE   set to .true. to activate the optional mode.
       Set to .false. to deactivate the mode.
       It is .true. by default.

ENV_PREFIX  The ENV_FALLBACK mode prefixes every option name
            with ENV_PREFIX and an underscore. The default prefix
            is the leaf name of the executing program. The derived
            names are always the long names and are always converted
            to uppercase.

EXAMPLES

Sample program:

program demo_set_mode
use M_CLI2,  only : set_args, lget, set_mode
implicit none
character(len=*),parameter :: all='(*(g0))'
   !
   ! enable use of response files
   call set_mode('response_file')
   !
   ! Any dash in a keyword is treated as an underscore
   call set_mode('underdash')
   !
   ! The case of long keywords are ignored.
   ! Values and short names remain case-sensitive
   call set_mode('ignorelongcase')
   ! The case of short and long keywords are ignored
   call set_mode('ignoreallcase')
   !
   ! short single-character boolean keys may be bundled
   ! but it is required that a single dash is used for
   ! short keys and a double dash for long keywords.
   call set_mode('strict')
   !
   ! every option will be able to have a default set via
   ! an environment variable of the name NAME_OPTION all
   ! in caps where name is the leaf name of the executable
   ! by default with any .suffix removed from the name
   ! and OPTION is the long name of the option. Because case is
   ! always ignored all options should have a unique longname.
   call set_mode('env_fallback')
   !
   ! if env_prefix is supplied it is used as then prefix for
   ! the environment variables
   call set_mode('env_fallback',env_prefix='MYNAME')
   !
   call set_args(' --switch_X:X F --switch-Y:Y F --ox:O F -t F -x F -o F')
   !
   ! show the results
   print all,'--switch_X or -X ... ',lget('switch_X')
   print all,'--switch_Y or -Y ... ',lget('switch_Y')
   print all,'--ox or -O       ... ',lget('ox')
   print all,'-o               ... ',lget('o')
   print all,'-x               ... ',lget('x')
   print all,'-t               ... ',lget('t')
end program demo_set_mode

AUTHOR

  John S. Urban, 2019

LICENSE

  Public Domain

Arguments

NameTypeIntentAttributesDescription
keycharacter(len=*)in
modelogicalinoptional
env_prefixcharacter(len=*)inoptional

Calls

  • upper
  • get_env
  • lower
  • journal

Source

Show the code
elemental impure subroutine set_mode(key,mode,env_prefix)
character(len=*),intent(in)          :: key
logical,intent(in),optional          :: mode
character(len=*),intent(in),optional :: env_prefix
logical                              :: local_mode
character(len=:),allocatable         :: debug_mode

   debug_mode= upper(get_env('CLI_DEBUG_MODE','FALSE'))//' '
   select case(debug_mode(1:1))
   case('Y','T')
      G_DEBUG=.true.
   end select

   if(present(mode))then
      local_mode=mode
   else
      local_mode=.true.
   endif

   select case(lower(key))
   case('response_file','response file'); CLI_RESPONSE_FILE=local_mode
   case('auto_response_file','auto response file'); CLI_AUTO_RESPONSE_FILE=local_mode
   case('debug');                         G_DEBUG=local_mode
   case('ignorecase','ignorelongcase');   G_IGNORELONGCASE=local_mode
   case('ignoreallcase');   G_IGNOREALLCASE=local_mode
   case('underdash','dashunder');         G_UNDERDASH=local_mode
   case('nodashunder','nounderdash');     G_NODASHUNDER=local_mode
   case('strict');                        G_STRICT=local_mode
   case('lastonly');                      G_APPEND=.not.local_mode
   case('env_fallback');                  G_ENV_FALLBACK=local_mode
   case default
      call journal('*set_mode* unknown key name ',key)
   end select

   if(present(env_prefix))then
      G_ENV_NAME=env_prefix
   endif

   if(G_DEBUG)write(*,gen)'<DEBUG>SET_MODE:END'

end subroutine set_mode

Graphs

Called by