Documentation
¶
Overview ¶
Package clix wires the global flags --quiet, --verbose, --color and --narration, the console, exit codes and signal handling around a cobra command tree. A main needs only a few lines: build the root command and call clix.Run(root, clix.Options{...}). The house style for what a command prints is in docs/conventions.md and the contract in docs/reference.
Example ¶
package main
import (
"context"
"github.com/spf13/cobra"
"github.com/vi-dev/clix"
"github.com/vi-dev/clix/console"
)
func main() {
root := &cobra.Command{
Use: "demo",
RunE: func(cmd *cobra.Command, args []string) error {
clix.ConsoleOf(cmd).Infof("hello")
return nil
},
}
clix.Run(root, clix.Options{
App: "demo",
Version: "0.1.0",
EnvPrefix: "DEMO",
Setup: func(ctx context.Context, c *console.Console) (context.Context, error) {
return ctx, nil
},
})
}
Output:
Index ¶
- func AddOutputFlag(cmd *cobra.Command, output *string)
- func ConsoleOf(cmd *cobra.Command) *console.Console
- func Execute(ctx context.Context, root *cobra.Command, opts Options) (int, error)
- func OutputFlag(cmd *cobra.Command) *string
- func Run(root *cobra.Command, opts Options)
- func ValidateOutput(output string) error
- type ExitError
- type Options
- type SignalError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddOutputFlag ¶
AddOutputFlag adds the --output flag to cmd and stores its value in output. The default is text.
func ConsoleOf ¶
ConsoleOf returns the console of a command run through Run or Execute, and panics for any other command.
func Execute ¶
Execute runs root like Run but returns the exit code and the command's error instead of exiting, and does not handle signals.
func OutputFlag ¶
OutputFlag adds the --output flag to cmd, rejects any value other than text or json before the command runs, and returns the value.
func Run ¶
Run executes root with the global flags, the console and signal handling, then exits the process with the run's exit code.
func ValidateOutput ¶
ValidateOutput returns an error unless output is text or json.
Types ¶
type ExitError ¶
type ExitError struct{ Code int }
ExitError ends the run with exit code Code and no error line.
type Options ¶
type Options struct {
// App names the program in JSON narration; empty means the root command's name.
App string
// Version is the program version in JSON narration; empty means the root command's Version.
Version string
// EnvPrefix makes <EnvPrefix>_NARRATION choose the narration format; empty means no variable is read.
EnvPrefix string
// Hint returns the hint shown under the final error, or an empty string for none; nil means no hints.
Hint func(error) string
// Setup runs once the console is ready and returns the context the command runs with; an error from it ends the run like an error from the command.
Setup func(ctx context.Context, c *console.Console) (context.Context, error)
}
Options configures Execute and Run.
type SignalError ¶
SignalError is the cause of the context's cancellation when Run receives a signal, with Sig naming that signal.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package console prints progress on stderr through tasks and lines, and results on stdout.
|
Package console prints progress on stderr through tasks and lines, and results on stdout. |
|
consoletest
Package consoletest helps tests check what a function printed.
|
Package consoletest helps tests check what a function printed. |
|
docs
|
|
|
tools/check
command
|
|
|
examples
|
|
|
app
command
|
|
|
hello
command
|