{"modulePath":"github.com/segmentio/cli","version":"v0.8.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package cli\n\nPackage cli provides high-level tools for building command-line interfaces.\n\n## Variables\n\n```go\nvar Err io.Writer = os.Stderr\n```\nErr is used by the Exec and Call functions to print out errors returned by the commands they call out to.\n\n## Functions\n\n```go\nfunc Call(cmd Function, args ...string) int\n```\nCall calls cmd with args and environment variables prefixed with the uppercased program name.\n\nThis function is often used to test commands in example programs with constructs like:\n\n\tvar command = cli.Command(func(config config) {\n\t\t...\n\t})\n\nThen in the test file:\n\n\tfunc Example_command_with_option() {\n\t\tcli.Call(command, \"--option\", \"value\")\n\t\t// Output:\n\t\t// ...\n\t}\n\n```go\nfunc CallContext(ctx context.Context, cmd Function, args ...string) int\n```\nCallContext calls Call but with a specified context.Context.\n\n```go\nfunc Exec(cmd Function)\n```\nExec delegate the program execution to cmd, then exits with the code returned by the function call.\n\nA typical use case is for Exec to be the last statement of the main function of a program:\n\n\tfunc main() {\n\t\tcli.Exec(cli.Command(func(config config) {\n\t\t\t...\n\t\t})\n\t}\n\nThe Exec function never returns.\n\n```go\nfunc ExecContext(ctx context.Context, cmd Function)\n```\nExecContext calls Exec but with a specified context.Context.\n\n## Types\n\n```go\ntype CommandFunc struct {\n\t// A short help message describing what the command does.\n\tHelp string\n\n\t// A full description of the command.\n\tDesc string\n\n\t// The function that the command calls out to when invoked.\n\t//\n\t// See Command for details about the accepted signatures.\n\tFunc interface{}\n\n\t// An optional usage string for this function. If set, then this replaces\n\t// the default one that shows the types (but not names) of arguments.\n\tUsage string\n\n\t// Set of options to not set from the environment\n\t// this is a more user-friendly-syntax than IgnoreEnvOptionMap\n\t// However, this is strictly for user input and should not be used in the cli code\n\t// Please use IgnoreEnvOptionMap internally\n\tIgnoreEnvOptions []string\n\n\t// Set of options to not set from the environment\n\t// This is to convert IgnoreEnvOptions field to a map for efficient lookups\n\tIgnoreEnvOptionsMap map[string]struct{}\n\t// contains filtered or unexported fields\n}\n```\nCommandFunc is an implementation of the Function interface which calls out to a nested function when invoked.\n\n```go\nfunc (cmd *CommandFunc) Call(ctx context.Context, args, env []string) (int, error)\n```\nCall satisfies the Function interface.\n\nSee Command for the full documentation of how the Call method behaves.\n\n```go\nfunc (cmd *CommandFunc) Format(w fmt.State, v rune)\n```\nFormat satisfies the fmt.Formatter interface. It recognizes the following verbs:\n\n\t%s\toutputs the usage information of the command\n\t%v\toutputs the full description of the command\n\t%x\toutputs the help message of the command\n\n```go\ntype CommandSet map[string]Function\n```\nA CommandSet is used to construct a routing mechanism for named commands.\n\nThis model is often used in CLI tools to support a list of verbs, each routing the caller to a sub-functionality of the main command.\n\nCommandSet satisfies the Function interface as well, making it possible to compose sub-commands recursively, for example:\n\n\tcmd := cli.CommandSet{\n\t\t\"top\": cli.CommandSet{\n\t\t\t\"sub-1\": cli.Command(func() {\n\t\t\t\t...\n\t\t\t}),\n\t\t\t\"sub-2\": cli.Command(func() {\n\t\t\t\t...\n\t\t\t}),\n\t\t},\n\t}\n\nThe sub-commands can be called with one of these invocations:\n\n\t$ program top sub-1\n\n\t$ program top sub-2\n\n```go\nfunc (cmds CommandSet) Call(ctx context.Context, args, env []string) (int, error)\n```\nCall dispatches the given arguments and environment variables to the sub-command named in the first non-option value in args. Finding the command separator \"--\" before a sub-command name results in an error.\n\nThe method returns a \\*Help (as an error) is the first argument is -h or --help, and a usage error if the first argument did not match any sub-command.\n\nCall satisfies the Function interface.\n\n```go\nfunc (cmds CommandSet) Format(w fmt.State, v rune)\n```\nFormat writes a human-readable representation of cmds to w, using v as the formatting verb to determine which property of the command set should be written.\n\nThe method supports the following formatting verbs:\n\n\t%s\toutputs the usage information of the command set\n\t%v\toutputs the full description of the command set\n\nFormat satisfies the fmt.Formatter interface.\n\n```go\ntype Flusher interface {\n\tFlush()\n}\n```\nFlusher is an interface implemented by types that buffer content.\n\n```go\ntype Function interface {\n\tCall(ctx context.Context, args, env []string) (int, error)\n}\n```\nThe Function interface is implemented by commands that may be invoked with argument and environment variable lists.\n\nFunctions returns a status code intended to be the exit code of the program that called them as well as a non-nil error if the function call failed.\n\n```go\nfunc Command(fn interface{}) Function\n```\nCommand constructs a Function which delegates to the Go function passed as argument.\n\nThe argument is an interface{} because functions with multiple types are accepted.\n\nThe function may receive no arguments, indicating that the program delegating to the command is expected to be invoked with no arguments, for example:\n\n\tcmd := cli.Command(func() {\n\t\t...\n\t})\n\nIf the function accepts arguments, the first argument (except for an optional initial \\`context.Context\\`) must always be a struct type, which describes the set of options that are accepted by the command:\n\n\t// Struct tags are used to declare the flags accepted by the command.\n\ttype config struct {\n\t\tPath    string `flag:\"--path\"       help:\"Path to a text file\" default:\"file.txt\"`\n\t\tVerbose bool   `flag:\"-v,--verbose\" help:\"Enable verbose mode\"`\n\t}\n\n\tcmd := cli.Command(func(config config) {\n\t\t...\n\t})\n\nFive keys are recognized in the struct tags: \"flag\", \"env\", \"help\", \"default\", and \"hidden\".\n\nThe \"flag\" struct tag is a comma-separated list of command line flags that map to the field. This tag is required.\n\nThe \"env\" struct tag optionally specifies the name of an environment variable whose value may provide a field value. When the tag is not specified, then environment variables corresponding to long command line flags may provide field values. A tag value of \"-\" disables this default behavior.\n\nThe \"help\" struct tag is a human-readable message describing what the field is used for.\n\nThe \"default\" struct tag provides the default value of the field when the argument was missing from the call to the command. Any flag which has no default value and isn't a boolean or a slice type must be passed when calling the command, otherwise a usage error is returned. The special default value \"-\" can be used to indicate that the option is not required and should assume its zero-value when omitted.\n\nThe \"hidden\" struct flag is a Boolean indicating if the field should be excluded from help text, essentially making it undocumented.\n\nIf the struct contains a field named \\`\\_\\`, the command will look for a \"help\" struct tag to define its own help message. Note that the type of the field is irrelevant, but it is common practice to use an empty struct.\n\nThe command always injects and handles the -h and --help flags, which can be used to request that call to the command return a help error to describe the configuration options of the command.\n\nEvery flag starting with a \"--\" may also be configured via an environment variable. The environment variable is matched by converting the flag name to a snakecase and uppercase format. Flags that should not be matched to environment variables must specify a struct tag env:\"-\" to disable the feature.\n\nEach extra argument to the function is interpreted as a positional argument and decoded as such, for example:\n\n\t// This command expects two integers as positional arguments.\n\tcmd := cli.Command(func(config config, x, y int) {\n\t\t...\n\t})\n\nThe last positional argument may be a slice, which consumes as many values as remained on the command invocation.\n\nAn extra variadic string parameter may be accepted by the function, which receives any extra arguments found after a \"--\" separator. This mechanism is often used by programs that spawn other programs to define the limits between the arguments of the first program, and the second command.\n\nIf the command is called with an invalid set of arguments, it returns a non-zero code and a usage error which describes the issue.\n\n```go\nfunc NamedCommand(name string, cmd Function) Function\n```\nNamedCommand constructs a command which carries the name passed as argument and delegate execution to cmd.\n\n```go\ntype Help struct {\n\tCmd Function\n}\n```\nHelp values are returned by commands to indicate to the caller that it was called with a configuration that requested a help message rather than executing the command. This type satisfies the error interface.\n\n```go\nfunc (h *Help) Error() string\n```\nError satisfies the error interface.\n\n```go\nfunc (h *Help) Format(w fmt.State, v rune)\n```\nFormat satisfies the fmt.Formatter interface, print the help message for the command carried by h.\n\n```go\ntype PrintFlusher interface {\n\tPrinter\n\tFlusher\n}\n```\nPrintFlusher is an interface implemented by printers that may buffer content until they are flushed.\n\n```go\nfunc Format(format string, output io.Writer) (PrintFlusher, error)\n```\nFormat returns a Printer which formats printed values.\n\nTypical usage looks like this:\n\n\tp, err := cli.Format(config.Format, os.Stdout)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer p.Flush()\n\t...\n\tp.Print(v1)\n\tp.Print(v2)\n\tp.Print(v3)\n\nThe package supports three formats: text, json, and yaml. All formats einterpret the \\`json\\` struct tag to configure the names of the fields and the behavior of the formatting operation.\n\nThe text format also interprets \\`fmt\\` tags as carrying the formatting string passed in calls to functions of the \\`fmt\\` package.\n\nIf the format name is not supported, the function returns a usage error.\n\n```go\nfunc FormatList(format string, output io.Writer) (PrintFlusher, error)\n```\nFormatList returns a Printer which formats lists of printed values.\n\nTypical usage looks like this:\n\n\tp, err := cli.FormatList(config.Format, os.Stdout)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer p.Flush()\n\t...\n\tp.Print(v1)\n\tp.Print(v2)\n\tp.Print(v3)\n\nThe package supports three formats: text, json, and yaml. All formats einterpret the \\`json\\` struct tag to configure the names of the fields and the behavior of the formatting operation.\n\nThe text format also interprets \\`fmt\\` tags as carrying the formatting string passed in calls to functions of the \\`fmt\\` package.\n\nIf the format name is not supported, the function returns a usage error.\n\n```go\ntype Printer interface {\n\tPrint(interface{})\n}\n```\nPrinter is an interface implemented for high-level printing formats.\n\n```go\ntype Usage struct {\n\tCmd Function\n\tErr error\n}\n```\nUsage values are returned by commands to indicate that the combination of arguments and environment variables they were called with was invalid. This type satisfies the error interface.\n\n```go\nfunc (u *Usage) Error() string\n```\nError satisfies the error interface.\n\n```go\nfunc (u *Usage) Format(w fmt.State, v rune)\n```\nFormat satisfies the fmt.Formatter interface, print the usage message for the command carried by u.\n\n```go\nfunc (u *Usage) Unwrap() error\n```\nUnwrap satisfies the errors wrapper interface.\n\n","path":"github.com/segmentio/cli","name":"cli","synopsis":"Package cli provides high-level tools for building command-line interfaces.","isRedistributable":true}
