def __init__(self, spec_dict, settings_folders, app_name, app_subfolder, subparser): self.app_name = app_name self.app_subfolder = app_subfolder self.settings_folders = settings_folders self.subparser = subparser # inject name to the spec_dict to handle it as regular subparser spec_dict['name'] = app_name self.spec_helper = helper.SpecDictHelper(spec_dict) # create parser self.parser = CliParser.create_parser(self, subparsers=subparser)
def __init__(self, subparser, spec_dict, vars_dir, defaults_dir): """ :param subparser: argparse.subparser to extend :param spec_dict: dict with CLI description :param vars_dir: Path to plugin's vars dir :param defaults_dir: Path to plugin's defaults dir """ self.vars = vars_dir self.defaults = defaults_dir self.spec_helper = helper.SpecDictHelper(spec_dict) # create parser self.parser = CliParser.create_parser(self, subparser)
def parse_args(self, arg_parser): """ Parses all the arguments (cli, file, env) and returns two dicts: * command arguments dict (arguments to control the IR logic) * nested arguments dict (arguments to pass to the playbooks) """ spec_defaults = self.get_spec_defaults() env_defaults = self.get_env_defaults() cli_args, unknown_args = CliParser.parse_args( self, arg_parser=arg_parser) file_args = self.get_config_file_args(cli_args) # generate config file and exit if self.generate_config_file(cli_args, spec_defaults): LOG.warning("Config file has been generated. Exiting.") return None # print warnings when something was overridden from non-cli source. self.validate_arg_sources( cli_args, env_defaults, file_args, spec_defaults) # todo(obaranov) Pass all the unknown arguments to the ansible # For now just raise exception if unknown_args: raise exceptions.IRUnrecognizedOptionsException(unknown_args) # merge defaults into one utils.dict_merge(spec_defaults, env_defaults) # now filter defaults to have only parser defined in cli defaults = {key: spec_defaults[key] for key in cli_args.keys() if key in spec_defaults} # copy cli args with the same name to all parser groups self._merge_duplicated_cli_args(cli_args) self._merge_duplicated_cli_args(file_args) utils.dict_merge(defaults, file_args) utils.dict_merge(defaults, cli_args) self.validate_requires_args(defaults) # now resolve complex types. self.resolve_custom_types(defaults) nested, control = self.get_nested_and_control_args(defaults) return nested, control, unknown_args
def parse_args(self, arg_parser, args=None): """Parses all the arguments (cli, answers file) :return: None, if ``--generate-answers-file`` in arg_arg_parser :return: (dict, dict): * command arguments dict (arguments to control the IR logic) * nested arguments dict (arguments to pass to the playbooks) """ spec_defaults = self.get_spec_defaults() cli_args = CliParser.parse_cli_input(arg_parser, args) file_args = self.get_answers_file_args(cli_args) # generate answers file and exit if self.generate_answers_file(cli_args, spec_defaults): LOG.warning("Answers file generated. Exiting.") # print warnings when something was overridden from non-cli source. self.validate_arg_sources(cli_args, file_args, spec_defaults) # print warnings for deprecated self.validate_arg_deprecation(cli_args, file_args) # now filter defaults to have only parser defined in cli defaults = dict((key, spec_defaults[key]) for key in cli_args.keys() if key in spec_defaults) # copy cli args with the same name to all parser groups self._merge_duplicated_cli_args(cli_args) self._merge_duplicated_cli_args(file_args) dict_utils.dict_merge(defaults, file_args) dict_utils.dict_merge(defaults, cli_args) self.validate_requires_args(defaults) self.validate_length_args(defaults) self.validate_choices_args(defaults) self.validate_min_max_args(defaults) # now resolve complex types. self.resolve_custom_types(defaults) nested, control, custom = \ self.get_nested_custom_and_control_args(defaults) return nested, control, custom
def parse_args(self, arg_parser, args=None): """Parses all the arguments (cli, answers file) :return: None, if ``--generate-answers-file`` in arg_arg_parser :return: (dict, dict): * command arguments dict (arguments to control the IR logic) * nested arguments dict (arguments to pass to the playbooks) """ spec_defaults = self.get_spec_defaults() cli_args = CliParser.parse_cli_input(arg_parser, args) file_args = self.get_answers_file_args(cli_args) # generate answers file and exit if self.generate_answers_file(cli_args, spec_defaults): LOG.warning("Answers file generated. Exiting.") # print warnings when something was overridden from non-cli source. self.validate_arg_sources(cli_args, file_args, spec_defaults) # print warnings for deprecated self.validate_arg_deprecation(cli_args, file_args) # now filter defaults to have only parser defined in cli defaults = dict((key, spec_defaults[key]) for key in cli_args.keys() if key in spec_defaults) # copy cli args with the same name to all parser groups self._merge_duplicated_cli_args(cli_args) self._merge_duplicated_cli_args(file_args) dict_utils.dict_merge(defaults, file_args) dict_utils.dict_merge(defaults, cli_args) self.validate_requires_args(defaults) self.validate_length_args(defaults) self.validate_choices_args(defaults) self.validate_min_max_args(defaults) # now resolve complex types. self.resolve_custom_types(defaults) nested, control = self.get_nested_and_control_args(defaults) return nested, control
def parse_args(self, arg_parser, args=None): """Parses all the arguments (cli, answers file) :return: None, if ``--generate-answers-file`` in arg_arg_parser :return: (dict, dict): * command arguments dict (arguments to control the IR logic) * nested arguments dict (arguments to pass to the playbooks) * custom_args custom arguments dict (arguments with custom ansible variables) """ spec_defaults, custom_spec_defaults = self.get_spec_defaults() cli_args, custom_cli_args = CliParser.parse_cli_input(arg_parser, args) helper_custom_cli_args = dict(custom_cli_args) # parse custom ansible variables and their values custom_parsed_cli_args = {} for custom_parse in custom_cli_args.values(): custom_parsed_cli_args.update(custom_parse) file_args, custom_file_args = self.get_answers_file_args(cli_args) # generate answers file and exit if self.generate_answers_file(cli_args, custom_cli_args, spec_defaults): LOG.warning("Answers file generated. Exiting.") # print warnings when something was overridden from non-cli source. self.validate_arg_sources(cli_args, file_args, spec_defaults) # print warnings for deprecated self.validate_arg_deprecation(cli_args, file_args) # now filter defaults to have only parser defined in cli defaults = dict((key, spec_defaults[key]) for key in cli_args.keys() if key in spec_defaults) # now filter custom ansible defaults to have values defined: for custom_default in custom_spec_defaults: if custom_default not in custom_parsed_cli_args: custom_parsed_cli_args[custom_default] = custom_spec_defaults[ custom_default] # copy cli args with the same name to all parser groups self._merge_duplicated_cli_args(cli_args) self._merge_duplicated_cli_args(file_args) dict_utils.dict_merge(defaults, file_args) # combine custom cli commands with custom file parsing if needed if custom_file_args: custom_file_args.update(custom_parsed_cli_args) custom_parsed_cli_args = custom_file_args dict_utils.dict_merge(defaults, cli_args) self.validate_requires_args(defaults, custom_parsed_cli_args) self.validate_length_args(defaults) self.validate_choices_args(defaults) self.validate_min_max_args(defaults) # now resolve complex types. self.resolve_custom_types(defaults, custom_cli_args, custom_file_args) nested, control = self.get_nested_and_control_args(defaults) # populate custom cli args with resolved complextypes when parsing from CLI if helper_custom_cli_args: for helper_arg in helper_custom_cli_args: resolved_custom_value = custom_cli_args[helper_arg] for custom_variable in helper_custom_cli_args[helper_arg]: custom_parsed_cli_args[ custom_variable] = resolved_custom_value return nested, control, custom_parsed_cli_args