Parsers¶
Interfacy is the programmatic entry point.
Interfacy¶
from interfacy import Interfacy
Interfacy().run(main)
- class Interfacy[source]¶
Build and run command-line interfaces from Python callables.
Register functions, classes, or command groups, and Interfacy turns their names, type annotations, and docstrings into commands, options, and help output. Use
command()as a decorator,add_command()for explicit registration, orrun()when you want to register and execute in one step.- Parameters:
description – Text shown before commands and options in help output.
epilog – Text shown after generated help output.
type_parser – Registry used to convert raw CLI strings into annotated types.
help_layout – Layout configuration for generated help output.
backend – Parser implementation to use. Must be
"argparse"or"click".help_colors – Color theme applied by the help layout.
run – Run backend setup immediately when supported by the selected backend.
print_result – Print returned command values after execution.
tab_completion – Install shell completion support when the backend supports it.
full_error_traceback – Include full tracebacks for runtime errors.
allow_args_from_file – Enable
@fileargument expansion.sys_exit_enabled – Call
sys.exitfor parser exits instead of returning codes.flag_strategy – Strategy for deriving option flags from Python names.
abbreviation_gen – Generator used for short option flags.
abbreviation_max_generated_len – Maximum generated short-flag length. Must be >= 1.
abbreviation_scope – Scope where generated short flags may be reused.
help_option_sort – Rules for ordering option help entries.
help_subcommand_sort – Rules for ordering subcommand help entries.
help_position – Absolute help-description column.
executable_flags – Root-level flags that run without a command.
pipe_targets – Default stdin routing configuration.
print_result_func – Callable used when
print_resultis enabled.include_inherited_methods – Include inherited methods when registering classes.
include_protected_methods – Include protected methods when registering classes.
include_private_methods – Include private methods when registering classes.
include_staticmethods – Include static methods when registering classes.
include_classmethods – Include class methods when registering classes.
on_interrupt – Callback invoked for handled
KeyboardInterruptinstances.silent_interrupt – Suppress interrupt log output.
reraise_interrupt – Raise handled interrupts after logging or callbacks.
expand_model_params – Expand supported model parameters into nested CLI flags.
model_expansion_max_depth – Maximum model expansion depth. Must be >= 1.
bool_negative_prefix – Prefix used for generated negative boolean flags.
plugins – Plugins to register during initialization.
method_skips – Class method names to skip when registering class commands.
parse_recovery_max_attempts – Maximum plugin recovery attempts. Must be >= 0.
formatter_class – Argparse help formatter class. Only valid with
backend="argparse".
- Raises:
ConfigurationError – If
backendis unsupported orformatter_classis used withbackend="click".ImportError – If
backend="click"is requested without Click installed.
- add_plugin(plugin)[source]¶
Register a plugin on the active backend parser.
- Raises:
DuplicatePluginError – If another plugin with the same parser-local name exists.
- add_type_parser(typ, parser)[source]¶
Register a converter for an annotation type.
The converter receives one raw CLI string and returns the value passed to the command callable.
- property type_parser: strto.StrToTypeParser¶
Return the active type parser registry.
- apply_setup(
- *,
- help_layout=None,
- help_colors=None,
- type_parser=None,
- print_result=None,
- tab_completion=None,
- full_error_traceback=None,
- allow_args_from_file=None,
- flag_strategy=None,
- abbreviation_gen=None,
- abbreviation_max_generated_len=None,
- abbreviation_scope=None,
- help_option_sort=None,
- help_subcommand_sort=None,
- help_position=None,
- include_inherited_methods=None,
- include_protected_methods=None,
- include_private_methods=None,
- include_staticmethods=None,
- include_classmethods=None,
- silent_interrupt=None,
- expand_model_params=None,
- model_expansion_max_depth=None,
- bool_negative_prefix=None,
- plugins=None,
- method_skips=None,
- parse_recovery_max_attempts=None,
Update parser defaults used by later registrations and parser builds.
Arguments left as
Nonekeep their current value. Plugins supplied here are registered immediately.- Raises:
ConfigurationError – If an update is invalid for the current parser state.
- add_command(
- command,
- name=None,
- description=None,
- aliases=None,
- pipe_targets=None,
- include_inherited_methods=None,
- include_protected_methods=None,
- include_private_methods=None,
- include_staticmethods=None,
- include_classmethods=None,
- expand_model_params=None,
- model_expansion_max_depth=None,
- abbreviation_scope=None,
- executable_flags=None,
- help_option_sort=None,
- help_subcommand_sort=None,
- help_group=None,
- method_skips=None,
Register a function, class, instance, or command group.
Per-command options override parser defaults for this command only. If
nameis omitted, the CLI name is derived from the command target.description,aliases,pipe_targets, andhelp_groupcustomize the public CLI surface without changing the underlying callable.- Raises:
DuplicateCommandError – If the command name or alias already exists.
InvalidCommandError – If
commandcannot be converted to a CLI command.ConfigurationError – If an override is invalid.
- command(
- name=None,
- description=None,
- aliases=None,
- pipe_targets=None,
- include_inherited_methods=None,
- include_protected_methods=None,
- include_private_methods=None,
- include_staticmethods=None,
- include_classmethods=None,
- expand_model_params=None,
- model_expansion_max_depth=None,
- abbreviation_scope=None,
- executable_flags=None,
- help_option_sort=None,
- help_subcommand_sort=None,
- help_group=None,
- method_skips=None,
Return a decorator that registers a function or class as a command.
Options passed to the decorator override parser defaults for the decorated target only. The decorated object is returned unchanged.
- add_group(
- group,
- name=None,
- description=None,
- aliases=None,
- include_inherited_methods=None,
- include_protected_methods=None,
- include_private_methods=None,
- include_staticmethods=None,
- include_classmethods=None,
- expand_model_params=None,
- model_expansion_max_depth=None,
- abbreviation_scope=None,
- executable_flags=None,
- help_option_sort=None,
- help_subcommand_sort=None,
- help_group=None,
- method_skips=None,
Register an explicit command group.
Group options override parser defaults for commands created from this group. If
nameis omitted, the CLI name is derived from the group.description,aliases, andhelp_groupcustomize how the group is presented in generated help and command resolution.- Raises:
DuplicateCommandError – If the group name or alias already exists.
ConfigurationError – If an override is invalid.
- get_command_by_cli_name(cli_name)[source]¶
Resolve a registered command by CLI name or alias.
- Raises:
InvalidCommandError – If
cli_namedoes not resolve to a registered command.
- pipe_to(
- targets,
- *,
- command=None,
- subcommand=None,
- **normalization_kwargs,
Configure stdin pipe routing for the parser or a command.
Without
command, the targets become parser defaults. Withcommandand optionallysubcommand, they apply only to that command path.normalization_kwargsare passed through to pipe-target normalization.- Raises:
ConfigurationError – If the pipe target declaration is invalid.
- parser_from_command(command, main=False)[source]¶
Build a backend parser from an inspected command.
maintells the backend to build the command as the root parser rather than as a nested command parser.- Raises:
InvalidCommandError – If
commandis not a supported inspected object.
- parse_args(args=None)[source]¶
Parse CLI arguments into a command argument mapping.
If
Beyond sys.argvis omitted, the current process arguments are used.
- run(*commands, args=None)[source]¶
Register any command targets, parse arguments, and execute the selection.
Returns the command result unless execution exits through the configured backend. If
Beyond sys.argvis omitted, the current process arguments are used.
- parser_from_function(
- function,
- parser=None,
- taken_flags=None,
Build a backend parser for an inspected function.
Existing parser and flag reservations are used by backends that build nested or shared argparse parsers.
- parser_from_class(cls, parser=None, subparser=None)[source]¶
Build a backend parser for an inspected class.
Existing parser objects are used by backends that populate nested argparse parser trees.