Skip to content

Getting CLAs values from parsed CLI

Stefano Zaghi edited this page Mar 24, 2016 · 3 revisions

After the CLI has been parsed, the user is allowed to get any of the defined CLA value. Accordingly to the user-definition, a CLA value can be obtained either by the switch name (for named CLA) or by the CLA position (for positional CLA):

call cli%get(switch='-r', val=rval, error=err)

or

call cli%get(position=1, val=prval, error=err)

where rval and prval are two previously defined variables. Currently, the val variable can be only of types integer, real, logical and character. The complete API of cli%get is the following:

call cli%get(val, pref, args, group, switch, position, error)

where the signature of get is:

class(*),               intent(inout) :: val      !< CLA value.
character(*), optional, intent(in)    :: pref     !< Prefixing string.
character(*), optional, intent(in)    :: args     !< String containing command line arguments.
character(*), optional, intent(in)    :: group    !< Name of group (command) of CLAs.
character(*), optional, intent(in)    :: switch   !< Switch name.
integer(I4P), optional, intent(in)    :: position !< Position of positional CLA.
integer(I4P), optional, intent(out)   :: error    !< Error trapping flag.

The dummy arguments should be auto-explicative. Note that the switch passed can be also the abbreviated form if defined differently from the extended one. If no switch neither position is passed an error is arisen. Moreover, the type of the value returned is chosen accordingly to actual val argument passed: inside the get method val is an unlimited polymorphic variable which type is defined only when the user passes the actual val container.

Note that for multiple valued (list) CLA, the get method accept also array val:

character(*), optional, intent(in)::    pref     !< Prefixing string.
character(*), optional, intent(in)::    switch   !< Switch name.
integer(I4P), optional, intent(in)::    position !< Position of positional CLA.
class(*),               intent(inout):: val(1:)  !< CLA value.
integer(I4P),           intent(out)::   error    !< Error trapping flag.

however, the get method is invoked exactly with the same signature of single valued CLA as above: get is a generic, user-friendly method that automatically handles both scalar and array val variables.

Getting CLA array values with varying length

If you define a CLA having an unknown a priori number of arguments, i.e. you define a CLA with cli%add(nargs='*') or cli%add(nargs='+') (see CLI-Method-get_varying for more details), you must use the get_varying method instead of the get one, e.g.

use flap

type(command_line_interface) :: cli
integer, allocatable         :: ival(:)
integer, allocatable         :: err

call cli%init(...)
call cli%add(...)
call cli%parse(...)
call cli%get_varying(switch='-i', val=ival, error=err)
Clone this wiki locally