Appearance
3. Lists and parameters
heat records the temperature at a probe (two coordinates), saves some fields, and lets the user override the physical parameters of the problem without editing an input file.
f90
! exactly two values: the coordinates of a probe
call cli%add(switch='--probe', help='Probe coordinates', required=.false., act='store', nargs='2', def='0.5 0.5', &
metavar='X')
! any number of values, each one a choice: the switch alone saves nothing
call cli%add(switch='--fields', help='Fields to save', required=.false., act='store', nargs='*', def='T', &
choices='T,flux', metavar='NAME')
! KEY=VALUE pairs overriding the physical parameters
call cli%add(switch='--set', help='Physical parameters', required=.false., act='store', nargs='+', map=.true., &
map_keys='alpha,t_end', def='alpha=1e-4')nargs='2': exactly two values, read into an array of two elements withget.nargs='*': any number of values, also none; read into an allocatable array withget_varying, which allocates it to the right size.choicesapply to every value.map=.true.:KEY=VALUEpairs;map_keyslists the keys allowed.get_map_valuereads one key, and leaves the variable untouched (found=.false.) when the key was not given: the built-in value stays.
f90
call cli%get(switch='--probe', val=probe, error=error) ; if (error /= 0) stop 1, quiet=.true.
call cli%get_varying(switch='--fields', val=fields, error=error) ; if (error /= 0) stop 1, quiet=.true.
alpha = 1.0d-4 ; t_end = 10.0d0 ! the built-in values
call cli%get_map_value(switch='--set', key='alpha', val=alpha, found=found, error=error)
call cli%get_map_value(switch='--set', key='t_end', val=t_end, found=found, error=error)$ heat --probe 0.5 0.25 --fields T flux --set alpha=2e-5 t_end=60
probe at 0.50 0.25
saving 2 field(s): T, flux
alpha = 2.0E-05, t_end = 60.0$ heat
probe at 0.50 0.50
saving 1 field(s): T
alpha = 1.0E-04, t_end = 10.0--fields alone is an empty list, not the default:
$ heat --fields
probe at 0.50 0.50
saving 0 field(s):
alpha = 1.0E-04, t_end = 10.0A misspelled key is caught by parse, with a suggestion:
$ heat --set alfa=1
heat: error: unknown key "alfa" for "--set" (allowed: alpha, t_end)! Did you mean "alpha"?
Try 'heat --help' for help.
[exit status 1]$ heat --help
usage: heat [--probe X#1 X#2] [--fields [NAME#1 NAME#2...]] [--set KEY=VALUE [KEY=VALUE...]] [--help] [--markdown] [--version]
Solve the 2D heat equation on a square plate
Optional switches:
--probe X#1 X#2
default value 0.5 0.5
Probe coordinates
--fields [NAME#1 NAME#2...]
choices: T, flux
default value T
Fields to save
--set KEY=VALUE [KEY=VALUE...]
keys: alpha, t_end
default value alpha=1e-4
Physical parameters
--help, -h
Print this help message
--markdown, -md
Save this help message in a Markdown file
--version, -v
Print versionWhat you learned
Fixed and variable lists, get_varying, maps with get_map_value. Reference: List-valued arguments, Key=value options.
Next: 4. Commands.