libSUFR
a LIBrary of Some Useful Fortran Routines
Loading...
Searching...
No Matches
getopt_example.f90
1!> \file getopt_example.f90 Example code demonstrating the use of the libSUFR getopt() implementation in Fortran.
2
3!***********************************************************************************************************************************
4!> \brief Example code demonstrating the use of the libSUFR getopt() implementation in Fortran.
5
6program getopt_example
10
11 implicit none
12 character :: option, optStr*(99)
13
14 ! Set libSUFR constants:
16
17 ! Set getopt help output lines:
18 getopthelpheader = 'present some possibilities for the libSUFR getopt implementation'
19 getopthelpsyntax = '[long/short options] [positional arguments]'
20 ! getoptHelpFooter = 'this is a footer' ! Don't print a footer by keeping the string empty.
21
22 ! Set the option string for short options. Only specify the character after the dash (e.g. 'a' for -a). Characters followed
23 ! by a colon (:) have a required argument:
24 optstr = 'af:hx'
25
26 do ! scan all the command-line parameters
27
28 ! getopt() returns a single character" ">","!",".", or the short-option character (e.g. "a" for -a).
29 ! The following 'global' variables are set (accessible through the module SUFR_getopt):
30 ! - commandLine: the full command line as a single string
31 ! - curArg: the current argument or option
32 ! - optArg: the argument, if required and present
33
34 option = getopt(trim(optstr))
35
36 ! Do different things depending on the option returned:
37 select case(option)
38 case('>') ! Last parameter
39 if(command_argument_count().eq.0) call getopt_help(trim(optstr), 1,1) ! No parameters found - print help with 1 empty line before/after
40 exit
41 case('!') ! Unknown option (starting with "-" or "--")
42 write(*,'(A)') 'WARNING: unknown option: '//trim(optarg)//' Use -h for a list of valid options'
43 case('a')
44 write(*,'(A)') 'Found option: -'//option
45 case('f')
46 write(*,'(A)') 'Found option: -'//option//' '//trim(optarg)
47 case('h')
48 call getopt_help(trim(optstr))
49 stop
50 case('x')
51 write(*,'(A)') 'Found option: -'//option
52 case('.') ! Parameter is not an option (i.e., it doesn't start with "-" or "--")
53 write(*,'(A)') 'Found non-option element: '//trim(optarg)
54 case default
55 write(*,'(A)') 'Option ignored: -'//option
56 end select
57 end do
58
59end program getopt_example
60!***********************************************************************************************************************************
61
Provides all constants in the library, and routines to define them.
Definition constants.f90:23
subroutine set_sufr_constants
Define the values of all the constants used in this package.
Procedures for a getopt and getopt_long implementation to parse command-line parameters in Fortran.
Definition getopt.f90:50
character, dimension(999) optarg
The option's argument, if required and present.
Definition getopt.f90:59
character, dimension(999) getopthelpheader
The header line for the message printed by getopt(_long)_help()
Definition getopt.f90:63
character function getopt(optstr)
Parse a command-line parameter and return short options and their arguments. A warning is printed to ...
Definition getopt.f90:96
subroutine getopt_help(optstr, linebef, lineaft)
Print a help list of all short options and their required arguments.
Definition getopt.f90:574
character, dimension(999) getopthelpsyntax
The syntax line for the message printed by getopt(_long)_help()
Definition getopt.f90:64
Struct to define short and long options for getopt_long()
Definition getopt.f90:68