Documentation
¶
Overview ¶
Package tap provides a lightweight command-line argument parser with support for commands, flags, colored output, and customizable help messages.
Index ¶
- Constants
- 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) Main() error
- func (p *Parser) Parse(cmdArgs []string) error
- func (p *Parser) Print(flag string, format string, args ...any)
- type ParserConfig
Constants ¶
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 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.
const Version = "1.3.0"
Variables ¶
This section is empty.
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
Scope core.ScopeType
// 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.
type ParserConfig ¶
type ParserConfig struct {
// 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_args_header_block_fmt string, help_args_data_block_fmt string, help_docs_header_block_fmt string, help_docs_data_block_fmt string, help_end_block_fmt string, ) 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_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.