53 integer,
parameter,
private :: longOptLen = 99
69 character :: short =
''
70 character :: long*(longoptlen) =
''
72 character :: descr*(999) =
''
97 character,
intent(in) :: optstr*(*)
98 integer :: narg, optstri
99 character ::
getopt, option
125 if(
curarg(1:1).eq.
'-')
then
130 do optstri=1,len(optstr)
131 if(optstr(optstri:optstri).eq.option)
then
134 if(optstr(optstri+1:optstri+1).eq.
':')
then
135 if(len_trim(
curarg).gt.2)
then
142 if(
optcount.gt.narg .or.
optarg.eq.
'')
write(0,
'(A)')
'WARNING: option -'//option//
' requires an argument'
196 type(
getopt_t),
intent(in) :: longopts(:)
197 integer :: narg, opti, pos, debug=0
198 character ::
getopt_long, option, longopt*(longoptlen)
199 logical :: found, haseql
224 if(debug.ge.1)
write(*,
'(A,I0,A)')
'getopt_long(): option ',
optcount,
': '//trim(
curarg)
228 if(
curarg(1:2).eq.
'--')
then
229 longopt = trim(
curarg(3:))
233 pos = scan(trim(longopt),
'=')
235 optarg = trim(longopt(pos+1:))
236 longopt = longopt(1:pos-1)
241 do opti=1,
size(longopts)
242 if(longopts(opti)%long.eq.longopt)
then
245 if(longopts(opti)%reqArg.gt.0 .and. .not.haseql)
then
250 if(
optcount.gt.narg .or.
optarg.eq.
'')
write(0,
'(A)')
'WARNING: option --'//option//
' requires an argument'
252 else if(longopts(opti)%reqArg.eq.0 .and. haseql)
then
253 write(0,
'(A)')
'WARNING: option --'//option//
' does not require an argument'
268 else if(
curarg(1:1).eq.
'-')
then
272 do opti=1,
size(longopts)
273 if(longopts(opti)%short.eq.option)
then
276 if(longopts(opti)%reqArg.gt.0)
then
277 if(len_trim(
curarg).gt.2)
then
285 if(
optcount.gt.narg .or.
optarg.eq.
'')
write(0,
'(A)')
'WARNING: option -'//option//
' requires an argument'
307 if(debug.ge.1)
write(*,
'(2(A,I0))')
'optCount: ',
optcount,
' -> ',
optcount+1
359 integer,
intent(in) :: argNr
360 character,
intent(out) :: arg*(99)
361 integer :: iArg, in,din
393 integer,
intent(in),
optional :: minvalue,maxvalue
402 if(
present(minvalue))
then
404 ', but should not be lower than '//
int2str(minvalue)//
', aborting.', 1)
407 if(
present(maxvalue))
then
409 ', but should not be higher than '//
int2str(maxvalue)//
', aborting.', 1)
430 real,
intent(in),
optional :: minvalue,maxvalue
440 if(
present(minvalue))
then
442 ', but should not be lower than '//
real2str(minvalue, len_trim(
optarg))//
', aborting.', 1)
445 if(
present(maxvalue))
then
447 ', but should not be higher than '//
real2str(maxvalue, len_trim(
optarg))//
', aborting.', 1)
469 real(
double),
intent(in),
optional :: minvalue,maxvalue
479 if(
present(minvalue))
then
481 ', but should not be lower than '//
dbl2str(minvalue, len_trim(
optarg))//
', aborting.', 1)
484 if(
present(maxvalue))
then
486 ', but should not be higher than '//
dbl2str(maxvalue, len_trim(
optarg))//
', aborting.', 1)
502 character,
intent(inout) :: CLoptStr*(*)
503 integer :: iArg, in1,din, status, iChar,nChar, lverbose
504 character :: subStr*(99),newSubStr*(99)
512 din = index(trim(cloptstr(in1:)),
' ') - 1
514 if(lverbose.gt.0)
then
516 write(*,
'(A,3I5, A50)')
'iArg: ',iarg,in1,din,
'###'//trim(cloptstr(in1:))//
'###'
517 print*,len_trim(cloptstr),in1,din,len_trim(cloptstr(in1:)), in1+len_trim(cloptstr(in1:))-1
520 if(din.lt.0 .and. in1+len_trim(cloptstr(in1:))-1.eq.len_trim(cloptstr)) din = len_trim(cloptstr(in1:))
523 substr = cloptstr(in1:in1+din)
526 if(len_trim(substr).gt.2)
then
527 if(substr(1:1) .eq.
'-')
then
528 if(.not.substr(1:2) .eq.
'--')
then
529 read(substr,*, iostat=status)
dumdbl
533 if(lverbose.gt.0)
write(*,
'(A,4I5,2A25)')
' New group: ', iarg,in1,din,status,
'###'//cloptstr(in1:in1+din-1)//
'###',
'###'//substr(1:din)//
'###'
536 nchar = len_trim(substr)
539 newsubstr = trim(newsubstr)//
' -'//substr(ichar:ichar)
541 if(lverbose.gt.0) print*,
'New substring: ###'//trim(newsubstr)//
'###'
543 cloptstr = cloptstr(1:in1-2)//trim(newsubstr)//trim(cloptstr(in1+din:))
544 in1 = in1-2 + len_trim(newsubstr) - din
576 character,
intent(in) :: optStr*(*)
577 integer,
intent(in),
optional :: lineBef, lineAft
579 integer :: iLine,iChar
582 if(
present(linebef))
then
583 if(linebef.gt.0)
then
600 write(*,
'(A)', advance=
'no')
'Available options: '
601 do ichar=1,len_trim(optstr)
602 curchar = optstr(ichar:ichar)
604 if(curchar.eq.
':')
then
605 write(*,
'(A)', advance=
'no')
' <arg>'
607 if(ichar.gt.1)
write(*,
'(A)', advance=
'no')
','
608 write(*,
'(A)', advance=
'no')
' -'//curchar
619 if(
present(lineaft))
then
620 if(lineaft.gt.0)
then
645 type(
getopt_t),
intent(in) :: longopts(:)
646 integer,
intent(in),
optional :: lineBef, lineAft
648 integer :: iLine, iOpt, nChar, iSpc
651 if(
present(linebef))
then
652 if(linebef.gt.0)
then
671 write(*,
'(A)')
'Available options:'
672 do iopt=1,
size(longopts)
673 curopt = longopts(iopt)
677 if(trim(curopt%short).ne.
'')
then
678 write(*,
'(A4)', advance=
'no')
' -'//curopt%short
683 if(trim(curopt%long).ne.
'')
then
684 write(*,
'(A)', advance=
'no')
' --'//trim(curopt%long)
685 nchar = nchar + len_trim(curopt%long) + 3
689 if(curopt%reqArg.gt.0)
then
690 write(*,
'(A7)', advance=
'no')
' <arg>'
696 write(*,
'(1x)', advance=
'no')
700 write(*,
'(5x,A)') trim(curopt%descr)
710 if(
present(lineaft))
then
711 if(lineaft.gt.0)
then
Provides all constants in the library, and routines to define them.
character, dimension(199), public program_name
Name of the currently running program, without the path.
Module containing dummy variables for all kinds.
real(double) dumdbl
Dummy double.
Procedures for a getopt and getopt_long implementation to parse command-line parameters in Fortran.
subroutine getopt_get_command_argument(argnr, arg)
Returns the argNr-th command-line argument or option.
real function getopt_optarg_to_real(minvalue, maxvalue)
Extract and return a real value from the argument optArg. On error, report and abort.
character, dimension(999) optarg
The option's argument, if required and present.
character, dimension(999) getopthelpheader
The header line for the message printed by getopt(_long)_help()
integer, save optcount
The current option count.
integer numberofarguments
The number of arguments on the command line, excluding the command (= argc-1 in C)
character, dimension(longoptlen+2) longoption
The short or long option found, including leading dash(es)
character, dimension(999) getopthelpfooter
The footer line for the message printed by getopt(_long)_help()
subroutine getopt_long_help(longopts, linebef, lineaft)
Print a help list of all short/long options, their required arguments and their descriptions.
integer function getopt_optarg_to_int(minvalue, maxvalue)
Extract and return an integer value from the argument optArg. On error, report and abort.
subroutine getopt_split_short_options(cloptstr)
Split combined short command-line options into several individual ones, e.g. "... -abc ....
character function getopt(optstr)
Parse a command-line parameter and return short options and their arguments. A warning is printed to ...
character function getopt_long(longopts)
Parse a command-line parameter and return short and/or long options and their arguments....
real(double) function getopt_optarg_to_dbl(minvalue, maxvalue)
Extract and return a double value from the argument optArg. On error, report and abort.
subroutine getopt_get_command()
Get the full command line in a string, split compound short options (e.g. "-abc" -> "-a -b -c") and s...
subroutine getopt_help(optstr, linebef, lineaft)
Print a help list of all short options and their required arguments.
character, dimension(999) curarg
The current argument or option (word)
character, dimension(999) getopthelpsyntax
The syntax line for the message printed by getopt(_long)_help()
integer function getopt_command_argument_count()
Return the number of arguments on the command line (excluding the command).
character, dimension(9999) commandline
The full command line as a single string.
Provides kinds and related constants/routines.
integer, parameter double
Double-precision float. Precision = 15, range = 307.
System-related procedures.
subroutine quit_program_error(message, status)
Print an error message to StdErr and stop the execution of the current program.
Procedures to manipulate text/strings.
integer function count_substring(string, substr)
Count how many times a substring is present in a string.
pure character function, dimension(sign(1, floor(number)) -1) real2str(number, decim, mark)
Convert a single-precision real to a nice character string. Single-precision wrapper for dbl2str.
pure character function, dimension(sign(1_long, floor(number, long)) -1) dbl2str(number, decim, mark)
Convert a double-precision real to a nice character string. Difference with the F0 format descriptor:...
pure character function, dimension(sign(1, number) -1) int2str(number)
Convert an integer to a character string of the proper length.
Struct to define short and long options for getopt_long()