ConfigParser¶
Bases: ArgumentParser
Parser to parse command-line arguments for CHANfiG.
ConfigParser
is a subclass of argparse.ArgumentParser
.
It provides a new parse
method to parse command-line arguments to CHANfiG.Config
object.
Different to ArgumentParser.parse_args
, ConfigParser.parse
will try to parse any command-line arguments,
even if they are not pre-defined by ArgumentParser.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.
Note that ArgumentParser.parse_args
method is not overridden in ConfigParser
.
This is because it is still possible to construct CHANfiG.Config
with ArgumentParser.parse_args
,
which has strict checking on command-line arguments.
Source code in chanfig/config.py
Python | |
---|---|
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
|
parse(args=None, config=None, default_config=None, no_default_config_action='raise')
¶
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 the function, - 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 |
Optional[Sequence[str]]
|
The arguments to parse. Defaults to sys.argv[1:]. |
None
|
config |
Optional[Config]
|
The base |
None
|
default_config |
Optional[str]
|
Path to the base config file. |
None
|
no_default_config_action |
str
|
What to do when |
'raise'
|
Returns:
Name | Type | Description |
---|---|---|
config |
Config
|
The parsed |
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |
Examples:
>>> p = ConfigParser()
>>> p.parse(['--i.d', '1013', '--f.n', 'chang']).dict()
{'i': {'d': 1013}, 'f': {'n': 'chang'}}
Values in command line overrides values in default_config
file.
>>> p = ConfigParser()
>>> p.parse(['--a', '2', '--config', 'example.yaml'], default_config='config').dict()
{'a': 2, 'b': 2, 'c': 3, 'config': 'example.yaml'}
Values in default_config
file overrides values in Config
object.
>>> c = Config(a=2)
>>> c.parse(['--config', 'example.yaml'], default_config='config').dict()
{'a': 1, 'b': 2, 'c': 3, 'config': 'example.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):
ValueError: default_config is set to config, but 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/config.py
Python | |
---|---|
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 |
|