-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
97 lines (79 loc) · 3.28 KB
/
app.go
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
package libcmd
import (
"io"
"os"
)
// CmdCallback is a routine that runs when new command is configured.
// If the command don't need to define new arguments or subcommands, you
// can safely run your custom code directly in this callback.
type CmdCallback func(cmd *Cmd)
// MatchCallback is a callback that is run when a command matches and
// and the parsing happens without raising an error. You can safely assume that
// the argument values are correctly loaded; however, since this callback runs
// before the subcommand processing occurs, this may not be the 'final' command
// to be executed by the user.
//
// To run code when the final command is selected, take a look at the
// Run function and the RunCallback type.
type MatchCallback func(cmd *Cmd)
// RunCallback is a callback that runs when a specified command is invoked. This
// callback only runs if the parsing did succeed.
//
// You can safely assume that all parsed args are correctly loaded and no other
// subcommands are pending. If you return an error value in this callback, it will be
// available as a return to the Run or RunArgs on the App instance.
type RunCallback func(cmd *Cmd) error
// ErrCallback is a callback that executes when the parser encounters an error.
// If you wish to recover (or ignore) the error, return a nil value to force the parser
// to continue it's normal process.
type ErrCallback func(err error) error
// HelpCallback is a callback that allows the user to customize the help text.
type HelpCallback func(cmd *Cmd, out io.Writer)
// Options defines the configuration options of the main
// App instance. The zero value of this struct reflect the default
// set of options.
type Options struct {
// When true, the automatic creation of help flags will
// be suppressed.
SuppressHelpFlag bool
// When true, do not print the help automatically when a
// help flag is set
SupressPrintHelpWhenSet bool
// When true, do not print the help automatically when a command with
// subcommands and without a Run callback is executed
SuppressPrintHelpPartialCommand bool
// When true, returns an parsing error when the number of operands does not match
// the expected number of required operands (optional and repeated operands are ignored).
StrictOperands bool
// When set, redirect the help output to the specified writer.
// When it is nil, the help text will be printed to Stdout
HelpOutput io.Writer
// Function that overrides the auto-generated help text.
OnHelp HelpCallback
}
// App defines the main application parser.
// An application can define one or more command-line arguments to parse, as well
// as define a chain of subcommands supported by the application.
//
// To get a new instance of App, use the NewApp function.
type App struct {
*Cmd
}
// NewApp returns a new instance of an app parser.
func NewApp(name, brief string) *App {
var app App
app.Cmd = newCmd()
app.Name = name
app.Brief = brief
return &app
}
// Parse runs the parser, collecting the values from the command-line
// and running any needed subcommands.
func (app *App) Parse() error {
return app.ParseArgs(os.Args[1:])
}
// ParseArgs behave like parse, but instead of looking to the command-line
// arguments, it takes an array of arguments as parameters.
func (app *App) ParseArgs(args []string) error {
return app.doRun(args)
}