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

Last change on this file since 3837 was 3837, checked in by jbclement, 9 hours ago

COMMON:
Follow-up of r3836 (addition of the new files).
For a review of the "--version" option, read https://lmdz-forge.lmd.jussieu.fr/mediawiki/Planets/index.php/Tool_Box#Printing_the_code_version_of_a_program or use the command-line option "--help" when running your program.
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
13use pgrm_version_mod,  only: print_pgrm_version
14use job_timelimit_mod, only: get_job_timelimit
15
16implicit none
17
18logical :: add_sso_fields = .false. ! Default: do not include SSO fields
19
20!=======================================================================
21contains
22!=======================================================================
23
24SUBROUTINE parse_args()
25
26implicit none
27
28!---- Arguments
29
30!---- Variables
31integer        :: narg, i, eq_pos
32character(256) :: arg, key, vlu
33
34!---- Code
35narg = command_argument_count() ! Get the number of command-line arguments
36if (narg == 0) return ! No option: normal/default case, nothing to do
37
38i = 1
39do 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
84enddo
85
86END SUBROUTINE parse_args
87!=======================================================================
88
89SUBROUTINE 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(*,*)
98END SUBROUTINE print_usage
99!=======================================================================
100
101PURE FUNCTION strip(s) RESULT(t)
102
103implicit none
104
105!---- Arguments
106character(*), intent(in) :: s
107character(len(s)) :: t
108
109!---- Variables
110
111!---- Code
112t = trim(adjustl(s))
113
114END FUNCTION
115!=======================================================================
116
117PURE FUNCTION to_lower(s) RESULT(low)
118
119implicit none
120
121!---- Arguments
122character(*), intent(in) :: s
123character(len(s)) :: low
124
125!---- Variables
126integer :: i
127
128!---- Code
129low = s
130do 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)
132enddo
133
134END FUNCTION to_lower
135
136
137END MODULE parse_args_mod
Note: See TracBrowser for help on using the repository browser.