Skip to content

Error Codes

Every FLAP method that can fail accepts an optional error integer argument. Check it after each call to detect problems early.

fortran
call cli%add(switch='--output', ..., error=error)
if (error /= 0) then
  print '(A,I0)', 'CLI definition error: ', error
  stop
end if

Code table

CodeMeaningTypical cause
-2Help printed--help / -h was passed — not a real error
-1Version printed--version / -v was passed — not a real error
0SuccessNo error
1Missing default for optional argumentAdded required=.false. but omitted def=
2Required argument cannot use excluderequired=.true. combined with exclude=
3Positional argument cannot use excludepositional=.true. combined with exclude=
4Named argument has no switchNon-positional add called without switch=
5Positional argument has no positionpositional=.true. without position=
6Positional argument must use act='store'Incompatible action for a positional CLA
7Value not in choices listUser supplied a value outside the allowed set
8Required argument missingA required=.true. argument was not passed
9Two mutually exclusive arguments both passedBoth sides of an exclude= pair were given
10Cast to logical failedCLA value string cannot be parsed as logical
11choices not allowed for logical typechoices= used with a boolean argument
12Argument is not list-valuedget used with an array on a scalar argument
13Insufficient list argumentsnargs='N' but fewer than N values were passed
14Missing valueA named argument was passed but no value followed
15Unknown switchAn unrecognised switch was passed on the command line
16envvar not allowed for positionalenvvar= combined with positional=.true.
17envvar requires act='store'Environment variable used with an incompatible action
18envvar not allowed for list-valuedenvvar= combined with nargs=
19act='store*' not allowed for positionalIncompatible combination
20act='store*' not allowed for list-valuedIncompatible combination
21act='store*' not allowed with envvarIncompatible combination
22Unknown actionact= set to an unrecognised string
23Group (command) consistency brokenInternal group definition error
24Two mutually exclusive groups both passedBoth sides of set_mutually_exclusive_groups given
25CLA not found in CLIget or is_passed called for an undefined switch
26Group not found in CLIget or run_command called for an undefined group
27CLA selection failingInternal retrieval error
28Insufficient arguments for CLIToo few arguments on the command line

Handling status codes

Codes -2 and -1 indicate that FLAP printed help or version text respectively. Depending on your program structure you may want to handle these differently from true errors:

fortran
call cli%parse(error=error)
select case (error)
  case (0)
    ! normal execution
  case (-1, -2)
    stop  ! help or version was printed — exit cleanly
  case default
    write(*,'(A,I0)') 'Parse error: ', error
    stop 1
end select

Accessing the error message

command_line_interface has a public error_message attribute that contains a human-readable description of the last error:

fortran
call cli%parse(error=error)
if (error /= 0) then
  write(*,'(A)') trim(cli%error_message)
  stop 1
end if