Documentation ¶
Overview ¶
Package cli provides high-level tools for building command-line interfaces.
Index ¶
- Variables
- func Call(cmd Function, args ...string) int
- func CallContext(ctx context.Context, cmd Function, args ...string) int
- func Exec(cmd Function)
- func ExecContext(ctx context.Context, cmd Function)
- type CommandFunc
- type CommandSet
- type Flusher
- type Function
- type Help
- type PrintFlusher
- type Printer
- type Usage
Examples ¶
- Command (BinaryUnmarshaler)
- Command (Bool)
- Command (Context)
- Command (Context_args)
- Command (Context_config)
- Command (Default)
- Command (Duration)
- Command (Embedded_struct)
- Command (Environment)
- Command (Float)
- Command (Help)
- Command (HelpContext)
- Command (Int)
- Command (Positional_arguments)
- Command (Positional_arguments_slice)
- Command (Required)
- Command (Slice)
- Command (SpacesInFlag)
- Command (String)
- Command (TextUnmarshaler)
- Command (Time)
- Command (Uint)
- Command (Usage)
- Command (With_sub_command)
- CommandSet
- CommandSet (Help)
- CommandSet (Help2)
- CommandSet (Option_after_command)
- CommandSet (Option_before_command)
- CommandSet (Usage_text)
- Format (Json)
- Format (Text_map)
- Format (Text_string)
- Format (Text_struct)
- Format (Yaml)
- FormatList (Json)
- FormatList (Yaml)
Constants ¶
This section is empty.
Variables ¶
var Err io.Writer = os.Stderr
Err is used by the Exec and Call functions to print out errors returned by the commands they call out to.
Functions ¶
func Call ¶
Call calls cmd with args and environment variables prefixed with the uppercased program name.
This function is often used to test commands in example programs with constructs like:
var command = cli.Command(func(config config) { ... })
Then in the test file:
func Example_command_with_option() { cli.Call(command, "--option", "value") // Output: // ... }
func CallContext ¶ added in v0.2.0
CallContext calls Call but with a specified context.Context.
func Exec ¶
func Exec(cmd Function)
Exec delegate the program execution to cmd, then exits with the code returned by the function call.
A typical use case is for Exec to be the last statement of the main function of a program:
func main() { cli.Exec(cli.Command(func(config config) { ... }) }
The Exec function never returns.
func ExecContext ¶ added in v0.2.0
ExecContext calls Exec but with a specified context.Context.
Types ¶
type CommandFunc ¶
type CommandFunc struct { // A short help message describing what the command does. Help string // A full description of the command. Desc string // The function that the command calls out to when invoked. // // See Command for details about the accepted signatures. Func interface{} // An optional usage string for this function. If set, then this replaces // the default one that shows the types (but not names) of arguments. Usage string // Set of options to not set from the environment // this is a more user-friendly-syntax than IgnoreEnvOptionMap // However, this is strictly for user input and should not be used in the cli code // Please use IgnoreEnvOptionMap internally IgnoreEnvOptions []string // Set of options to not set from the environment // This is to convert IgnoreEnvOptions field to a map for efficient lookups IgnoreEnvOptionsMap map[string]struct{} // contains filtered or unexported fields }
CommandFunc is an implementation of the Function interface which calls out to a nested function when invoked.
type CommandSet ¶
A CommandSet is used to construct a routing mechanism for named commands.
This 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.
CommandSet satisfies the Function interface as well, making it possible to compose sub-commands recursively, for example:
cmd := cli.CommandSet{ "top": cli.CommandSet{ "sub-1": cli.Command(func() { ... }), "sub-2": cli.Command(func() { ... }), }, }
The sub-commands can be called with one of these invocations:
$ program top sub-1 $ program top sub-2
Example ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { help := cli.Command(func() { fmt.Println("help") }) this := cli.Command(func() { fmt.Println("this") }) that := cli.Command(func() { fmt.Println("that") }) cmd := cli.CommandSet{ "help": help, "do": cli.CommandSet{ "this": this, "that": that, }, } cli.Call(cmd, "help") cli.Call(cmd, "do", "this") cli.Call(cmd, "do", "that") }
Output: help this that
Example (Help) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { type thisConfig struct { _ struct{} `help:"Call this command"` Path string `flag:"-p,--path" help:"Path to some file" default:"file" env:"-"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } type thatConfig struct { _ struct{} `help:"Call that command"` Count int `flag:"-n" help:"Number of things" default:"1"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } cmd := cli.CommandSet{ "do": cli.CommandSet{ "this": cli.Command(func(config thisConfig) { // ... }), "that": cli.Command(func(config thatConfig) { // ... }), }, } cli.Err = os.Stdout cli.Call(cmd, "do", "--help") }
Output: Usage: do [command] [-h] [--help] ... Commands: that Call that command this Call this command Options: -h, --help Show this help message
Example (Help2) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { type thisConfig struct { _ struct{} `help:"Call this command"` Path string `flag:"-p,--path" help:"Path to some file" default:"file" env:"-"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } type thatConfig struct { _ struct{} `help:"Call that command"` Count int `flag:"-n" help:"Number of things" default:"1"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } cmd := cli.CommandSet{ "do": cli.CommandSet{ "this": cli.Command(func(config thisConfig) { // ... }), "that": cli.Command(func(config thatConfig) { // ... }), }, } cli.Err = os.Stdout cli.Call(cmd, "do", "this", "-h") }
Output: Usage: do this [options] Options: -d, --debug Enable debug mode -h, --help Show this help message -p, --path string Path to some file (default: file)
Example (Option_after_command) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { String string `flag:"-f,--flag" default:"-"` } sub := cli.Command(func(config config) { fmt.Println(config.String) }) cmd := cli.CommandSet{ "sub": sub, } cli.Call(cmd, "sub", "-f=hello") }
Output: hello
Example (Option_before_command) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { String string `flag:"-f,--flag" default:"-"` } sub := cli.Command(func(config config) { fmt.Println(config.String) }) cmd := cli.CommandSet{ "sub": sub, } cli.Call(cmd, "-f=hello", "sub") }
Output: hello
Example (Usage_text) ¶
package main import ( "fmt" "os" "github.com/segmentio/cli" ) func main() { help := cli.Command(func() { fmt.Println("help") }) doc := cli.Command(func() { fmt.Println("doc") }) cover := cli.Command(func() { fmt.Println("cover") }) cmd := cli.CommandSet{ "help": help, "tool": cli.CommandSet{ "_": &cli.CommandFunc{ Help: "run specified go tool", }, "cover": cover, "doc": doc, }, } cli.Err = os.Stdout cli.Call(cmd, "--help") }
Output: Usage: [command] [-h] [--help] ... Commands: help tool run specified go tool Options: -h, --help Show this help message
func (CommandSet) Call ¶
Call 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.
The 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.
Call satisfies the Function interface.
func (CommandSet) Format ¶
func (cmds CommandSet) Format(w fmt.State, v rune)
Format 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.
The method supports the following formatting verbs:
%s outputs the usage information of the command set %v outputs the full description of the command set
Format satisfies the fmt.Formatter interface.
type Flusher ¶
type Flusher interface {
Flush()
}
Flusher is an interface implemented by types that buffer content.
type Function ¶
The Function interface is implemented by commands that may be invoked with argument and environment variable lists.
Functions 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.
func Command ¶
func Command(fn interface{}) Function
Command constructs a Function which delegates to the Go function passed as argument.
The argument is an interface{} because functions with multiple types are accepted.
The function may receive no arguments, indicating that the program delegating to the command is expected to be invoked with no arguments, for example:
cmd := cli.Command(func() { ... })
If 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:
// Struct tags are used to declare the flags accepted by the command. type config struct { Path string `flag:"--path" help:"Path to a text file" default:"file.txt"` Verbose bool `flag:"-v,--verbose" help:"Enable verbose mode"` } cmd := cli.Command(func(config config) { ... })
Five keys are recognized in the struct tags: "flag", "env", "help", "default", and "hidden".
The "flag" struct tag is a comma-separated list of command line flags that map to the field. This tag is required.
The "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.
The "help" struct tag is a human-readable message describing what the field is used for.
The "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.
The "hidden" struct flag is a Boolean indicating if the field should be excluded from help text, essentially making it undocumented.
If 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.
The 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.
Every 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.
Each extra argument to the function is interpreted as a positional argument and decoded as such, for example:
// This command expects two integers as positional arguments. cmd := cli.Command(func(config config, x, y int) { ... })
The last positional argument may be a slice, which consumes as many values as remained on the command invocation.
An 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.
If the command is called with an invalid set of arguments, it returns a non-zero code and a usage error which describes the issue.
Example (BinaryUnmarshaler) ¶
package main import ( "fmt" "net/url" "github.com/segmentio/cli" ) func main() { type config struct { URL url.URL `flag:"--url" default:"http://localhost/"` } cmd := cli.Command(func(config config) { fmt.Println(config.URL.String()) }) cli.Call(cmd) cli.Call(cmd, "--url", "http://www.segment.com/") }
Output: http://localhost/ http://www.segment.com/
Example (Bool) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { Bool bool `flag:"-f,--flag"` } cmd := cli.Command(func(config config) { fmt.Println(config.Bool) }) cli.Call(cmd) cli.Call(cmd, "-f") cli.Call(cmd, "--flag") cli.Call(cmd, "-f=false") cli.Call(cmd, "--flag=false") cli.Call(cmd, "-f=true") cli.Call(cmd, "--flag=true") }
Output: false true true false false true true
Example (Context) ¶
package main import ( "context" "fmt" "github.com/segmentio/cli" ) func main() { ctx := context.Background() cmd := cli.Command(func(ctx context.Context) { if ctx == context.TODO() { fmt.Println("context.TODO()") } else { fmt.Println("context.Background()") } }) cli.Call(cmd) cli.CallContext(ctx, cmd) }
Output: context.TODO() context.Background()
Example (Context_args) ¶
package main import ( "context" "fmt" "github.com/segmentio/cli" ) func main() { ctx := context.TODO() type config struct{} cmd := cli.Command(func(ctx context.Context, config config, args []string) { fmt.Println(args) }) cli.CallContext(ctx, cmd, "hello", "world") }
Output: [hello world]
Example (Context_config) ¶
package main import ( "context" "fmt" "github.com/segmentio/cli" ) func main() { ctx := context.TODO() type config struct{} cmd := cli.Command(func(ctx context.Context, config config) { fmt.Println("hello world") }) cli.CallContext(ctx, cmd) }
Output: hello world
Example (Default) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { Path string `flag:"-p,--path" default:"file.txt" env:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Path) }) cli.Call(cmd) }
Output: file.txt
Example (Duration) ¶
package main import ( "fmt" "time" "github.com/segmentio/cli" ) func main() { type config struct { Duration time.Duration `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Duration) }) cli.Call(cmd) cli.Call(cmd, "-f=1ms") cli.Call(cmd, "--flag=2s") cli.Call(cmd, "-f", "3m") cli.Call(cmd, "--flag", "4h") }
Output: 0s 1ms 2s 3m0s 4h0m0s
Example (Embedded_struct) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type embed struct { AnotherString string `flag:"--another-string" default:"b"` } type config struct { String string `flag:"--string" default:"a"` embed } cmd := cli.Command(func(config config) { fmt.Println(config.String, config.AnotherString) }) cli.Call(cmd) cli.Call(cmd, "--string", "A") cli.Call(cmd, "--another-string", "B") cli.Call(cmd, "--string", "A", "--another-string", "B") }
Output: a b A b a B A B
Example (Environment) ¶
package main import ( "fmt" "os" "github.com/segmentio/cli" ) func main() { type config struct { String string `flag:"-f,--flag" default:"-"` } // If you don't specify the name using NamedCommand, it defaults // to the binary name. In this test, the name must correspond to the prefix // of the environment variable. cmd := cli.NamedCommand("prog", cli.Command(func(config config) { fmt.Println(config.String) })) os.Setenv("PROG_FLAG", "hello world") cli.Err = os.Stdout cli.Call(cmd) }
Output: hello world
Example (Float) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { Float float64 `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Float) }) cli.Call(cmd) cli.Call(cmd, "-f=1") cli.Call(cmd, "--flag=2") cli.Call(cmd, "-f", "3") cli.Call(cmd, "--flag", "4") }
Output: 0 1 2 3 4
Example (Help) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { type config struct { Path string `flag:"--path" help:"Path to some file" default:"file" env:"-"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } cmd := cli.CommandSet{ "do": cli.Command(func(config config) { // ... }), } cli.Err = os.Stdout cli.Call(cmd, "do", "-h") }
Output: Usage: do [options] Options: -d, --debug Enable debug mode -h, --help Show this help message --path string Path to some file (default: file)
Example (HelpContext) ¶
package main import ( "context" "os" "github.com/segmentio/cli" ) func main() { type config struct { Path string `flag:"--path" help:"Path to some file" default:"file" env:"-"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } cmd := cli.CommandSet{ "do": cli.Command(func(ctx context.Context, config config) { // ... }), } cli.Err = os.Stdout cli.CallContext(context.Background(), cmd, "do", "-h") }
Output: Usage: do [options] Options: -d, --debug Enable debug mode -h, --help Show this help message --path string Path to some file (default: file)
Example (Int) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { Int int `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Int) }) cli.Call(cmd) cli.Call(cmd, "-f=1") cli.Call(cmd, "--flag=2") cli.Call(cmd, "-f", "3") cli.Call(cmd, "--flag", "4") }
Output: 0 1 2 3 4
Example (Positional_arguments) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct{} cmd := cli.Command(func(config config, x, y int) { fmt.Println(x, y) }) cli.Call(cmd, "10", "42") }
Output: 10 42
Example (Positional_arguments_slice) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct{} cmd := cli.Command(func(config config, paths []string) { fmt.Println(paths) }) cli.Call(cmd, "file1.txt", "file2.txt", "file3.txt") }
Output: [file1.txt file2.txt file3.txt]
Example (Required) ¶
package main import ( "fmt" "os" "github.com/segmentio/cli" ) func main() { type config struct { Path string `flag:"-p,--path" env:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Path) }) cli.Err = os.Stdout cli.Call(cmd) }
Output: Usage: [options] Options: -h, --help Show this help message -p, --path string Error: missing required flag: "--path"
Example (Slice) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { // Slice types in the configuration struct means the flag can be // passed multiple times. Input []string `flag:"-f,--flag"` } cmd := cli.Command(func(config config) { fmt.Println(config.Input) }) cli.Call(cmd) cli.Call(cmd, "-f=file1", "--flag=file2", "--flag", "file3") }
Output: [] [file1 file2 file3]
Example (SpacesInFlag) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { String string `flag:"-f, --flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.String) }) cli.Call(cmd) cli.Call(cmd, "-f=short") cli.Call(cmd, "--flag", "hello world") }
Output: short hello world
Example (String) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { String string `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.String) }) cli.Call(cmd) cli.Call(cmd, "-f=") cli.Call(cmd, "-f=short") cli.Call(cmd, "-f", "") cli.Call(cmd, "-f", "hello world") cli.Call(cmd, "--flag=") cli.Call(cmd, "--flag=long") cli.Call(cmd, "--flag", "") cli.Call(cmd, "--flag", "hello world") }
Output: short hello world long hello world
Example (TextUnmarshaler) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) type unmarshaler []byte func (u *unmarshaler) UnmarshalText(b []byte) error { *u = b return nil } func main() { type config struct { Input unmarshaler `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(string(config.Input)) }) cli.Call(cmd) cli.Call(cmd, "--flag", "hello world") }
Output: hello world
Example (Time) ¶
package main import ( "fmt" "time" "github.com/segmentio/cli" ) func main() { type config struct { Time time.Time `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Time.Unix()) }) cli.Call(cmd) cli.Call(cmd, "-f=Mon, 02 Jan 2006 15:04:05 UTC") cli.Call(cmd, "--flag=Mon, 02 Jan 2006 15:04:05 UTC") cli.Call(cmd, "-f", "Mon, 02 Jan 2006 15:04:05 UTC") cli.Call(cmd, "--flag", "Mon, 02 Jan 2006 15:04:05 UTC") }
Output: -62135596800 1136214245 1136214245 1136214245 1136214245
Example (Uint) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct { Uint uint `flag:"-f,--flag" default:"-"` } cmd := cli.Command(func(config config) { fmt.Println(config.Uint) }) cli.Call(cmd) cli.Call(cmd, "-f=1") cli.Call(cmd, "--flag=2") cli.Call(cmd, "-f", "3") cli.Call(cmd, "--flag", "4") }
Output: 0 1 2 3 4
Example (Usage) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { type config struct { Count int `flag:"-n" help:"Number of things" default:"1"` Debug bool `flag:"-d,--debug" help:"Enable debug mode"` } cmd := cli.CommandSet{ "do": cli.Command(func(config config) { // ... }), } cli.Err = os.Stdout cli.Call(cmd, "do", "-n", "abc") }
Output: Usage: do [options] Options: -d, --debug Enable debug mode -h, --help Show this help message -n int Number of things (default: 1) Error: decoding "-n": strconv.ParseInt: parsing "abc": invalid syntax
Example (With_sub_command) ¶
package main import ( "fmt" "github.com/segmentio/cli" ) func main() { type config struct{} cmd := cli.Command(func(config config, sub ...string) { fmt.Println(sub) }) cli.Call(cmd, "--", "curl", "https://segment.com") }
Output: [curl https://segment.com]
func NamedCommand ¶
NamedCommand constructs a command which carries the name passed as argument and delegate execution to cmd.
type Help ¶
type Help struct {
Cmd Function
}
Help 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.
type PrintFlusher ¶
PrintFlusher is an interface implemented by printers that may buffer content until they are flushed.
func Format ¶
func Format(format string, output io.Writer) (PrintFlusher, error)
Format returns a Printer which formats printed values.
Typical usage looks like this:
p, err := cli.Format(config.Format, os.Stdout) if err != nil { return err } defer p.Flush() ... p.Print(v1) p.Print(v2) p.Print(v3)
The 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.
The text format also interprets `fmt` tags as carrying the formatting string passed in calls to functions of the `fmt` package.
If the format name is not supported, the function returns a usage error.
Example (Json) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.Format("json", os.Stdout) if err != nil { return err } defer p.Flush() p.Print(struct { Message string }{"Hello World!"}) return nil }) cli.Call(cmd) }
Output: { "Message": "Hello World!" }
Example (Text_map) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.Format("text", os.Stdout) if err != nil { return err } defer p.Flush() p.Print(map[string]interface{}{ "ID": "1234", "Name": "A", "Value": 1, }) p.Print(map[string]interface{}{ "ID": "5678", "Name": "B", "Value": 2, }) p.Print(map[string]interface{}{ "ID": "9012", "Name": "C", "Value": 3, }) return nil }) cli.Call(cmd) }
Output: ID NAME VALUE 1234 A 1 5678 B 2 9012 C 3
Example (Text_string) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.Format("text", os.Stdout) if err != nil { return err } defer p.Flush() p.Print("hello") p.Print("world") return nil }) cli.Call(cmd) }
Output: hello world
Example (Text_struct) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.Format("text", os.Stdout) if err != nil { return err } defer p.Flush() type output struct { ID string Name string `fmt:"%q"` Value int `fmt:"% 5d"` } p.Print(output{"1234", "A", 1}) p.Print(output{"5678", "B", 2}) p.Print(output{"9012", "C", 3}) return nil }) cli.Call(cmd) }
Output: ID NAME VALUE 1234 "A" 1 5678 "B" 2 9012 "C" 3
Example (Yaml) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.Format("yaml", os.Stdout) if err != nil { return err } defer p.Flush() type output struct { Value int `json:"value"` } p.Print(output{1}) p.Print(output{2}) p.Print(output{3}) return nil }) cli.Call(cmd) }
Output: value: 1 --- value: 2 --- value: 3
func FormatList ¶ added in v0.2.4
func FormatList(format string, output io.Writer) (PrintFlusher, error)
FormatList returns a Printer which formats lists of printed values.
Typical usage looks like this:
p, err := cli.FormatList(config.Format, os.Stdout) if err != nil { return err } defer p.Flush() ... p.Print(v1) p.Print(v2) p.Print(v3)
The 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.
The text format also interprets `fmt` tags as carrying the formatting string passed in calls to functions of the `fmt` package.
If the format name is not supported, the function returns a usage error.
Example (Json) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.FormatList("json", os.Stdout) if err != nil { return err } defer p.Flush() p.Print(struct { Message string }{"Hello World!"}) return nil }) cli.Call(cmd) }
Output: [ { "Message": "Hello World!" } ]
Example (Yaml) ¶
package main import ( "os" "github.com/segmentio/cli" ) func main() { cmd := cli.Command(func() error { p, err := cli.FormatList("yaml", os.Stdout) if err != nil { return err } defer p.Flush() type output struct { Value int `json:"value"` } p.Print(output{1}) p.Print(output{2}) p.Print(output{3}) return nil }) cli.Call(cmd) }
Output: - value: 1 - value: 2 - value: 3
type Printer ¶
type Printer interface {
Print(interface{})
}
Printer is an interface implemented for high-level printing formats.
type Usage ¶
Usage 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.