ConfigParser¶
chanfig.ConfigParser
¶
Bases: ArgumentParser
Parser to parse command-line arguments for CHANfiG.
ConfigParser
is a subclass of argparse.ArgumentParser
.
It provides new parse_config
and parse
method to parse command-line arguments to CHANfiG.Config
object.
parse_config
will read the configuration and determine possible arguments and their types.
This makes it more favourable than parse
as it has strict name checking.
parse
will try to parse any command-line arguments, even if they are not pre-defined by add_argument
.
This allows to relief the burden of adding tons of arguments for each tuneable parameter.
In the meantime, there is no mechanism to notify you if you made a typo in command-line arguments.
ConfigParser
override parse_args
method to ensure the output is a NestedDict
.
Source code in chanfig/parser.py
Python | |
---|---|
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
parse_config
¶
Python | |
---|---|
Parse the arguments for Config
.
You may optionally specify a name for default_config
,
and CHANfiG will read the file under this name.
There are three levels of config:
- The base
Config
parsed into this method, - The base config file located at the path of
default_config
(if specified), - The config specified in arguments.
Higher levels override lower levels (i.e. 3 > 2 > 1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Sequence[str] | None
|
Command-line arguments. Defaults to |
None
|
config |
NestedDict | None
|
existing configuration. |
None
|
default_config |
str | None
|
Path to default config file. Defaults to |
None
|
no_default_config_action |
str
|
Action when |
'raise'
|
Returns:
Name | Type | Description |
---|---|---|
config |
Config
|
The parsed |
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |
See Also
parse
: Parse all command-line arguments.
Examples:
Note that all examples uses NestedDict instead of Config for avoiding circular import.
>>> p = ConfigParser()
>>> p.parse_config(['--a', '1'], config=NestedDict(a=2)).dict()
{'a': 1}
You can only parse argument that is defined in Config
.
error: unrecognized arguments: –b 1
>>> p = ConfigParser()
>>> p.parse_config(['--b', '1'], config=NestedDict(a=2)).dict()
Traceback (most recent call last):
SystemExit: 2
Source code in chanfig/parser.py
parse
¶
Python | |
---|---|
Parse the arguments for Config
.
You may optionally specify a name for default_config
,
and CHANfiG will read the file under this name.
There are three levels of config:
- The base
Config
parsed into this method, - The base config file located at the path of
default_config
(if specified), - The config specified in arguments.
Higher levels override lower levels (i.e. 3 > 2 > 1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Sequence[str] | None
|
Command-line arguments. Defaults to |
None
|
config |
NestedDict | None
|
existing configuration. |
None
|
default_config |
str | None
|
Path to default config file. Defaults to |
None
|
no_default_config_action |
str
|
Action when |
'raise'
|
Returns:
Name | Type | Description |
---|---|---|
config |
Config
|
The parsed |
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |
See Also
parse_config
: Only parse valid config arguments.
Examples:
Note that all examples uses NestedDict instead of Config for avoiding circular import.
>>> p = ConfigParser()
>>> p.parse(['--i.d', '1016', '--f.n', 'chang']).dict()
{'i': {'d': 1016}, 'f': {'n': 'chang'}}
Values in command line overrides values in default_config
file.
>>> p = ConfigParser()
>>> p.parse(['--a', '2', '--config', 'tests/test.yaml'], default_config='config').dict()
{'a': 2, 'b': 2, 'c': 3, 'config': 'tests/test.yaml'}
Values in default_config
file overrides values in Config
object.
>>> p = ConfigParser()
>>> p.parse(['--config', 'tests/test.yaml'], config=NestedDict(a=2), default_config='config').dict()
{'a': 1, 'b': 2, 'c': 3, 'config': 'tests/test.yaml'}
ValueError will be raised when default_config
is specified but not presented in command line.
>>> p = ConfigParser()
>>> p.parse(['--a', '2'], default_config='config').dict()
Traceback (most recent call last):
RuntimeError: ConfigParser has default_config set to config, but it is not found in args.
ValueError will be suppressed when default_config
is specified bug not presented in command line,
and no_default_config_action
is set to ignore
or warn
.
>>> p = ConfigParser()
>>> p.parse(['--a', '2'], default_config='config', no_default_config_action='ignore').dict()
{'a': 2}
ValueError will be raised when no_default_config_action
is not in raise
, ignore
, and warn
.
>>> p = ConfigParser()
>>> p.parse(['--a', '2'], default_config='config', no_default_config_action='suppress').dict()
Traceback (most recent call last):
ValueError: no_default_config_action must be one of 'warn', 'ignore', 'raise', bug got suppress
Source code in chanfig/parser.py
Python | |
---|---|
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
parse_args
¶
Python | |
---|---|
|
Parse command line arguments and convert types.
This function first calls ArgumentParser.parse_args
to parse command line arguments.
It ensures the returned parsed values is stored in a NestedDict instance.
If eval_str
is specified, it also performs literal_eval
on all str
values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Sequence[str] | None
|
Command-line arguments. Defaults to |
None
|
namespace |
NestedDict | None
|
existing configuration. |
None
|
eval_str |
bool
|
Whether to evaluate string values. |
True
|