ccli

package module
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 5 Imported by: 0

README

ccli

ci Go Reference

Command line parsing in Go, with coloring support

This package wraps urfave/cli/v3 and adds coloring to help output. Section headers, command names, author info, and copyright each get their own color.

screenshot

Usage

Install the package with:

go get github.com/saschagrunert/ccli/v3

Then use it like the cli package:

package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/saschagrunert/ccli/v3"
	"github.com/urfave/cli/v3"
)

func main() {
	cmd := ccli.NewCommand()
	cmd.Name = "AppName"
	cmd.Usage = "App usage..."
	cmd.Version = "0.1.0"
	cmd.Description = "Application description"
	cmd.Copyright = fmt.Sprintf("(c) %d Some Company", time.Now().Year())
	cmd.Authors = []any{"Name <e@mail.com>"}
	cmd.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:  "lang",
			Value: "english",
			Usage: "language for the greeting",
		},
	}
	cmd.Commands = []*cli.Command{
		{
			Name:  "greet",
			Usage: "send a greeting",
			Commands: []*cli.Command{
				{
					Name:  "hello",
					Usage: "say hello",
					Action: func(_ context.Context, _ *cli.Command) error {
						fmt.Println("hello!")
						return nil
					},
				},
			},
		},
	}
	ccli.Apply(cmd) // colored help for all subcommands
	cmd.Action = func(_ context.Context, _ *cli.Command) error {
		fmt.Println("boom! I say!")
		return nil
	}
	if err := cmd.Run(context.Background(), os.Args); err != nil {
		os.Exit(1)
	}
}

Custom colors

Use functional options with NewCommandWith:

import (
	"github.com/fatih/color"
	"github.com/saschagrunert/ccli/v3"
)

cmd := ccli.NewCommandWith(
	ccli.WithGreen(color.New(color.FgHiGreen).SprintFunc()),
	ccli.WithYellow(color.New(color.FgHiYellow).SprintFunc()),
)

Or pass an Options struct to NewCommandWithOptions:

cmd := ccli.NewCommandWithOptions(ccli.Options{
	Green:  color.New(color.FgHiGreen).SprintFunc(),
	Yellow: color.New(color.FgHiYellow).SprintFunc(),
})

Any color left unset falls back to its default. To turn off all coloring:

cmd := ccli.NewCommandWith(ccli.WithDisable())

Subcommand colors

Subcommands added after creating the root command don't automatically get colored help. Call Apply (or ApplyWithOptions) after setting up your command tree:

cmd := ccli.NewCommand()
cmd.Commands = []*cli.Command{
	{Name: "serve", Usage: "start the server"},
	{Name: "config", Usage: "manage config", Commands: []*cli.Command{
		{Name: "show", Usage: "show config"},
	}},
}
ccli.Apply(cmd) // recursively sets colored templates on all subcommands

Migrating from v2

The v2 branch is deprecated. To upgrade:

  1. Change imports from github.com/saschagrunert/ccli/v2 to github.com/saschagrunert/ccli/v3
  2. Rename NewApp to NewCommand, NewAppWith to NewCommandWith, and NewAppWithOptions to NewCommandWithOptions
  3. Follow the urfave/cli v3 migration guide for other cli.App to cli.Command changes

Notes

All help templates are set per-command via CustomRootCommandHelpTemplate and CustomHelpTemplate. No package-level globals are modified.

License

MIT

Documentation

Overview

Example
cmd := ccli.NewCommand()
cmd.Name = exampleAppName
cmd.Usage = exampleUsage
cmd.Version = exampleVersion

// cmd.Run(context.Background(), os.Args)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(cmd *cli.Command)

Apply recursively sets colored help templates on all subcommands of cmd using default colors. Call this after adding subcommands to ensure they get colored help output.

Example
cmd := ccli.NewCommand()
cmd.Name = exampleAppName
cmd.Usage = exampleUsage
cmd.Commands = []*cli.Command{
	{Name: "serve", Usage: "start the server"},
}

ccli.Apply(cmd)

// cmd.Run(context.Background(), os.Args)

func ApplyWith

func ApplyWith(cmd *cli.Command, opts ...Option)

ApplyWith recursively sets colored help templates on all subcommands of cmd using functional options. Call this after adding subcommands.

func ApplyWithOptions

func ApplyWithOptions(cmd *cli.Command, opts Options)

ApplyWithOptions recursively sets colored help templates on all subcommands of cmd using the provided color options. Call this after adding subcommands.

func NewCommand

func NewCommand() *cli.Command

NewCommand creates a new root command with colored help output using default colors. All help templates are set per-command via CustomRootCommandHelpTemplate and CustomHelpTemplate, avoiding global side effects.

func NewCommandWith

func NewCommandWith(opts ...Option) *cli.Command

NewCommandWith creates a new root command with colored help output, configured by functional options. Unset colors fall back to defaults. All help templates are set per-command, avoiding global side effects.

Example
cmd := ccli.NewCommandWith(
	ccli.WithGreen(color.New(color.FgHiGreen).SprintFunc()),
	ccli.WithYellow(color.New(color.FgHiYellow).SprintFunc()),
)
cmd.Name = exampleAppName
cmd.Usage = exampleUsage

// cmd.Run(context.Background(), os.Args)
Example (Disable)
cmd := ccli.NewCommandWith(ccli.WithDisable())
cmd.Name = exampleAppName
cmd.Usage = exampleUsage

// cmd.Run(context.Background(), os.Args)

func NewCommandWithOptions

func NewCommandWithOptions(opts Options) *cli.Command

NewCommandWithOptions creates a new root command with colored help output using the provided color options. Any nil color function in opts falls back to the default color. Set Disable to true to turn off all coloring. All help templates are set per-command, avoiding global side effects.

Example
cmd := ccli.NewCommandWithOptions(ccli.Options{
	Green:   color.New(color.FgHiGreen).SprintFunc(),
	Yellow:  color.New(color.FgHiYellow).SprintFunc(),
	Blue:    nil,
	Cyan:    nil,
	Red:     nil,
	Disable: false,
})
cmd.Name = exampleAppName
cmd.Usage = exampleUsage

// cmd.Run(context.Background(), os.Args)

Types

type ColorFunc

type ColorFunc func(a ...any) string

ColorFunc is a function that colorizes a string.

type Option

type Option func(*Options)

Option is a functional option for configuring a ccli application.

func WithBlue

func WithBlue(fn ColorFunc) Option

WithBlue sets the color function for author names.

func WithCyan

func WithCyan(fn ColorFunc) Option

WithCyan sets the color function for usage text.

func WithDisable

func WithDisable() Option

WithDisable turns off all coloring.

func WithGreen

func WithGreen(fn ColorFunc) Option

WithGreen sets the color function for app/command names.

func WithRed

func WithRed(fn ColorFunc) Option

WithRed sets the color function for copyright text.

func WithYellow

func WithYellow(fn ColorFunc) Option

WithYellow sets the color function for section headers.

type Options

type Options struct {
	// Blue is used for author names. Defaults to color.FgBlue.
	Blue ColorFunc
	// Cyan is used for usage text. Defaults to color.FgCyan.
	Cyan ColorFunc
	// Green is used for app/command names. Defaults to color.FgGreen.
	Green ColorFunc
	// Red is used for copyright text. Defaults to color.FgRed.
	Red ColorFunc
	// Yellow is used for section headers. Defaults to color.FgYellow.
	Yellow ColorFunc
	// Disable turns off all coloring. When true, all color functions
	// are replaced with plain fmt.Sprint.
	Disable bool
}

Options allows customizing the color scheme used in help output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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