~samwhited/cli

ref: fcf5af920f9ad23ecd9426a942ea0a272650f133 cli/cmd.go -rw-r--r-- 3.9 KiB
fcf5af92 — Sam Whited Make vet shut up 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2017 The Mellium Authors.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

// Package cli can be used to create modern command line interfaces.
//
// User interfaces created with Command and CommandSet take the form of the
// application name followed by the subcommand which may do its own parsing on
// all arguments after it.
// For instance, if recreating the "git" command it might have a subcommand
// called "commit" and each could have their own flags:
//
//     git -config mygit.config commit -interactive
//
// See the examples for the definition of this command.
package cli // import "mellium.im/cli"

import (
	"flag"
	"fmt"
	"io"
	"os"
	"strings"
)

// Command represents a new subcommand.
type Command struct {
	// Usage always starts with the name of the command, followed by a description
	// of its usage. For more information, see the Name method.
	Usage string

	// Description starts with a short, one line description. It can optionally be
	// followed by a blank line and then a longer description or help info.
	Description string

	// Flags is a flag set that provides options that are specific to this
	// subcommand.
	Flags *flag.FlagSet

	// The action to take when this command is executed. The args will be the
	// remaining command line args after all flags have been parsed.
	Run func(c *Command, args ...string) error
}

// Help writes the usage line, flags, and description for the command to the
// provided io.Writer.
// If c.Flags is a valid flag set, calling Help sets the output of c.Flags.
func (c *Command) Help(w io.Writer) {
	fmt.Fprintf(w, "Usage: %s\n\n", c.Usage)
	if c.Flags != nil {
		fmt.Fprint(w, "Options:\n\n")
		c.Flags.SetOutput(w)
		c.Flags.PrintDefaults()
	}
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, c.Description)
}

// Name returns the first word of c.Usage which will be the name of the command.
// For example with a usage line of:
//
//     commit [options]
//
// Name returns "commit".
func (c *Command) Name() string {
	idx := strings.Index(c.Usage, " ")
	if idx == -1 {
		return c.Usage
	}
	return c.Usage[:idx]
}

// ShortDesc returns the first line of c.Description.
// For example, given the description:
//
//     Stores the current contents of the index.
//
//     The content to be added can be specified in several ways: …
//
// ShortDescr returns "Stores the current contents of the index."
func (c *Command) ShortDesc() string {
	idx := strings.IndexByte(c.Description, '\n')
	if idx == -1 {
		return c.Description
	}
	return c.Description[:idx]
}

// CommandSet is a set of application subcommands and application level flags.
type CommandSet struct {
	Name     string
	Flags    *flag.FlagSet
	Commands []*Command
}

// Run attempts to run the command in the CommandSet that matches the first
// argument passed in.
// If no arguments are passed in, run prints help information to stdout.
// If the first argument does not match a command in the CommandSet, run prints
// the same help information to stderr.
func (cs *CommandSet) Run(args ...string) error {
	if len(args) == 0 || cs == nil {
		cs.Help(os.Stderr)
		return nil
	}
	for _, cmd := range cs.Commands {
		if cmd.Name() != args[0] {
			continue
		}

		return cmd.Run(cmd, args[1:]...)
	}
	cs.Help(os.Stderr)
	return nil
}

// Help prints a usage line for the command set and a list of commands to the
// provided writer.
func (cs *CommandSet) Help(w io.Writer) {
	if cs == nil {
		return
	}
	fmt.Fprintf(w, "Usage of %s:\n\n", cs.Name)
	fmt.Fprintf(w, "%s [options] command\n\n", cs.Name)
	if cs.Flags != nil {
		cs.Flags.SetOutput(w)
		cs.Flags.PrintDefaults()
	}
	fmt.Fprint(w, "\nCommands:\n\n")
	printCmds(w, cs.Commands...)
}

func printCmds(w io.Writer, commands ...*Command) {
	for _, command := range commands {
		name := command.Name()
		if short := command.ShortDesc(); short != "" {
			fmt.Fprintf(w, "\t%s\t%s\n", name, short)
			continue
		}
		fmt.Fprintf(w, "\t%s", name)
	}
}