Resolve default arguments from the config file and CLI param defaults. Add an extra _args_given argument which determines if an argument was given or not. Args set from the config file count as given.
(self, kwargs)
| 155 | return '\n'.join(str(arg[key]) for key in self._arguments) |
| 156 | |
| 157 | def _get_args(self, kwargs): |
| 158 | ''' |
| 159 | Resolve default arguments from the config file and CLI param defaults. |
| 160 | |
| 161 | Add an extra _args_given argument which determines if an argument was given or not. |
| 162 | Args set from the config file count as given. |
| 163 | ''' |
| 164 | args_with_defaults = kwargs.copy() |
| 165 | # Add missing args from config file. |
| 166 | config_file = None |
| 167 | if 'config_file' in args_with_defaults and args_with_defaults['config_file'] is not None: |
| 168 | config_file = args_with_defaults['config_file'] |
| 169 | else: |
| 170 | config_file = self._config_file |
| 171 | args_given = {} |
| 172 | for key in self._arguments: |
| 173 | args_given[key] = not ( |
| 174 | not key in args_with_defaults or |
| 175 | args_with_defaults[key] is None or |
| 176 | self._arguments[key].nargs == '*' and args_with_defaults[key] == [] |
| 177 | ) |
| 178 | if config_file is not None and os.path.exists(config_file): |
| 179 | config_configs = {} |
| 180 | config = imp.load_source('config', config_file) |
| 181 | if self.extra_config_params is None: |
| 182 | config.set_args(config_configs) |
| 183 | else: |
| 184 | config.set_args(config_configs, self.extra_config_params) |
| 185 | for key in config_configs: |
| 186 | if key not in self._arguments: |
| 187 | raise Exception('Unknown key in config file: ' + key) |
| 188 | if not args_given[key]: |
| 189 | args_with_defaults[key] = config_configs[key] |
| 190 | args_given[key] = True |
| 191 | # Add missing args from hard-coded defaults. |
| 192 | for key in self._arguments: |
| 193 | argument = self._arguments[key] |
| 194 | if (not key in args_with_defaults) or args_with_defaults[key] is None: |
| 195 | if argument.optional: |
| 196 | args_with_defaults[key] = argument.default |
| 197 | else: |
| 198 | raise Exception('Value not given for mandatory argument: ' + key) |
| 199 | args_with_defaults['_args_given'] = args_given |
| 200 | if 'config_file' in args_with_defaults: |
| 201 | del args_with_defaults['config_file'] |
| 202 | return args_with_defaults |
| 203 | |
| 204 | def add_argument( |
| 205 | self, |