Documentation ¶
Overview ¶
Package cli implements utility functions for running command line applications.
Index ¶
- Variables
- func Context() context.Context
- func MustPrint(encoding string, i interface{})
- func Print(encoding string, value interface{}) error
- func PrintWriter(encoding string, value interface{}, w io.Writer) error
- func ReadLine(ctx context.Context) string
- func ReadLines(ctx context.Context) <-chan string
- func ReceiveSignal(s os.Signal)
Constants ¶
This section is empty.
Variables ¶
var ( // JSONHTMLEscape controls the SetEscapeHTML option in the "json" output // format. If this is true then problematic HTML characters will be escaped // inside JSON quoted strings. // // By default this is disabled to not interfere with readability of the // output. If you are rendering output in an HTML context you should enable // this feature. JSONHTMLEscape = false )
Functions ¶
func Context ¶
Context returns a context that is cancelled automatically when a SIGINT, SIGQUIT or SIGTERM signal is received.
func MustPrint ¶
func MustPrint(encoding string, i interface{})
MustPrint is exactly like Print but panics if an error occurs.
func Print ¶
Print encodes the value using the given encoding and then prints it to the standard output. Accepted encodings are "json", "yml", "yaml", "table" and "raw". If encoding is the empty string this function defaults to "table" encoding.
Usually the encoding is controlled via command line flags of your application so the user can select in what format the output should be returned.
Accepted encodings ¶
"table": value is printed via a tab writer (see below) "json": value is printed as indented JSON "yaml": value is printed as YAML "raw": value is printed via fmt.Println
Table encoding ¶
If the "table" encoding is used, the reflection API is used to print all exported fields of the value via a tab writer. The columns will be the UPPERCASE field names or whatever you set in the "table" tag of the corresponding field. Field names with a "table" tag set to "-" are omitted. When the "table" encoding is used the value must either be a struct, pointer to a struct, a slice or an array.
func PrintWriter ¶
PrintWriter is like Print but lets the caller inject an io.Writer.
func ReadLine ¶
ReadLine reads a single line from stdin and returns it without the trailing newline. This function blocks until the first newline is read or the context is canceled. In the later case the empty string is returned.
func ReadLines ¶
ReadLines reads lines from stdin and returns them in a channel. All strings in the returned channel will not include the trailing newline. The channel is closed automatically if there are no more lines or if the context is closed.
This function panics if there was any error other than io.EOF when reading from os.Stdin.
func ReceiveSignal ¶
ReceiveSignal propagates the given signal to all contexts that may have been created via cli.Context(). This function is only useful when the terminal is hijacked and you want to emulate signals manually.
Types ¶
This section is empty.