Features
Clak extends Python's argparse with a class-based API. If you know argparse,
you already know most of Clak. How Clak relates to argparse, Click, Typer, and
Cliff: Comparison.
Argparse-friendly core
- Reuse argparse concepts; organize CLI code as Python classes.
- Canonical API:
argparse.ArgumentParser()→class MyApp(Parser):.add_argument(...)→dest = Argument(...).add_subparsers(...)→subcmd = Command(...)
- Aliases (supported, not preferred in new code):
SubParser/SubCommand/CmdforCommand;ArgumentParserforParser. - Optional helpers:
Optfor flags (-/--) andArgfor positionals.Argumentstill accepts both;Arg/Optare never required. Mixing names (Arg("--flag"),Opt("NAME")) raisesValueError. See Getting started.
Class-based structure
- Declarative CLI via classes and inheritance.
- Share options and behaviour up the command tree.
- Keep argparse internals out of your application code.
Nested (git-like) commands
- Each subcommand is a
Parser, bound withCommand. - Root
--helplists nested subcommands (Meta.help_subcommands = "all", parent path hidden). Set"top"for immediate children only. Each node has its own help text.
Optional components
Mix in only what you need:
Help
- Command-tree overview,
--help/-h, customizable usage / description / epilog. Default listing is nested children (Meta.help_subcommands = "all", parent path hidden unlessMeta.help_hide_parent = False;"top"for immediate children only). - Colored
--helpby default when Rich is installed and stdout is a TTY (mrjk.clak[markdown]). Markup inhelp_description/help_epilogwhen color is on. Opt out:Meta.help_formatter = RecursiveHelpFormatter. Guide: Colored help. - Named help groups:
Argument(..., option_group="Title")orargument_group="Title"places flags under a titled section in--help(same title reuses one group). Exclusive groups:Argument(..., exclusive_group="key")for XOR. Subcommand sections:Meta.command_groupsplusCommand(..., command_group="key")(formatter metadata, not a secondadd_subparsers). See Nested commands.
Views
- Turn return values into tables or pretty-prints.
- Mixins:
ShowViewMixin,ListViewMixin,PprintViewMixin,DataViewMixin,RawViewMixin,MarkdownViewMixin,RstViewMixin,CompositeViewMixin. - Cliff-style output:
--format view|yaml|json|csv,--sort-columns,--sort-mode,--width content|fit|terminal. Text uses--line-length(N/terminal/nowrap, default 120). Markdown/rst also use--format view|raw. Data uses--format json|yaml(auto yaml/json),--compact,--color,--anchors. Composite uses--format-scope first|allfor multi-section machine export. - Markdown/data color:
mrjk.clak[markdown]; YAML:mrjk.clak[config]; RST:mrjk.clak[rst]. - Guide: Views.
Error handling
dispatch()try/except +clean_terminate()handler chain.Meta.known_exceptionsfor app errors with customrc.Meta.exception_handlersfor third-party libraries.- Guide: Error handling.
Logging
LoggingOptMixin: stderr logging,self.logger, cumulative-vtiers.- Formatters, optional colors (
mrjk.clak[colors]),--trace. - Custom levels:
spam,verbose,success,notice. - Guide: Logging.
Config
XDGConfigMixin:--conf-fileand XDG data/cache/log paths fromMeta.app_name.- Loads JSON always; YAML via
mrjk.clak[config]. - Guide: Config.
Completion
- Emit shell completion scripts with
CompCmdRender(argcomplete). - Guide: Completion.
Runtime / facts
ctx.runtime: TTY, shell parent, color/size, pager (core CLI session).ctx.facts: optional lazy host/user/distro helpers.- Guide: Runtime and facts.
Build your own
- Package reusable mixins (options + hooks) the same way Clak’s components do.
Planned
See the roadmap for intermixed args, env-var mapping, and runtime autocomplete.