7
subroutine getopts(optionstring,name,optarg,optind,exitcode)
8
!$$$ Subprogram Documentation Block
10
! Subprogram: Getopts Process command line arguments for valid options
11
! Prgmmr: Iredell Org: W/NP23 Date: 2000-08-22
13
! Abstract: This subprogram processes command-line arguments for valid options.
14
! It is the Fortran equivalent of the built-in shell command getopts.
15
! Options on the command line come before the positional arguments.
16
! Options are preceded by a - (minus sign) or a + (plus sign).
17
! Options are single case-sensitive alphanumeric characters.
18
! Options either do or do not have an expected argument.
19
! Options without an argument may be immediately succeeded by
20
! further options without the accompanying - or + prefix.
21
! Options with an argument may be separated from their argument
22
! by zero or more blanks. The argument cannot end with a blank.
23
! Options end when not preceded by a - or a + or after -- or ++.
24
! This subprogram processes one option per invocation.
25
! This subprogram is not thread-safe.
27
! Program History Log:
28
! 2007-05-05 Alberto Garcia: Use f2k-compliant f2kcli module
31
! Usage: call getopts(optionstring,name,optarg,optind,exitcode)
33
! Input Argument List:
35
! character string containing a list of all valid options;
36
! options succeeded by a : require an argument
38
! Input and Output Argument List:
40
! integer index of the next argument to be processed;
41
! set to 0 before initial call or to restart processing
43
! Output Argument List:
45
! character string containing the name of the next option
46
! or ? if no option or an unknown option is found
47
! or : if an option had a missing required argument;
48
! a + is prepended to the value in name if the option begins with a +
50
! character string containing the option argument if required
51
! or null if not required or not found;
52
! optarg contains the option found if name is ? or :.
54
! integer return code (0 if an option was found, 1 if end of options)
57
! command_argument_count
58
! Retrieve number of command-line arguments
59
! get_command_argument
60
! Retrieve a command-line argument
62
! Retrieve the starting position of a substring within a string
65
! Here is an example of how to use this subprogram.
66
! -----------------------------------------------------------
68
! character(len=20) copt,carg,cb,cpos
69
! integer ia,ib,iopt,iret,narg,npos,ipos
71
! ia=0 ! Programmer's flag for option a
76
! call getopts('ab:',copt,carg,iopt,iret)
85
! print *,'invalid option ',carg(1:1)
89
! if(ia.eq.1) print *,'option a selected'
90
! if(ib.eq.1) print *,'option b selected; argument=',cb
92
! Now process positional parameters
93
! narg=command_argument_count()
96
! call get_command_argument(number=ipos+iopt-1,value=cpos)
97
! print *,'positional argument ',ipos,' is ',cpos
100
! -----------------------------------------------------------
103
! Language: Fortran 90
109
character(len=*),intent(in) :: optionstring
110
character(len=*),intent(out) :: name
111
character(len=*),intent(out) :: optarg
112
integer, intent(inout) :: optind
113
integer, intent(out) :: exitcode
116
character(len=256), save :: carg
117
character(len=1) , save :: cone
118
integer,save :: narg,larg,lcur
121
character(len=1) :: copt
122
integer :: lname,lopt
124
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125
! Initially set saved data.
128
narg=command_argument_count()
134
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135
! Retrieve next command-line argument if necessary;
136
! exit if at end of options
137
if(lcur.gt.larg) then
139
if(optind.gt.narg) then
145
call get_command_argument(number=optind,value=carg)
149
if(larg.eq.1.or.(cone.ne.'-'.and.cone.ne.'+')) then
154
elseif(larg.eq.2.and.carg(2:2).eq.cone) then
162
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
163
! Find next option in the list; exit if option is unknown
167
lopt=index(optionstring,copt)
173
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
174
! Option found; retrieve its argument if requested
182
name(lname:lname)=copt
184
if(lopt.lt.len(optionstring)) then
185
if (optionstring(lopt+1:lopt+1).eq.':') then
186
if(lcur.gt.larg) then
188
if(optind.gt.narg) then
193
call get_command_argument(number=optind,value=carg)
197
optarg=carg(lcur:larg)