Base Module
Base classes and helpers for Parameters and Groups
- author:
Doug Skrypa
- class cli_command_parser.parameters.base.ParamBase(name: str = None, required: Bool = False, help: str = None, hide: Bool = False)[source]
Bases:
ABC
Base class for
Parameter
andParamGroup
.- Parameters:
name¶ – The name to use for this parameter. Defaults to the name assigned to this parameter.
required¶ – Whether this parameter is required or not. If it is required, then an exception will be raised if the user did not provide a value for this parameter. Defaults to
False
.help¶ – A brief description of this parameter that will appear in
--help
text.hide¶ – If
True
, this parameter will not be included in usage / help messages. Defaults toFalse
.
- group: ParamGroup = None
The group this object is a member of, if any
- required: Bool
Whether this param/group is required
- hide: Bool
Whether this param/group should be hidden in
--help
text
- property formatter: ParamHelpFormatter[source]
- format_usage(*args, **kwargs) str [source]
Convenience method for calling
ParamHelpFormatter.format_usage()
- format_help(*args, **kwargs) str [source]
Convenience method for calling
ParamHelpFormatter.format_help()
- class cli_command_parser.parameters.base.Parameter(action: str, *, help: str = None, hide: Bool = False, metavar: str = None, name: str = None, required: Bool = False, default: Any = <object object>, default_cb: DefaultFunc = None, cb_with_cmd: Bool = False, show_default: Bool = None, strict_default: Bool = False)[source]
Bases:
ParamBase
,Generic
[T_co
],ABC
Base class for all other parameters. It is not meant to be used directly.
Custom parameter classes should generally extend
BasePositional
orBaseOption
instead of this, otherwise additional handling may be necessary in the parser.- Parameters:
action¶ – The action to take on individual parsed values. Actions must be defined as methods in classes that extend Parameter, and must be registered via
parameter_action
.name¶ – The name to use for this parameter. Defaults to the name assigned to this parameter.
required¶ – Whether this parameter is required or not. If it is required, then an exception will be raised if the user did not provide a value for this parameter. Defaults to
False
.metavar¶ – The name to use as a placeholder for values in usage / help messages.
help¶ – A brief description of this parameter that will appear in
--help
text.default¶ – The default value for this parameter if it is not specified. Defaults to
None
if this parameter is not required; not used if it is required.default_cb¶ – A default callback function (or other callable) may be provided instead of a static default value (they cannot both be provided). If
cb_with_cmd
is False (the default), then it must be callable with no arguments, otherwise it must accept a single positional argument (theCommand
that contains this Parameter). Similar to when thedefault
value would be used, it will only be called when no argument was provided. It is also possible to register a method in a Command to be the default callback.cb_with_cmd¶ – Whether the provided
default_cb
should be called with theCommand
that contains this Parameter. Ignored ifdefault_cb
is not provided.show_default¶ – Override the
CommandConfig.show_defaults
setting for this parameter to always or never include the default value in usage / help messages. Default: follow theshow_defaults
setting.hide¶ – If
True
, this parameter will not be included in usage / help messages. Defaults toFalse
.
- allow_leading_dash: AllowLeadingDash = 'numeric'
- classmethod __init_subclass__(repr_attrs: Strings = None, actions: Collection[Type[ParamAction]] = None, **kwargs)[source]
- default = <object object>
- default_cb: DefaultCallback | None = None
- strict_default: Bool = False
- show_default: Bool = None
- register_default_cb(method: Callable[[CommandObj], T_co]) Callable[[CommandObj], T_co] [source]
Intended to be used as a decorator to register a method in a Command to be used as the default callback for this Parameter. The method will only be called during parsing if no value was explicitly provided for this Parameter. The decorated method is returned unchanged, so it can still be called directly if necessary.
- Parameters:
method¶ – A method that does not accept any arguments (except
self
), and returns the value that should be used for this Parameter.- Returns:
The method, unchanged.
- class cli_command_parser.parameters.base.BasePositional(action: str, *, required: Bool = True, default: Any = <object object>, default_cb: DefaultFunc = None, **kwargs)[source]
-
Base class for
Positional
,SubCommand
,Action
, and any other parameters that are provided positionally, without prefixes. It is not meant to be used directly.All positional parameters are required by default.
Custom positional parameter classes should extend this class to be treated the same as other positionals by the parser.
- Parameters:
- class cli_command_parser.parameters.base.BaseOption(*option_strs: str, action: str, name_mode: Union[OptionNameMode, str, None] = <object object>, env_var: OptStrs = None, strict_env: bool = True, use_env_value: Bool = None, show_env_var: Bool = None, **kwargs)[source]
-
Base class for
Option
,Flag
,Counter
, and any other keyword-like parameters that have--long
and-short
prefixes before values.Only the handling for processing long/short options and formatting usage of these parameters is provided in this class - it is not meant to be used directly.
Custom option classes should extend this class to be treated the same as other options by the parser.
- Parameters:
option_strs¶ – The long and/or short option prefixes for this option. If no long prefixes are specified, then one will automatically be added based on the name assigned to this parameter.
action¶ – The action to take on individual parsed values. Actions must be defined as methods in classes that extend Parameter, and must be registered via
parameter_action
.name_mode¶ – Override the configured option_name_mode for this Option/Flag/Counter/etc.
env_var¶ – A string or sequence (tuple, list, etc) of strings representing environment variables that should be searched for a value when no value was provided via CLI. If a value was provided via CLI, then these variables will not be checked. If multiple env variable names/keys were provided, then they will be checked in the order that they were provided. When enabled, values from env variables take precedence over the default value. When enabled and the Parameter is required, then either a CLI value or an env var value must be provided.
strict_env¶ – When
True
(the default), if anBaseOption.env_var
is used as the source of a value for this Parameter and that value is invalid, then parsing will fail. WhenFalse
, invalid values from environment variables will be ignored (and a warning message will be logged).use_env_value¶ – For optional Parameters that support storing a constant (such as Flag), this option controls behavior when an
BaseOption.env_var
is used as the source of a value for this Parameter. IfTrue
, the parsed value will be stored as this Parameter’s value (it must be a valid value). IfFalse
(the default), then the parsed value will be used to determine whether the store/append const action should be taken as if it was specified as a CLI flag (e.g.,--foo
with no value).kwargs¶ – Additional keyword arguments to pass to
Parameter
.
- const = <object object>
- option_strs: OptionStrings
- strict_env: Bool
- env_var: OptStrs = None
- use_env_value: Bool
- show_env_var: Bool = None