## 25 June 2010

### How to parse Fortran command line arguments using NAMELIST

While searching around for a pure Fortran 90+ way to parse command line arguments and/or input files, I stumbled across Exploring NAMELIST by John S. Urban. John's article shows a way to use Fortran's NAMELIST feature to quickly parse arbitrary command line arguments while providing sane default behavior.

Like usual, I decided to overdevelop a simple idea. The complete sample is up on github. Assuming you've got an interface declaration in scope for get_command_arguments, here's what you'd see as an end consumer:

PROGRAM namelist_cli

IMPLICIT NONE

CHARACTER(LEN=255) :: string, message
INTEGER :: status

! Declare and initialize a NAMELIST for values of interest
INTEGER  :: i=1,   j=2
REAL     :: s=3.3, t=4.4
NAMELIST /cmd/ i, j, s, t

! Get command line arguments as a string ready for NAMELIST
CALL get_command_arguments(string, "&cmd", "/")