colorflag

package module
v0.0.0-...-d4c845e Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 5 Imported by: 0

README

colorflag

colorflag provides colorized help for CLI applications using Go's flag standard library

Installation

go get github.com/relastle/colorflag

Example

Sample code
package main

import (
	"flag"

	"github.com/relastle/colorflag"
)

func main() {
	// main command
	flag.String("main-opt1-string", "", "Description of `quoted target`")
	flag.Int("main-opt2-integer", 0, "Description of integer option")

	// sub command 1
	sub1FlagSet := flag.NewFlagSet("sub1", flag.ExitOnError)
	sub1FlagSet.String("sub1-option-string", "default string value", "Description of string option of sub command 1")
	sub1FlagSet.Int("sub1-option-integer", 2, "Description of integer option for sub command")
	sub1FlagSet.Bool("sub1-flag-bool", false, "Description of flag for sub command")

	// sub command 2
	sub2FlagSet := flag.NewFlagSet("sub2", flag.ExitOnError)
	sub2FlagSet.String("sub2-option-string", "", "Description of string option of sub command 2")

	// Optional
	colorflag.Indent = 4               // default is 2
	colorflag.ExpandsSubCommand = true // default is true
	colorflag.TitleColor = "green"     // default is yellow
	colorflag.FlagColor = "cyan"       // default is green

	// Parse (and return selected sub command name)
	subCommand := colorflag.Parse([]*flag.FlagSet{
		sub1FlagSet,
		sub2FlagSet,
	})

	switch subCommand {
	case "sub1":
		// Handle sub1 sub-command
	case "sub2":
		// Handle sub2 sub-command
	}
}
Help output

License

The MIT License (MIT)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Indent indicates depth of one indent level
	// (the number of spaces inserted).
	Indent = 2

	// ExpandsSubCommand defines whether options and flags
	// of sub-commands are displayed in the top level help
	// message.
	ExpandsSubCommand = true

	// TitleColor specifies color of title
	// such as `subcommand`.
	TitleColor = "yellow"

	// FlagColor specifies color of flags
	FlagColor = "green"
)

Functions

func Parse

func Parse(flagSets []*flag.FlagSet) string

Parse parse subcommands and override usage

Types

type OutputFormatter

type OutputFormatter struct {
	Indent int
	// contains filtered or unexported fields
}

OutputFormatter is a formatter that constructs help messages in a structured way.

func (*OutputFormatter) AddFlag

func (o *OutputFormatter) AddFlag(flg *flag.Flag)

AddFlag adds group name which is followd by multiple options or flags

func (*OutputFormatter) AddSubCommand

func (o *OutputFormatter) AddSubCommand(subCommand string)

AddSubCommand adds subcommand

func (*OutputFormatter) CloseGroup

func (o *OutputFormatter) CloseGroup()

CloseGroup closes one group. which break line and unshift indent

func (*OutputFormatter) InitGroup

func (o *OutputFormatter) InitGroup(groupName string)

InitGroup adds group name which is followd by multiple options or flags

func (*OutputFormatter) Print

func (o *OutputFormatter) Print()

Print prints constructed help message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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