Line data Source code
1 : !> \file mo_mhm_cli.f90
2 : !> \brief \copybrief mo_mhm_cli
3 : !> \details \copydetails mo_mhm_cli
4 :
5 : !> \brief Module to parse command line arguments of mHM.
6 : !> \version 0.1
7 : !> \authors Sebastian Mueller
8 : !> \date Oct 2021
9 : !> \details A simple parser for command line arguments for mHM.
10 : !! You can pass the path to mhm.nml, mhm_parameters.nml, mhm_outputs.nml and mrm_outputs.nml
11 : !!
12 : !! You can also pass the CWD as plain last argument and get a help or version text.
13 : !!
14 : !! \code{.sh}
15 : !! The mesoscale hydrological model - mHM
16 : !!
17 : !! Usage: mhm [options] <cwd>
18 : !!
19 : !! Options:
20 : !! <cwd>
21 : !! Description: The desired working directory (optional).
22 : !!
23 : !! --help / -h
24 : !! Description: Print this help message.
25 : !!
26 : !! --version / -V
27 : !! Description: Print the version of the program.
28 : !!
29 : !! --nml / -n <path>
30 : !! Description: The mHM configuration namelist.
31 : !! Default: mhm.nml
32 : !!
33 : !! --parameter / -p <path>
34 : !! Description: The mHM parameter namelist.
35 : !! Default: mhm_parameter.nml
36 : !!
37 : !! --mhm_output / -o <path>
38 : !! Description: The mHM output namelist.
39 : !! Default: mhm_output.nml
40 : !!
41 : !! --mrm_output / -r <path>
42 : !! Description: The mRM output namelist.
43 : !! Default: mrm_output.nml
44 : !!
45 : !! --quiet / -q
46 : !! Description: Decrease verbosity level.
47 : !! Can be repeated.
48 : !! \endcode
49 : !!
50 : !> \copyright Copyright 2005-\today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
51 : !! mHM is released under the LGPLv3+ license \license_note
52 : !> \ingroup f_mhm
53 : module mo_mhm_cli
54 :
55 : use mo_kind, only: i4
56 :
57 : implicit none
58 :
59 : private
60 :
61 : public :: parse_command_line
62 : public :: set_verbosity_level
63 :
64 : contains
65 :
66 : !> \brief parse the given command line arguments.
67 14 : subroutine parse_command_line()
68 : use mo_cli, only: cli_parser
69 : use mo_file, only: version, file_namelist_mhm, file_namelist_mhm_param, file_defOutput
70 : use mo_mrm_file, only: mrm_file_defOutput => file_defOutput
71 : use mo_os, only: change_dir
72 :
73 : implicit none
74 :
75 14 : type(cli_parser) :: parser
76 :
77 : parser = cli_parser( &
78 : description="The mesoscale hydrological model - mHM", &
79 : add_version_option=.true., &
80 14 : version=version)
81 :
82 : call parser%add_option( &
83 : name="cwd", &
84 : blank=.true., &
85 14 : help="The desired working directory (optional).")
86 :
87 : call parser%add_option( &
88 : name="nml", &
89 : s_name="n", &
90 : has_value=.true., &
91 : value_name="path", &
92 : default="mhm.nml", &
93 14 : help="The mHM configuration namelist.")
94 :
95 : call parser%add_option( &
96 : name="parameter", &
97 : s_name="p", &
98 : has_value=.true., &
99 : value_name="path", &
100 : default="mhm_parameter.nml", &
101 14 : help="The mHM parameter namelist.")
102 :
103 : call parser%add_option( &
104 : name="mhm_output", &
105 : s_name="o", &
106 : has_value=.true., &
107 : value_name="path", &
108 : default="mhm_outputs.nml", &
109 14 : help="The mHM output namelist.")
110 :
111 : call parser%add_option( &
112 : name="mrm_output", &
113 : s_name="r", &
114 : has_value=.true., &
115 : value_name="path", &
116 : default="mrm_outputs.nml", &
117 14 : help="The mRM output namelist.")
118 :
119 : call parser%add_option( &
120 : name="quiet", &
121 : s_name="q", &
122 : repeated=.true., &
123 14 : help="Decrease verbosity level.")
124 :
125 : ! parse given command line arguments
126 14 : call parser%parse()
127 :
128 : ! set nml file paths
129 14 : file_namelist_mhm = parser%option_value("nml")
130 14 : file_namelist_mhm_param = parser%option_value("parameter")
131 14 : file_defOutput = parser%option_value("mhm_output")
132 14 : mrm_file_defOutput = parser%option_value("mrm_output")
133 14 : call set_verbosity_level(2_i4 - parser%option_read_count("quiet"))
134 : ! change working directory first
135 14 : if (parser%option_was_read("cwd")) call change_dir(parser%option_value("cwd"))
136 :
137 126 : end subroutine parse_command_line
138 :
139 : !> \brief Set the verbosity level of mHM.
140 28 : subroutine set_verbosity_level(level)
141 14 : use mo_message, only: SHOW_MSG, SHOW_ERR
142 : implicit none
143 : integer(i4), intent(in), optional :: level !< verbosity level (0: no output, 1: errors, 2: all)
144 : integer(i4) :: level_
145 28 : SHOW_MSG = .false.
146 28 : SHOW_ERR = .false.
147 28 : level_ = 2_i4
148 28 : if ( present(level) ) level_ = level
149 14 : if ( level_ > 0 ) SHOW_ERR = .true.
150 28 : if ( level_ > 1 ) SHOW_MSG = .true.
151 28 : end subroutine set_verbosity_level
152 :
153 : end module mo_mhm_cli
|