1 | MODULE parse_args_mod |
---|
2 | |
---|
3 | !*********************************************************************** |
---|
4 | ! DESCRIPTION: |
---|
5 | ! Provides a subroutine to parse command-line options. |
---|
6 | ! Recognizes: |
---|
7 | ! --help |
---|
8 | ! --version [file] |
---|
9 | ! --jobid <id> |
---|
10 | ! --add-sso |
---|
11 | !*********************************************************************** |
---|
12 | |
---|
13 | use pgrm_version_mod, only: print_pgrm_version |
---|
14 | use job_timelimit_mod, only: get_job_timelimit |
---|
15 | |
---|
16 | implicit none |
---|
17 | |
---|
18 | logical :: add_sso_fields = .false. ! Default: do not include SSO fields |
---|
19 | |
---|
20 | !======================================================================= |
---|
21 | contains |
---|
22 | !======================================================================= |
---|
23 | |
---|
24 | SUBROUTINE parse_args() |
---|
25 | |
---|
26 | implicit none |
---|
27 | |
---|
28 | !---- Arguments |
---|
29 | |
---|
30 | !---- Variables |
---|
31 | integer :: narg, i, eq_pos |
---|
32 | character(256) :: arg, key, vlu |
---|
33 | |
---|
34 | !---- Code |
---|
35 | narg = command_argument_count() ! Get the number of command-line arguments |
---|
36 | if (narg == 0) return ! No option: normal/default case, nothing to do |
---|
37 | |
---|
38 | i = 1 |
---|
39 | do while (i <= narg) |
---|
40 | call get_command_argument(i,arg) ! Read the argument given to the program |
---|
41 | |
---|
42 | eq_pos = index(arg,'=') |
---|
43 | if (eq_pos > 0) then ! Handle "--keyword=value" style |
---|
44 | key = strip(arg(:eq_pos - 1)) |
---|
45 | vlu = strip(arg(eq_pos + 1:)) |
---|
46 | else ! Handle "--keyword [value]" style |
---|
47 | key = strip(arg) |
---|
48 | vlu = '' |
---|
49 | if (i < narg) then |
---|
50 | call get_command_argument(i + 1,arg) ! Read the argument given to the program |
---|
51 | if (len_trim(adjustl(arg)) > 0 .and. arg(1:2) /= '--') then |
---|
52 | vlu = strip(arg) |
---|
53 | i = i + 1 ! To skip the value argument next time |
---|
54 | endif |
---|
55 | endif |
---|
56 | endif |
---|
57 | |
---|
58 | select case (to_lower(key)) |
---|
59 | case('--help') |
---|
60 | call print_usage() |
---|
61 | call exit(0) |
---|
62 | |
---|
63 | case ('--version') |
---|
64 | if (len_trim(adjustl(vlu)) > 0) then |
---|
65 | call print_pgrm_version(vlu) |
---|
66 | else |
---|
67 | call print_pgrm_version() |
---|
68 | endif |
---|
69 | call exit(0) |
---|
70 | |
---|
71 | case ('--add-sso') |
---|
72 | add_sso_fields = .true. |
---|
73 | write(*,*) 'SSO fields will be included in "start_archive.nc"' |
---|
74 | |
---|
75 | case ('--jobid') |
---|
76 | call get_job_timelimit(vlu) |
---|
77 | |
---|
78 | case default |
---|
79 | write(*,*) 'Error: unknown option "', key, '".' |
---|
80 | call exit(1) |
---|
81 | end select |
---|
82 | |
---|
83 | i = i + 1 |
---|
84 | enddo |
---|
85 | |
---|
86 | END SUBROUTINE parse_args |
---|
87 | !======================================================================= |
---|
88 | |
---|
89 | SUBROUTINE print_usage() |
---|
90 | write(*,*) |
---|
91 | write(*,*) 'Usage: program [options]' |
---|
92 | write(*,*) ' --help Show this help message and exit' |
---|
93 | write(*,*) ' --version [file] Print program version and exit (optional output file)' |
---|
94 | write(*,*) ' --add-sso Add SSO fields to "start_archive.nc" (only available for Mars start2archive)' |
---|
95 | write(*,*) ' --jobid <id> Query the time limit for the specified job ID,' |
---|
96 | write(*,*) ' enabling self-termination before timeout (only available for the PEM)' |
---|
97 | write(*,*) |
---|
98 | END SUBROUTINE print_usage |
---|
99 | !======================================================================= |
---|
100 | |
---|
101 | PURE FUNCTION strip(s) RESULT(t) |
---|
102 | |
---|
103 | implicit none |
---|
104 | |
---|
105 | !---- Arguments |
---|
106 | character(*), intent(in) :: s |
---|
107 | character(len(s)) :: t |
---|
108 | |
---|
109 | !---- Variables |
---|
110 | |
---|
111 | !---- Code |
---|
112 | t = trim(adjustl(s)) |
---|
113 | |
---|
114 | END FUNCTION |
---|
115 | !======================================================================= |
---|
116 | |
---|
117 | PURE FUNCTION to_lower(s) RESULT(low) |
---|
118 | |
---|
119 | implicit none |
---|
120 | |
---|
121 | !---- Arguments |
---|
122 | character(*), intent(in) :: s |
---|
123 | character(len(s)) :: low |
---|
124 | |
---|
125 | !---- Variables |
---|
126 | integer :: i |
---|
127 | |
---|
128 | !---- Code |
---|
129 | low = s |
---|
130 | do i = 1,len(s) |
---|
131 | if (iachar(s(i:i)) >= iachar('A') .and. iachar(s(i:i)) <= iachar('Z')) low(i:i) = achar(iachar(s(i:i)) + 32) |
---|
132 | enddo |
---|
133 | |
---|
134 | END FUNCTION to_lower |
---|
135 | |
---|
136 | |
---|
137 | END MODULE parse_args_mod |
---|