source: trunk/LMDZ.COMMON/libf/misc/parse_args_mod.F90 @ 4147

Last change on this file since 4147 was 4139, checked in by jbclement, 3 weeks ago

PEM:
Partial reversion of r4138: relocating "job_mod" to "LMDZ.COMMON/libf/misc/" because "parse_arg_mod" needs to know "job_mod" even if it is not the PEM.
JBC

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