Categorygithub.com/krostar/cli
modulepackage
1.4.0
Repository: https://github.com/krostar/cli.git
Documentation: pkg.go.dev

# README

License go.mod Go version GoDoc Latest tag Go Report

Command Line Interface made simpler

The main goal of this package is to avoid being too tightly coupled with existing CLI framework.

Motivation

As of today, there are some very nice frameworks to use to handle command line interface; one of them is spf13/cobra; but it is not super obvious how to create something decoupled from cobra.

Why would someone want to do that ?

  • i was using spf13/cobra and wanted to try urfave/cli and it implied quite a lot of changes to my application
  • i tried to avoid using a framework, but I was reinventing a lot of stuff
  • frameworks expose you to a lot of features, but it comes with a lot of concepts, and two different frameworks does not necessarily come with the same concepts nor the same implementation

I did not want to recreate yet a new CLI framework to solve my problem because existing ones already are complete, but I am not using a lot of features and I wanted to keep things simple to use, simple to test, and simple to extend.

Usage

The simplest command is defined by implementing the Command interface

type myCommand struct{}
func (myCommand) Execute(ctx context.Context, args []string, dashedArgs []string) error {
    return nil
}

func main() {
    cmd := cli.NewCommand(myCommand{})
    err := spf13cobra.Execute(context.Background(), os.Args, cmd)
    cli.Exit(err)
}

This will create a CLI with one root command. This CLI is then mapped to be executed by the spf13/cobra framework.

A more useful / complex example can be found in internal/example.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Exit exits the program and uses provided error to define program success or failure.
GetInitializedFlagsFromContext returns initialized command flags.
GetMetadataFromContext retrieves any value stores to the provided key, if any.
New creates a new CLI.
NewBuiltinFlag creates a new Flag which underlying type is any builtin type declared below.
NewBuiltinPointerFlag creates a new Flag which underlying type is any pointer on builtin type declared below.
NewBuiltinSliceFlag creates a new Flag which underlying type is any slice of builtin type declared below.
NewCommandContext is called for each command to create a dedicated context.
NewContextCancelableBySignal creates a new context that cancels itself when provided signals are triggered.
NewContextWithMetadata wraps the provided context to create a global metadata store to the CLI.
NewErrorWithExitStatus wraps the provided error and tells the CLI to exit with provided code.
NewErrorWithHelp wraps provided error and tells the CLI to show usage help.
NewFlag creates a Flag based on any underlying destination type.
NewFlagValuer creates a flag valuer from a parse and toString functions.
NewStringerFlagValuer creates a flag valuer from a parse function returning something that implements stringer.
SetExitLoggerInMetadata sets the logger used by the CLI to write the exit message if any, inside the metadata.
SetInitializedFlagsInContext sets the provided initialized flags in the command context.
SetMetadataInContext associates a key to a value in the global CLI metadata store.
WithExitFunc defines a custom function to exit.
WithExitLoggerFunc defines a way to customize logger used in messages.

# Structs

CLI stores the settings associated to the CLI.
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ExitStatusError defines a new type of error that allow the customization of the CLI exit status.
Flag defines a flagValue.
FlagValuer defines how to set and get the value of a flag.
ShowHelpError defines a new type of error that defines whenever the error should display the command help before the error message.

# Type aliases

ExitOption defines the function signature to configure things upon exit.
No description provided by the author