M_getopt(3fm) - [ARGUMENTS:M_getopt::INTRO] parse command line arguments similarly to those in standard C library. (LICENSE:GPL)
Synopsis
Description
Options
Returns
Examples
Copyright
Usage:
use M_getopt, only : getopt use M_getopt, only : option_s use M_getopt, only : optarg,optopt,optind
ch = getopt( optstring, [longopts] )
Returns next option character from command line arguments.
o If an option is not recognized, it returns ?. o If no options are left, it returns a null character, char(0).
optstring contains characters that are recognized as options. If a character is followed by a colon, then it takes a
required argument. For example, "x" recognizes "-x", while "x:" recognizes "-x arg" or "-xarg". opterr Errors are printed by default. Set opterr=.false. to suppress them.
Grouped options are allowed, so "-abc" is the same as "-a -b -c".
optopt is set to the option character, even if it isnt recognized. optarg is set to the options argument. optind has the index of the next argument to process. Initially optind=1. If longopts is present, it is an array of type(option_s), where each entry describes one long option.
type option_s character(len=4096) :: name logical :: has_arg character :: val end typeThe name field is the option name, without the leading -- double dash. Set the has_arg field to true if it requires an argument, false if not. The val field is returned. Typically this is set to the corresponding short option, so short and long options can be processed together. (But there is no requirement that every long option has a short option, or vice-versa.)
Differences from C version:
Differences for long options:
o when options are finished, C version returns -1 instead of char(0), and thus stupidly requires an int instead of a char. o does not support optreset o does not support "--" as last argument o if no argument, optarg is blank, not NULL o argc and argv are implicit
o optional argument to getopt(), rather than separate function getopt_long() o has_arg is logical, and does not support optional_argument o does not support flag field (and thus always returns val) o does not support longindex o does not support "--opt=value" syntax, only "--opt value" o knows the length of longopts, so does not need an empty last record
Sample program:
program demo_getopts use M_getopt, only : getopt,option_s,optarg,optopt implicit nonecharacter(len=*),parameter :: OPTIONS=ab:c type(option_s):: opts(2) opts(1) = option_s( "alpha", .false., a ) opts(2) = option_s( "beta", .true., b ) do PARSE: select case( getopt( OPTIONS, opts )) case( char(0)) exit PARSE case( a ) print *, option alpha/a, optarg case( b ) print *, option beta/b=, optarg case( ? ) print *, unknown option , optopt, not in ,OPTIONS stop case default print *, unhandled option c , optopt, (an intentional bug) end select PARSE end do end program demo_getopts
Copyright 2008 by Mark GatesThis program is free software; you can redistribute or modify it under the terms of the GNU general public license (GPL), version 2 or later.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of merchantability or fitness for a particular purpose.
If you wish to incorporate this into non-GPL software, please contact me regarding licensing terms.
Slightly modified from original to integrate it into the GPF (General Purpose Fortran) format and create a man(1) page - JSU
Nemo Release 3.1 | M_getopt (3) | February 23, 2025 |