How to use the jsonargparse.ActionConfigFile function in jsonargparse

To help you get started, we’ve selected a few jsonargparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omni-us / jsonargparse / jsonargparse.py View on Github external
Returns:
            types.SimpleNamespace: An object with all parsed values as attributes.

        Raises:
            ParserError: If there is a parsing error and error_handler=None.
        """
        try:
            if env is None:
                env = dict(os.environ)
            cfg = {}  # type: ignore
            for action in self._actions:
                env_var = _get_env_var(self, action)
                if env_var in env and isinstance(action, ActionConfigFile):
                    namespace = _dict_to_flat_namespace(cfg)
                    ActionConfigFile._apply_config(self, namespace, action.dest, env[env_var])
                    cfg = vars(namespace)
            for action in self._actions:
                env_var = _get_env_var(self, action)
                if env_var in env and isinstance(action, ActionSubCommands):
                    env_val = env[env_var]
                    if env_val in action.choices:
                        cfg[action.dest] = subcommand = self._check_value_key(action, env_val, action.dest, cfg)
                        pcfg = action._name_parser_map[env_val].parse_env(env=env, defaults=defaults, nested=False, _skip_logging=True, _skip_check=True)  # type: ignore
                        for k, v in vars(pcfg).items():
                            cfg[subcommand+'.'+k] = v
            for action in [a for a in self._actions if a.default != SUPPRESS]:
                if isinstance(action, ActionParser):
                    subparser_cfg = {}
                    if defaults:
                        subparser_cfg = vars(action._parser.get_defaults(nested=False))
                    env_var = _get_env_var(self, action)
github Solvik / netbox_agent / netbox_agent / config.py View on Github external
def get_config():
    p = jsonargparse.ArgumentParser(
        default_config_files=[
            '/etc/netbox_agent.yaml',
            '~/.config/netbox_agent.yaml',
            '~/.netbox_agent.yaml',
        ],
        prog='netbox_agent',
        description="Netbox agent to run on your infrastructure's servers",
        env_prefix='NETBOX_AGENT_',
        default_env=True
    )
    p.add_argument('-c', '--config', action=jsonargparse.ActionConfigFile)

    p.add_argument('-r', '--register', action='store_true', help='Register server to Netbox')
    p.add_argument('-u', '--update-all', action='store_true', help='Update all infos in Netbox')
    p.add_argument('-d', '--debug', action='store_true', help='Print debug infos')
    p.add_argument('--update-network', action='store_true', help='Update network')
    p.add_argument('--update-inventory', action='store_true', help='Update inventory')
    p.add_argument('--update-location', action='store_true', help='Update location')
    p.add_argument('--update-psu', action='store_true', help='Update PSU')

    p.add_argument('--log_level', default='debug')
    p.add_argument('--netbox.url', help='Netbox URL')
    p.add_argument('--netbox.token', help='Netbox API Token')
    p.add_argument('--virtual.enabled', action='store_true', help='Is a virtual machine or not')
    p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
    p.add_argument('--hostname_cmd', default=None,
                   help="Command to output hostname, used as Device's name in netbox")
github omni-us / jsonargparse / jsonargparse.py View on Github external
nested (bool): Whether the namespace should be nested.
            with_meta (bool): Whether to include metadata in config object.

        Returns:
            types.SimpleNamespace: An object with all parsed values as attributes.

        Raises:
            ParserError: If there is a parsing error and error_handler=None.
        """
        try:
            if env is None:
                env = dict(os.environ)
            cfg = {}  # type: ignore
            for action in self._actions:
                env_var = _get_env_var(self, action)
                if env_var in env and isinstance(action, ActionConfigFile):
                    namespace = _dict_to_flat_namespace(cfg)
                    ActionConfigFile._apply_config(self, namespace, action.dest, env[env_var])
                    cfg = vars(namespace)
            for action in self._actions:
                env_var = _get_env_var(self, action)
                if env_var in env and isinstance(action, ActionSubCommands):
                    env_val = env[env_var]
                    if env_val in action.choices:
                        cfg[action.dest] = subcommand = self._check_value_key(action, env_val, action.dest, cfg)
                        pcfg = action._name_parser_map[env_val].parse_env(env=env, defaults=defaults, nested=False, _skip_logging=True, _skip_check=True)  # type: ignore
                        for k, v in vars(pcfg).items():
                            cfg[subcommand+'.'+k] = v
            for action in [a for a in self._actions if a.default != SUPPRESS]:
                if isinstance(action, ActionParser):
                    subparser_cfg = {}
                    if defaults: