Documentation
¶
Overview ¶
Package tap provides a lightweight command-line argument parser with support for commands, flags, colored output, and customizable help messages.
Index ¶
- Constants
- Variables
- type HandlerFuncType
- type Parser
- func (p *Parser) AddAlias(aliasName, cmdName string) error
- func (p *Parser) AddCommand(name string, handler HandlerFuncType, docs string, required_args []string, ...)
- func (p *Parser) AddSubcommand(name string, parser *Parser)
- func (p *Parser) GetSubcommand(name string) (pr *Parser)
- func (p *Parser) Main() error
- func (p *Parser) Parse(cmdArgs []string) (err error)
- func (p *Parser) Print(flag string, format string, args ...any)
- func (p *Parser) Subcommand(name string) (err error, pr *Parser)
- type ParserConfig
Constants ¶
const DEFAULT_CMD = "#[DEFAULT CMD]#"
DEFAULT_CMD is a reserved command name. It is used when no command is specified as the first argument, allowing your CLI to be invoked in two ways: cli args... (implicit default) or cli cmd args... (explicit command). Note: documentation of default command is displayed after about message.
const DONT_SHOW = "#[DON'T SHOW]#"
DONT_SHOW is a special docstring value that hides the command from the help output. The command remains functional but will not appear in the auto-generated help.
const Version = "1.7.2"
Variables ¶
var HELP_DOCS = "Generate and print help message"
HELP_DOCS is the docstring used by the built‑in help command. Commands sharing this docstring will be grouped together as aliases.
Functions ¶
This section is empty.
Types ¶
type HandlerFuncType ¶
HandlerFuncType defines the signature for command handler functions. It receives the parser instance and the slice of command arguments. Returns an error if the command execution fails.
type Parser ¶
type Parser struct {
Flags map[string]string
Super *Parser
Scope core.ScopeType
RawArgs []string
TermWidth int
// contains filtered or unexported fields
}
Tap - Terminal Argument Parsing ¶
This parser is the main object in tap.
Main methods:
- AddCommand(name string, handler HandlerFuncType)
- Main() - start parser
func NewParser ¶
func NewParser(cli_name string, about string, help_commands []string, config ParserConfig) *Parser
NewParser creates a new Parser instance. Parameters:
- cli_name: name of the CLI application (used in help).
- about: informational text printed when no command is given.
- help_commands: slice of command names that trigger the help handler. If nil, defaults to []string{"help", "h"}.
- config: ParserConfig controlling help message formatting.
Returns a pointer to the initialized Parser.
func (*Parser) AddCommand ¶
func (p *Parser) AddCommand( name string, handler HandlerFuncType, docs string, required_args []string, optional_args []string, unlimited_max_args bool, )
AddCommand registers a new command with the parser. Parameters:
- name: command name (string used in CLI).
- handler: function called when the command is invoked.
- docs: description shown in help; use DONT_SHOW to hide the command.
- required_args: slice of required argument names.
- optional_args: slice of optional argument names.
- unlimited_max_args: if true, command accepts any number of trailing arguments.
func (*Parser) AddSubcommand ¶
AddSubCommand registers a nested sub-parser for a given subcommand name.
This method stores the provided Parser instance under the specified name, automatically integrate it into the main command dispatch flow.
Parameters:
- name: the subcommand name.
- parser: the Parser instance that will handle the subcommand.
func (*Parser) GetSubcommand ¶
SubCommand retrieves a previously registered sub-parser by name.
It returns the Parser pointer if the name exists; otherwise, it panic.
func (*Parser) Main ¶
Main is the primary entry point of the parser. It parses os.Args[1:], extracts flags, finds the command, and executes the corresponding handler. Returns an error if no command is provided, the command is unknown, or the handler fails.
type ParserConfig ¶
type ParserConfig struct {
BuiltinVerboseDebug bool
BuiltinNoColor bool
// contains filtered or unexported fields
}
ParserConfig defines the formatting templates for the auto-generated help message. Each field is a format string that may contain "%s" placeholders for dynamic content. If an empty string is passed to NewParserConfig, the corresponding default format will be used.
func NewParserConfig ¶
func NewParserConfig( help_command_block_fmt string, help_subcommand_block_fmt string, help_args_header_block_fmt string, help_args_data_block_fmt string, help_args_data_block_padding int, help_docs_header_block_fmt string, help_docs_data_block_fmt string, help_end_block_fmt string, BuiltinVerboseDebug bool, BuiltinNoColor bool, ) ParserConfig
NewParserConfig creates a new ParserConfig with the given format strings. Any empty string parameter will be replaced with a sensible default. Parameters:
- help_command_block_fmt: format for the command name block (e.g., "╭─────── Command [%s]").
- help_subcommand_block_fmt: format for the subcommand name block (e.g., "╭─────── Subcommand [%s]").
- help_args_header_block_fmt: format for the arguments section header.
- help_args_data_block_fmt: format for each argument line.
- help_docs_header_block_fmt: format for the description section header.
- help_docs_data_block_fmt: format for each line of the description.
- help_end_block_fmt: format for the closing block.
Returns a populated ParserConfig.