Skip to content

FLAPFortran command Line Arguments Parser

Describe your command line once, in pure Fortran 2018: FLAP parses it, reads the environment and the configuration files, checks every value, and writes the help, the errors, the man page and the shell completions.

Quick start โ€‹

A real session with a FLAP program: the help, the values and the errors all come from FLAP.

a terminal session of a FLAP program: its help, some runs, a typo and an out-of-range value

This is the whole program, in four steps: initialise, define, parse, get.

f90
program greet
!< Quick start: a positional, a ranged integer, a choice read from the environment too, a flag.
use flap
implicit none
type(command_line_interface) :: cli
character(32)                :: name, lang, hello
integer                      :: times, i, error
logical                      :: shout

call cli%init(progname='greet', version='v1.0', description='Greet someone, in a few languages', &
              error_color='red', error_style='bold_on')
call cli%add(positional=.true., position=1, help='Who to greet', required=.true., metavar='NAME')
call cli%add(switch='--times', switch_ab='-t', help='How many times', def='1', min='1', max='5', metavar='N', &
             help_color='cyan', help_style='bold_on')
call cli%add(switch='--lang', help='Language', def='en', choices='en,it,fr', envvar='GREET_LANG', metavar='LANG', &
             help_color='cyan', help_style='bold_on')
call cli%add(switch='--shout', help='Say it louder', act='store_true', def='.false.', &
             help_color='cyan', help_style='bold_on')
call cli%parse(error=error)                 ! prints the help, the version or the error by itself
if (error /= 0) stop 1, quiet=.true.
call cli%get(position=1, val=name, error=error)       ; if (error /= 0) stop 1, quiet=.true.
call cli%get(switch='--times', val=times, error=error) ; if (error /= 0) stop 1, quiet=.true.
call cli%get(switch='--lang', val=lang, error=error)   ; if (error /= 0) stop 1, quiet=.true.
call cli%get(switch='--shout', val=shout, error=error) ; if (error /= 0) stop 1, quiet=.true.

select case(trim(lang))
case('it') ; hello = 'Ciao'
case('fr') ; hello = 'Salut'
case default ; hello = 'Hello'
endselect
do i = 1, times
  print '(A)', trim(hello)//' '//trim(name)//merge('!', '.', shout)
enddo
endprogram greet

Grows with your program โ€‹

The same calls scale to commands, configuration files, a man page and shell completion. This help is generated from the definitions of the heat solver that the tutorial builds step by step; the colours are two keywords.

the help of a FLAP program

an error of a FLAP program

Learn FLAP step by step in the tutorial, find quick answers in the cookbook, look up every detail in the reference. Upgrading from v1.x? Read Upgrading.

Authors โ€‹

Contributions are welcome โ€” see the Contributing page.

Copyrights โ€‹

This project is distributed under a multi-licensing system:

Anyone interested in using, developing, or contributing to this project is welcome โ€” pick the license that best fits your needs.