-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_cmdline.f90
More file actions
113 lines (103 loc) · 3.4 KB
/
simple_cmdline.f90
File metadata and controls
113 lines (103 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
!==Class simple_cmdline
!
! simple_cmdline is for parsing command line arguments. The code is distributed with the hope that it will be useful,
! but _WITHOUT_ _ANY_ _WARRANTY_. Redistribution or modification is regulated by the GNU General Public License.
! *Author:* Hans Elmlund, 2011-08-17.
!
!==Changes are documented below
!
module simple_cmdline
implicit none
private :: NMAX, cmds, cmdarg
public
type cmdarg
character(len=32) :: key = ''
real :: rarg=0.
character(len=32) :: carg = ''
end type cmdarg
integer, parameter :: NMAX = 60
type(cmdarg) :: cmds(NMAX)
contains
subroutine parse_cmdline
character(len=32) :: arg
integer :: i, pos1, ri, ierr
do i=1,command_argument_count()
call get_command_argument(i, arg)
pos1 = index(arg, '=') ! position of '='
! parse everyting containing '='
if( pos1 /= 0 )then
cmds(i)%key = arg(:pos1-1) ! KEY
if( index(arg(pos1+1:), '.spi') /= 0 )then
cmds(i)%carg = arg(pos1+1:)
else if( index(arg(pos1+1:), '.fim') /= 0 )then
cmds(i)%carg = arg(pos1+1:)
else if( index(arg(pos1+1:), '.dat') /= 0 )then
cmds(i)%carg = arg(pos1+1:)
else if( index(arg(pos1+1:), '.log') /= 0 )then
cmds(i)%carg = arg(pos1+1:)
else if( index(arg(pos1+1:), '.') /= 0 )then
read(arg(pos1+1:),'(F6.3)') cmds(i)%rarg
else
read(arg(pos1+1:),'(I10)', iostat=ierr) ri
if( ierr==0 )then
cmds(i)%rarg = real(ri)
else
cmds(i)%carg = arg(pos1+1:)
endif
endif
endif
end do
end subroutine parse_cmdline
function defined_cmd_rarg( key ) result( def )
character(len=*), intent(in) :: key
logical :: def
integer :: i
def = .false.
do i=1,NMAX
if( cmds(i)%key == key )then
if( cmds(i)%rarg /= 0. )then
def = .true.
return
endif
endif
end do
end function defined_cmd_rarg
pure function get_cmd_rarg( key ) result( rval )
character(len=*), intent(in) :: key
real :: rval
integer :: i
rval = 0.
do i=1,NMAX
if( cmds(i)%key == key )then
rval = cmds(i)%rarg
return
endif
end do
end function get_cmd_rarg
function defined_cmd_carg( key ) result( def )
character(len=*), intent(in) :: key
logical :: def
integer :: i
def = .false.
do i=1,NMAX
if( cmds(i)%key == key )then
if( cmds(i)%carg /= '' )then
def = .true.
return
endif
endif
end do
end function defined_cmd_carg
pure function get_cmd_carg( key ) result( cval )
character(len=*), intent(in) :: key
character(len=32) :: cval
integer :: i
cval = ''
do i=1,NMAX
if( cmds(i)%key == key )then
cval = cmds(i)%carg
return
endif
end do
end function get_cmd_carg
end module simple_cmdline