[UP]


Manual Reference Pages  - M_getopt (3)

NAME

M_getopt(3fm) - [ARGUMENTS:M_getopt] parse command line arguments. similar to those in standard C library. (LICENSE:GPL)

CONTENTS

Synopsis
Description
Options
Returns
Example
Copyright

SYNOPSIS

use M_getopt, only : getopt use M_getopt, only : option_s use M_getopt, only : optarg,optopt,optind

DESCRIPTION

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).

OPTIONS

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.

RETURNS

optopt
  is set to the option character, even if it isn’t recognized.
optarg
  is set to the option’s argument.
optind
  has the index of the next argument to process. Initially optind=1.
Grouped options are allowed, so "-abc" is the same as "-a -b -c".

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 type

The 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:
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
Differences for long options:
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

EXAMPLE

Sample program:

   program demo_getopts
   use M_getopt, only : getopt,option_s,optarg,optopt
   implicit none

character(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

Copyright 2008 by Mark Gates

This 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


M_getopt (3) March 11, 2021
Generated by manServer 1.08 from facbeaa7-4ccb-4ccf-9bae-e7496575fe14 using man macros.