Skip to content

Commit

Permalink
Handle -h and --help differently (short text vs long text)
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Nov 12, 2014
1 parent fdb9cf0 commit e501180
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 22 additions & 7 deletions cmd/ipfs2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
// handle parse error (which means the commandline input was wrong,
// e.g. incorrect number of args, or nonexistent subcommand)
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
// or if a path was returned (user specified a valid subcommand), show the error message
// (this means there was an option or argument error)
if path != nil && len(path) > 0 {
if !help {
if !longHelp && !shortHelp {
fmt.Printf(errorFormat, err)
}
}
Expand All @@ -107,9 +112,10 @@ func createRequest(args []string) (cmds.Request, *cmds.Command, error) {
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
if help {
if longHelp || len(path) == 0 {
helpFunc = cmdsCli.LongHelp
}

Expand Down Expand Up @@ -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) {
help, err := req.Option("help").Bool()
longHelp, err := req.Option("help").Bool()
if err != nil {
return false, err
}
if !help {
shortHelp, err := req.Option("h").Bool()
if err != nil {
return false, err
}
if !longHelp && !shortHelp {
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 {
return false, err
}
Expand Down
6 changes: 2 additions & 4 deletions core/commands2/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ Plumbing commands:
block Interact with raw blocks in the datastore
object Interact with raw dag nodes
Use "ipfs <command> --help" for more information about a command.
`,

Options: []cmds.Option{
cmds.StringOption("config", "c", "Path to the configuration file to use"),
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"),
},
}
Expand Down

0 comments on commit e501180

Please sign in to comment.