~samwhited/cli

ref: 8c8ba2c6e0b25fd47fad0700a94dc36972c09791 cli/cmd.go -rw-r--r-- 4.6 KiB
8c8ba2c6Sam Whited support subcommands and remove CommandSet 3 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2017 The Mellium Contributors.
// 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"
	"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

	// Commands is a set of subcommands.
	Commands []*Command

	// 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 is normally called by a CommandSet and shouldn't be called directly.
	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) {
	if c == nil {
		return
	}
	// If there is a usage line and it's more than just the name, print it.
	if c.Usage != "" && c.Name() != c.Usage {
		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, "")
	}
	if c.Description != "" {
		fmt.Fprintln(w, c.Description)
	}
	printCmds(w, c.Commands...)
}

// 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]
}

// Exec attempts to run the command that matches the first argument passed in
// (or the current command if no command name is provided and a Run function has
// been specified).
// It parses unparsed flags for each subcommand it encounters.
// If no command matches help information is written to stderr.
// If a command matches, there are remaining arguments after flag parsing
// completes, and no Run function is provided, help information is written to
// stdout.
func (c *Command) Exec(stdout, stderr io.Writer, args ...string) error {
	if c == nil {
		return nil
	}
	if c.Flags != nil {
		if !c.Flags.Parsed() {
			err := c.Flags.Parse(args)
			if err != nil {
				return err
			}
		}
		args = c.Flags.Args()
	}
	if len(args) == 0 {
		if c.Run != nil {
			return c.Run(c)
		}
		c.Help(stdout)
		return nil
	}
	for _, cmd := range c.Commands {
		if cmd.Name() != args[0] {
			continue
		}

		return cmd.Exec(stdout, stderr, args[1:]...)
	}
	if c.Run != nil {
		return c.Run(c, args...)
	}
	c.Help(stderr)
	return nil
}

func printCmds(w io.Writer, commands ...*Command) {
	if len(commands) == 0 {
		return
	}
	fmt.Fprint(w, "Commands:\n\n")
	for _, command := range commands {
		if command.Run == nil {
			continue
		}
		name := command.Name()
		if short := command.ShortDesc(); short != "" {
			fmt.Fprintf(w, "\t%s\t%s\n", name, short)
			continue
		}
		fmt.Fprintf(w, "\t%s\n", name)
	}
	found := false
	for _, command := range commands {
		if command.Run != nil {
			continue
		}
		if !found {
			fmt.Fprint(w, "\nArticles:\n\n")
		}
		found = true
		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)
	}
}