keyvars(n) 1.0 "flibs"

Name

keyvars - Input facility

Table Of Contents

Synopsis

Description

The keyvars module was inspired by the observations that auxiliary programs that you create for doing your job, such as converting one type of file into another, because that is more conventient for the task at hand, often require a small amount of input, say four or five parameters. It is easy enough to use a few read statements to read them from a file, but that often means fairly little checking, because it is "just" an auxiliary program, to be used by yourself only and documentation is more often than not only contained in the source code (or more honestly: the source code is the documentation). To solve the problem of documenting the input and checking that all is there, at least partly, you can use this module:

Here is a simple example:

program demo
    use keyvars
    implicit none
    integer :: x
    real    :: y
    character(len=20) :: string
    x = -1
    y = -1.0
    call get_values( 'keyvars.inp', [keyvar("int",  x, "Integer value"), &
                                     keyvar("real", y, "Real value"), &
                                     keyvar("char", string, "Some text")] )
    write(*,*) 'x = ', x
    write(*,*) 'y = ', y
    write(*,*) 'string = ', string
end program demo

The subroutine get_values first looks at the command-line arguments:

Note: The algorithm was implemented without any consideration of duplicate keys either in the array or the input file. What happens if a key is not unique therefore depends on the exact implementation.

ROUTINES

There are two public routines:

call get_values( filename, args )

This subroutine reads the given input file and stores the values it found in the variables defined in the array args. More details provided above.

character(len=*) filename

Name of the file to be read, unless overwritten via the -input option.

type(keyvar_data), dimension(:) args

Array specifying the variables as well as the associated keywords and descriptive texts. Construct or fill it using the keyvar function.

arg = keyvar( keyword, var, description )

This function returns a value of type keyvar_data. It fills the components of this derived type with a pointer to the variable var, so that it can be set automatically. The variable itself is one of the basic types: integer, single and double precision real, logical or a character string of any length.

character(len=*) keyword

The keyword by which the variable is to be found in the input.

scalar var

The variable to be assigned a value from the input file.

character(len=*) description

The description of the variable, serves as documentation in the template.

arg = keyvar( section, keyword, var, description )

Alternative form of the keyvar function. In this case you can specify the section the variable should come from. Section names appear in the input file as "[section]".

character(len=*) section

The section the keyword belongs to.

character(len=*) keyword

The keyword by which the variable is to be found in the input.

scalar var

The variable to be assigned a value from the input file.

character(len=*) description

The description of the variable, serves as documentation in the template.

Notes: