urfavehelp

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 8 Imported by: 0

README

urfave-help

A drop-in, custom help formatter for urfave/cli/v3.

Partially inspired by Cobra, it gives your CLI clean, readable terminal help:

  • Aligned & wrapped: Single-column gutters and terminal-aware wrapping with hanging indents.
  • Positional arguments: First-class argument documentation via SetArgs.
  • Clean flags: Informative placeholders (<path>) and defaults without stutter or path leaks.
  • Preserves examples: Leaves authored multi-line descriptions and code blocks untouched.

Preview

Running the example below outputs:

Manage background tasks and worker deployments

Usage:
  taskctl [command] <environment> [replicas] [flags]

Arguments:
  <environment>           Target environment (staging, production)
  [replicas]              Worker instances (default: 2)

Commands:
  run     Start worker process and execute queued jobs
  status  Display active task status and worker health

Flags:
  --config <path>         Configuration file path
  --timeout <duration>    Job execution duration (default: 30s)
  --format <table|plain>  Output format: table|plain (default: table)
  --filter <string>       Filter jobs by status, tag, priority, queue name, or
                          worker assignment across the cluster
  --dry-run, -n           Simulate task execution without applying changes
  --verbose, -v           Enable debug logging
  --help, -h              Show help
  --version               Print the version

Use "taskctl [command] --help" for more information about a command.

Installation

go get github.com/zigai/urfave-help

Example

package main

import (
	"context"
	"os"
	"time"

	"github.com/urfave/cli/v3"

	urfavehelp "github.com/zigai/urfave-help"
)

func main() {
	cmd := &cli.Command{
		Name:      "taskctl",
		Usage:     "Manage background tasks and worker deployments",
		ArgsUsage: "<environment> [replicas]",
		Version:   "1.0.0",
		Commands: []*cli.Command{
			{
				Name:  "run",
				Usage: "Start worker process and execute queued jobs",
			},
			{
				Name:  "status",
				Usage: "Display active task status and worker health",
			},
		},
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "config",
				Usage: "Configuration file `path`",
			},
			&cli.DurationFlag{
				Name:  "timeout",
				Value: 30 * time.Second,
				Usage: "Job execution `duration`",
			},
			&cli.StringFlag{
				Name:  "format",
				Value: "table",
				Usage: "Output format: `table|plain`",
			},
			&cli.StringFlag{
				Name:  "filter",
				Usage: "Filter jobs by status, tag, priority, queue name, or worker assignment across the cluster",
			},
			&cli.BoolFlag{
				Name:    "dry-run",
				Aliases: []string{"n"},
				Usage:   "Simulate task execution without applying changes",
			},
			&cli.BoolFlag{
				Name:    "verbose",
				Aliases: []string{"v"},
				Usage:   "Enable debug logging",
			},
		},
	}

	urfavehelp.SetArgs(cmd,
		urfavehelp.Arg{Name: "<environment>", Desc: "Target environment (staging, production)"},
		urfavehelp.Arg{Name: "[replicas]", Desc: "Worker instances (default: 2)"},
	)

	urfavehelp.Install(
		urfavehelp.WithWidths(60, 100, 80),
		urfavehelp.WithGutterGap(2),
	)

	_ = cmd.Run(context.Background(), os.Args)
}

License

MIT

Documentation

Overview

Package urfavehelp provides a clean, terminal-aware help formatter for github.com/urfave/cli/v3.

Features:

  • Aligned columns: arguments, commands, and flags share a single vertical gutter.
  • Smart word wrapping: reflows descriptions at terminal width with hanging indents.
  • Clean placeholders: enforces <value> on value flags without repeating flag names.
  • Positional arguments: document arguments with SetArgs or native cli.Command.Arguments.
  • Sanitized defaults: hides dynamic machine-local paths like os.Executable.
  • Subcommand aliases: displays command aliases (e.g. list, ls) with aligned spacing.

Quick Start

Enable urfavehelp with a single line:

package main

import (
    "context"
    "os"

    "github.com/urfave/cli/v3"
    urfavehelp "github.com/zigai/urfave-help"
)

func main() {
    cmd := &cli.Command{
        Name:  "mytool",
        Usage: "A production command-line interface",
    }

    urfavehelp.Install()
    _ = cmd.Run(context.Background(), os.Args)
}

Index

Constants

View Source
const DefaultArgsMetadataKey = "urfave_help_arguments"

DefaultArgsMetadataKey stores positional argument documentation in Command.Metadata.

View Source
const LegacyArgsMetadataKey = "aht_help_arguments"

LegacyArgsMetadataKey is a fallback key for backward compatibility.

Variables

View Source
var HelpPrinter = NewPrinter()

HelpPrinter is the default HelpPrinterFunc.

Functions

func Install

func Install(opts ...Option)

Install registers the custom help formatter with urfave/cli.

func NewPrinter

func NewPrinter(opts ...Option) cli.HelpPrinterFunc

NewPrinter creates a cli.HelpPrinterFunc configured with the given options.

func PrintHelp

func PrintHelp(w io.Writer, cmd *cli.Command, opts Options)

PrintHelp formats and writes command help to w.

func SetArgs

func SetArgs(cmd *cli.Command, args ...Arg)

SetArgs attaches positional argument documentation to a command.

func TerminalWidth

func TerminalWidth(w any) int

TerminalWidth returns the terminal column count, or 0 if w is not an os.File terminal.

func WithArgs

func WithArgs(args ...Arg) func(*cli.Command)

WithArgs returns a mutator that attaches positional argument documentation.

Types

type Arg

type Arg struct {
	Name string
	Desc string
}

Arg documents a positional command argument.

type Option

type Option func(*Options)

Option configures help printer options.

func WithGutterGap

func WithGutterGap(gap int) Option

WithGutterGap sets the spacing between labels and descriptions.

func WithMetavarResolver

func WithMetavarResolver(fn func(cli.Flag) string) Option

WithMetavarResolver sets a custom resolver for flag placeholder names.

func WithMinDescWidth

func WithMinDescWidth(width int) Option

WithMinDescWidth sets the minimum readable width allocated to wrapped descriptions.

func WithPathPrefixFilter

func WithPathPrefixFilter(prefix string) Option

WithPathPrefixFilter sets the prefix used to suppress dynamic path leaks in defaults (e.g. "/").

func WithWidths

func WithWidths(minWidth, maxWidth, defaultWidth int) Option

WithWidths sets the minimum, maximum, and non-TTY fallback widths for word wrapping.

type Options

type Options struct {
	// MinWidth is the lower bound for terminal word wrapping (default: 60).
	MinWidth int

	// MaxWidth is the upper bound for terminal word wrapping (default: 0, uncapped).
	MaxWidth int

	// DefaultWidth is the fallback width when stdout is not a terminal (default: 100).
	DefaultWidth int

	// IndentSpaces is the section indentation (default: 2).
	IndentSpaces int

	// GutterGap is the spacing between labels and descriptions (default: 2).
	GutterGap int

	// MinDescWidth is the minimum width allocated for wrapped descriptions (default: 20).
	MinDescWidth int

	// PathPrefixFilter suppresses flag defaults starting with this prefix (default: "/").
	PathPrefixFilter string

	// CustomMetavar overrides flag placeholder resolution.
	CustomMetavar func(cli.Flag) string
}

Options configures help layout, word wrapping, and formatting.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the default formatting configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL