Handle -h and --help differently (short text vs long text)
Matt Bell committed
Nov 11, 2014 at 22:24 UTC
7a505b44c79ddbeab006d7ca72ff995a6bda6218
2 files changed
+24
-11
cmd/ipfs2/main.go
+22
-7
@@ -92,13 +92,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
92
// handle parse error (which means the commandline input was wrong,
93
// e.g. incorrect number of args, or nonexistent subcommand)
94
if err != nil {
95
- help, _ := req.Option("help").Bool()
95
+ var longHelp, shortHelp bool
96
+
97
+ if req != nil {
98
+ longHelp, _ = req.Option("help").Bool()
99
+ shortHelp, _ = req.Option("h").Bool()
100
+ }
101
102
// if the -help flag wasn't specified, show the error message
103
// or if a path was returned (user specified a valid subcommand), show the error message
104
// (this means there was an option or argument error)
105
if path != nil && len(path) > 0 {
101
- if !help {
106
+ if !longHelp && !shortHelp {
107
fmt.Printf(errorFormat, err)
108
}
109
}
@@ -107,9 +112,10 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
112
root = commands.Root
113
}
114
110
- // show the long help text if the help flag was specified, otherwise short help text
115
+ // show the long help text if the -help flag was specified or we are at the root command
116
+ // otherwise, show short help text
117
helpFunc := cmdsCli.ShortHelp
112
- if help {
118
+ if longHelp || len(path) == 0 {
119
helpFunc = cmdsCli.LongHelp
120
}
121
@@ -147,14 +153,23 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
153
}
154
155
func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
150
- help, err := req.Option("help").Bool()
156
+ longHelp, err := req.Option("help").Bool()
157
if err != nil {
158
return false, err
159
}
154
- if !help {
160
+ shortHelp, err := req.Option("h").Bool()
161
+ if err != nil {
162
+ return false, err
163
+ }
164
+ if !longHelp && !shortHelp {
165
return false, nil
166
}
157
- err = cmdsCli.LongHelp("ipfs", root, req.Path(), os.Stdout)
167
+ helpFunc := cmdsCli.ShortHelp
168
+ if longHelp || len(req.Path()) == 0 {
169
+ helpFunc = cmdsCli.LongHelp
170
+ }
171
+
172
+ err = helpFunc("ipfs", root, req.Path(), os.Stdout)
173
if err != nil {
174
return false, err
175
}
core/commands2/root.go
+2
-4
@@ -38,15 +38,13 @@ Plumbing commands:
38
39
block Interact with raw blocks in the datastore
40
object Interact with raw dag nodes
41
-
42
-
43
-Use "ipfs <command> --help" for more information about a command.
41
`,
42
43
Options: []cmds.Option{
44
cmds.StringOption("config", "c", "Path to the configuration file to use"),
45
cmds.BoolOption("debug", "D", "Operate in debug mode"),
49
- cmds.BoolOption("help", "h", "Show the command help text"),
46
+ cmds.BoolOption("help", "Show the full command help text"),
47
+ cmds.BoolOption("h", "Show a short version of the command help text"),
48
cmds.BoolOption("local", "L", "Run the command locally, instead of using the daemon"),
49
},
50
}