Skip to content

Commit f2a4f7b

Browse files
authored
Merge pull request #192 from ipfs/doc/document-command-struct
doc: document command fields
2 parents 4dfbfd8 + 12bc013 commit f2a4f7b

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

command.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,40 @@ type PostRunMap map[PostRunType]func(Response, ResponseEmitter) error
3232
// Command is a runnable command, with input arguments and options (flags).
3333
// It can also have Subcommands, to group units of work into sets.
3434
type Command struct {
35-
Options []Option
35+
// Options defines the flags accepted by the command. Flags on specified
36+
// on parent commands are inherited by sub commands.
37+
Options []Option
38+
39+
// Arguments defines the positional arguments for the command. These
40+
// arguments can be strings and/or files.
41+
//
42+
// The rules for valid arguments are as follows:
43+
//
44+
// 1. No required arguments may follow optional arguments.
45+
// 2. There can be at most one STDIN argument.
46+
// 3. There can be at most one variadic argument, and it must be last.
3647
Arguments []Argument
37-
PreRun func(req *Request, env Environment) error
48+
49+
// PreRun is run before Run. When executing a command on a remote
50+
// daemon, PreRun is always run in the local process.
51+
PreRun func(req *Request, env Environment) error
3852

3953
// Run is the function that processes the request to generate a response.
4054
// Note that when executing the command over the HTTP API you can only read
4155
// after writing when using multipart requests. The request body will not be
4256
// available for reading after the HTTP connection has been written to.
43-
Run Function
44-
PostRun PostRunMap
57+
Run Function
58+
59+
// PostRun is run after Run, and can transform results returned by run.
60+
// When executing a command on a remote daemon, PostRun is always run in
61+
// the local process.
62+
PostRun PostRunMap
63+
64+
// Encoders encode results from Run (and/or PostRun) in the desired
65+
// encoding.
4566
Encoders EncoderMap
67+
68+
// Helptext is the command's help text.
4669
Helptext HelpText
4770

4871
// External denotes that a command is actually an external binary.
@@ -54,7 +77,17 @@ type Command struct {
5477
// the Run Function.
5578
//
5679
// ie. If command Run returns &Block{}, then Command.Type == &Block{}
57-
Type interface{}
80+
Type interface{}
81+
82+
// Subcommands allow attaching sub commands to a command.
83+
//
84+
// Note: A command can specify both a Run function and Subcommands. If
85+
// invoked with no arguments, or an argument that matches no
86+
// sub commands, the Run function of the current command will be invoked.
87+
//
88+
// Take care when specifying both a Run function and Subcommands. A
89+
// simple typo in a sub command will invoke the parent command and may
90+
// end up returning a cryptic error to the user.
5891
Subcommands map[string]*Command
5992
}
6093

0 commit comments

Comments
 (0)