Documentation
¶
Overview ¶
Package acmd is a simple, useful and opinionated CLI package in Go.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrNoArgs = errors.New("no args provided")
)
Functions ¶
func AutocompleteFor ¶ added in v0.5.4
func HasHelpFlag ¶ added in v0.5.1
HasHelpFlag reports whether help flag is presented in args.
Types ¶
type Command ¶
type Command struct {
// Name of the command, ex: `init`
Name string
// Alias is an optional short second name, ex: `i`.
Alias string
// Description of the command.
Description string
// Do will be invoked.
// Deprecated: use ExecFunc or Exec.
Do func(ctx context.Context, args []string) error
// ExecFunc represents the command function.
// Use Exec if you have struct implementing this function.
ExecFunc func(ctx context.Context, args []string) error
// Exec represents the command function.
// Will be used only if ExecFunc is nil.
Exec Exec
// Subcommands of the command.
Subcommands []Command
// IsHidden reports whether command should not be show in help. Default false.
IsHidden bool
}
Command specifies a sub-command for a program's command-line interface.
type Config ¶
type Config struct {
// AppName is an optional name for the app, if empty os.Args[0] will be used.
AppName string
// AppDescription is an optional description. default is empty.
AppDescription string
// PostDescription of the command. Is shown after a help.
PostDescription string
// Version of the application.
Version string
// Output is a destionation where result will be printed.
// Exported for testing purpose only, if nil os.Stdout is used.
Output io.Writer
// Context for commands, if nil context based on os.Interrupt and syscall.SIGTERM will be used.
Context context.Context
// Args passed to the executable, if nil os.Args[1:] will be used.
Args []string
// Usage of the application, if nil default will be used.
Usage func(cfg Config, cmds []Command)
}
Config for the runner.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner of the sub-commands.
Example ¶
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/cristalhq/acmd"
)
var nopUsage = func(cfg acmd.Config, cmds []acmd.Command) {}
func main() {
testOut := os.Stdout
testArgs := []string{"someapp", "now", "--times", "3"}
const format = "15:04:05"
now, _ := time.Parse(format, "10:20:30")
cmds := []acmd.Command{
{
Name: "now",
Description: "prints current time",
ExecFunc: func(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("some name for help", flag.ContinueOnError)
times := fs.Int("times", 1, "how many times to print time")
if err := fs.Parse(args); err != nil {
return err
}
for i := 0; i < *times; i++ {
fmt.Printf("now: %s\n", now.Format(format))
}
return nil
},
},
{
Name: "status",
Description: "prints status of the system",
ExecFunc: func(ctx context.Context, args []string) error {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.githubstatus.com/", http.NoBody)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// TODO: parse response, I don't know
return nil
},
},
}
r := acmd.RunnerOf(cmds, acmd.Config{
AppName: "acmd-example",
AppDescription: "Example of acmd package",
PostDescription: "Best place to add examples",
Version: "the best v0.x.y",
Output: testOut,
Args: testArgs,
Usage: nopUsage,
})
if err := r.Run(); err != nil {
panic(err)
}
}
Output: now: 10:20:30 now: 10:20:30 now: 10:20:30
Click to show internal directories.
Click to hide internal directories.