JU Home Page

Building a Fortran CLI (Command Line Interface)

The libCLI.a library and related MODULES contain procedures useful for creating a command line interface (CLI) for a Fortran program.

Large repositories of numeric algorithms written in Fortran exist at such sites as the netlib repository. This is a collection of primarily non-numeric Fortran procedures that can be combined to quickly create programs that allow shell-like input syntax.

Routine Categories Descriptions

command line arguments

M_KRACKEN(3f): command line arguments The M_KRACKEN(3f) module makes cracking Unix-like arguments off the command line easy
NAMELIST-based command line parser Using new features in 2003/2008 Fortran such as reading NAMELIST groups from internal files and GET_COMMAND_ARGUMENT(3f), crack command line options using a standardized syntax and the least code.
look-alikes for Fortran 2003 command-line routines Use the routines provided by Winteractor in the library "f2kcli" to make a standard way to crack the command line if your compiler does not yet support GET_COMMAND_ARGUMENT(3f).

command input

M_PARSE for cracking input files that are written as Unix-line commands This is a superset of the M_KRACKEN(3f) module that has features to let you read input data into your program that is similar in syntax to common Unix commands.


journaling, logging, and general messages routines that let you have a single routine filter output to journal files; display attention-grabbing messages or reformat messages. These routines are required by several of the other modules located here!


String manipulations convert case, change numbers to strings, strings to numbers, parse on delimiters,edit strings, ...

expression parsers

JUCALC(3f): evaluate expressions JUCALC(3f) evaluates simple numeric and string expressions. This allows your input to use expressions and variable names instead of simple values. You will have to comment out or supply some of the functions called, depending on how 2003-compliant your compiler is and whether you have the libJSU library on your system.
JUEXPR(3f): Evaluate simple expressions JUEXPR(3f) is an older simpler FORTRAN77 version of JUCALC(3f) that should compile as-is with any Fortran compiler. It is useful when you want to allow simple expressions in your input instead of just numeric values.

flow control

COND(3f): conditionally select input The M_LOGIC(3f) module allows you to use if/else/elseif/endif directives to your input; allowing conditional selection of input lines. Requires an expression parser. It uses JUCALC(3f) by default.

command line recall and editing

M_HISTORY(3f): An interactive input editor module Acting much like a line-mode editor, the REDO(3f) procedure lets you list, edit, save, and modify your interactively entered program input. Built-in help and no dependence on terminal control sequences makes this a simple-to-master and portable input history editor.
READLINE(3f): calling readline(3c) from Fortran The readline(3c) routine lets you edit and then execute previously entered commands. An interface using a small C routine and the standard ISO_C_BINDING module lets the commonly available C routine be called from Fortran. The readline(3c) library is required.

help utilities

Help Utility read USH-like Help files
VMS-like Help Utility read VMS-like Help files

hot keys

GETKEY(3f): read a character from the keyboard in "immediate mode" A simple C routine for most Unix and GNU/Linux systems that immediately reads a single character keystroke from the keyboard without waiting for a carriage return. An interface using the ISO_C_BINDING interface allows the routine to be called from Fortran.

Fortran/C calls

Simple calls between Fortran and C Some simple but commonly called C routines interfaced to Fortran. Ultimately, these will all be changed to use the ISO_C_BINDING module introduced as part of Fortran 2003.

An example program that combines many of the components into a simple calculator-like utility that parses command line arguments, evaluates Fortran-like expressions, has interactive command history recall and editing, and supports if/else/elseif/endif directives can be found in example.f90. To avoid having to download and build all the components first the same program is available as a single self-contained program in example_all.f90. This program can process input files such as ...

if gt(A,100)
   !echo BAD
   if eq(myvalue,0)
      !echo myvalue is 0!
   elseif lt(myvalue,0)
      !echo myvalue is less than 0!
   elseif gt(myvalue,0)
      !echo myvalue is greater than 0!
   !echo GOOD
$str("after first test B is ",B)
if gt(A,100)
   !echo GOOD
   !echo BAD
$str("after second test B is ",B)
_ _ _ ____ _ _ _ _ _ __| |__ __ _ _ __ (_) ___ ___| |_ / __ \ ___ ___ _ __ ___ ___ __ _ ___| |_ _ __ ___| |_ | | | | '__| '_ \ / _` | '_ \ | |/ _ \/ __| __/ / _` |/ __/ _ \| '_ ` _ \ / __/ _` / __| __| | '_ \ / _ \ __| | |_| | | | |_) | (_| | | | || | (_) \__ \ || | (_| | (_| (_) | | | | | | (_| (_| \__ \ |_ _| | | | __/ |_ \__,_|_| |_.__/ \__,_|_| |_|/ |\___/|___/\__\ \__,_|\___\___/|_| |_| |_|\___\__,_|___/\__(_)_| |_|\___|\__|