1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

Handle -h and --help differently (short text vs long text)

This commit is contained in:
Matt Bell
2014-11-11 22:24:04 -08:00
committed by Juan Batiz-Benet
parent c76a52e422
commit 7a505b44c7
2 changed files with 24 additions and 11 deletions

View File

@ -92,13 +92,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
// handle parse error (which means the commandline input was wrong, // handle parse error (which means the commandline input was wrong,
// e.g. incorrect number of args, or nonexistent subcommand) // e.g. incorrect number of args, or nonexistent subcommand)
if err != nil { if err != nil {
help, _ := req.Option("help").Bool() var longHelp, shortHelp bool
if req != nil {
longHelp, _ = req.Option("help").Bool()
shortHelp, _ = req.Option("h").Bool()
}
// if the -help flag wasn't specified, show the error message // if the -help flag wasn't specified, show the error message
// or if a path was returned (user specified a valid subcommand), show the error message // or if a path was returned (user specified a valid subcommand), show the error message
// (this means there was an option or argument error) // (this means there was an option or argument error)
if path != nil && len(path) > 0 { if path != nil && len(path) > 0 {
if !help { if !longHelp && !shortHelp {
fmt.Printf(errorFormat, err) fmt.Printf(errorFormat, err)
} }
} }
@ -107,9 +112,10 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
root = commands.Root root = commands.Root
} }
// show the long help text if the help flag was specified, otherwise short help text // show the long help text if the -help flag was specified or we are at the root command
// otherwise, show short help text
helpFunc := cmdsCli.ShortHelp helpFunc := cmdsCli.ShortHelp
if help { if longHelp || len(path) == 0 {
helpFunc = cmdsCli.LongHelp helpFunc = cmdsCli.LongHelp
} }
@ -147,14 +153,23 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
} }
func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) { func handleHelpOption(req cmds.Request, root *cmds.Command) (helpTextDisplayed bool, err error) {
help, err := req.Option("help").Bool() longHelp, err := req.Option("help").Bool()
if err != nil { if err != nil {
return false, err return false, err
} }
if !help { shortHelp, err := req.Option("h").Bool()
if err != nil {
return false, err
}
if !longHelp && !shortHelp {
return false, nil return false, nil
} }
err = cmdsCli.LongHelp("ipfs", root, req.Path(), os.Stdout) helpFunc := cmdsCli.ShortHelp
if longHelp || len(req.Path()) == 0 {
helpFunc = cmdsCli.LongHelp
}
err = helpFunc("ipfs", root, req.Path(), os.Stdout)
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -38,15 +38,13 @@ Plumbing commands:
block Interact with raw blocks in the datastore block Interact with raw blocks in the datastore
object Interact with raw dag nodes object Interact with raw dag nodes
Use "ipfs <command> --help" for more information about a command.
`, `,
Options: []cmds.Option{ Options: []cmds.Option{
cmds.StringOption("config", "c", "Path to the configuration file to use"), cmds.StringOption("config", "c", "Path to the configuration file to use"),
cmds.BoolOption("debug", "D", "Operate in debug mode"), cmds.BoolOption("debug", "D", "Operate in debug mode"),
cmds.BoolOption("help", "h", "Show the command help text"), cmds.BoolOption("help", "Show the full command help text"),
cmds.BoolOption("h", "Show a short version of the command help text"),
cmds.BoolOption("local", "L", "Run the command locally, instead of using the daemon"), cmds.BoolOption("local", "L", "Run the command locally, instead of using the daemon"),
}, },
} }