libSUFR
a LIBrary of Some Useful Fortran Routines
Loading...
Searching...
No Matches
getopt_long_example.f90
1!> \file getopt_long_example.f90 Example code demonstrating the use of the libSUFR getopt_long() implementation in Fortran.
2
3!***********************************************************************************************************************************
4!> \brief Example code demonstrating the use of the libSUFR getopt_long() implementation in Fortran.
5
6program getopt_long_example
8 use sufr_system, only: quit_program_error ! warn
11
12 implicit none
13 integer :: nPosArg
14 character :: option
15 logical, parameter :: getoptDebug = .true. ! Print getopt debug output
16
17 ! Set up the longopts struct to define the valid options: short option, long option, argument (0/1), short description:
18 type(getopt_t) :: longopts(5) = [ &
19 getopt_t('a', 'all', 0, 'Select all'), &
20 getopt_t('f', 'file', 1, 'Specify input file'), &
21 getopt_t('', '', 0, ''), & ! Add an empty line to the help output for better readability
22 getopt_t('h', 'help', 0, 'Print help'), &
23 getopt_t('', 'ignore', 0, '') ]
24
25
26 ! Set libSUFR constants:
28
29 ! Set getopt help output lines:
30 getopthelpheader = 'present some possibilities for the libSUFR getopt implementation'
31 getopthelpsyntax = '[long/short options] [positional arguments]'
32 ! getoptHelpFooter = 'this is a footer' ! Don't print a footer by keeping the string empty.
33
34 nposarg = 0
35 do ! scan all the command-line parameters
36
37 ! getopt_long() returns a single character" ">","!",".", or the short-option character (e.g. "a" for -a).
38 ! The following 'global' variables are set (accessible through the module SUFR_getopt):
39 ! - commandLine: the full command line as a single string
40 ! - curArg: the current argument or option
41 ! - longOption: the short or long option found, including leading dash(es)
42 ! - optArg: the option's argument, if required and found
43
44 option = getopt_long(longopts)
45
46 ! Do different things depending on the option returned:
47 select case(option)
48 case('>') ! Last parameter
49 if(command_argument_count().eq.0) call getopt_long_help(longopts, 1,1) ! No parameters found - print help with 1 empty line before/after
50 exit
51 case('!') ! Unknown option (starting with "-" or "--")
52 ! call warn('unknown option: '//trim(optArg)//' Use -h/--help for a list of valid options') ! Warn only
53 call quit_program_error('unknown option: '//trim(optarg)//' Use -h/--help for a list of valid options', 1) ! Throw error and abort
54 case('a')
55 if(getoptdebug) write(*,'(A)') 'Found option: '//trim(longoption)
56 case('f')
57 if(getoptdebug) write(*,'(A)') 'Found option: '//trim(longoption)//' '//trim(optarg)
58 case('h')
59 call getopt_long_help(longopts, 0,1) ! Print getopt_long help with 0 leading empty lines and 1 trailing empty line
60 stop
61 case('.') ! Parameter is not an option (i.e., it doesn't start with "-" or "--")
62 nposarg = nposarg + 1
63 if(getoptdebug) write(*,'(A,I0,A)') 'Found parameter ',nposarg,': '//trim(optarg)
64
65 case default ! None of the short options above match - try to match long options
66 select case(longoption)
67 case('--ignore') ! Note that --ignore was not given a short equivalent
68 if(getoptdebug) write(*,'(A)') 'Found option: '//trim(longoption)
69 case default
70 if(len_trim(longoption).ge.3) write(*,'(A)') 'Valid option unhandled: '//trim(longoption)
71 end select
72 end select
73 end do
74
75 if(getoptdebug) then
76 if(nposarg.eq.0) then
77 write(*,'(A)') 'No positional arguments found'
78 else
79 write(*,'(I0,A)') nposarg, ' positional arguments found'
80 end if
81 end if
82
83end program getopt_long_example
84!***********************************************************************************************************************************
85
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, dimension(longoptlen+2) longoption
The short or long option found, including leading dash(es)
Definition getopt.f90:60
subroutine getopt_long_help(longopts, linebef, lineaft)
Print a help list of all short/long options, their required arguments and their descriptions.
Definition getopt.f90:643
character function getopt_long(longopts)
Parse a command-line parameter and return short and/or long options and their arguments....
Definition getopt.f90:195
character, dimension(999) getopthelpsyntax
The syntax line for the message printed by getopt(_long)_help()
Definition getopt.f90:64
System-related procedures.
Definition system.f90:23
subroutine quit_program_error(message, status)
Print an error message to StdErr and stop the execution of the current program.
Definition system.f90:78
Struct to define short and long options for getopt_long()
Definition getopt.f90:68