@cryptotaxi247 / kubo / commits / 3e2016d98

commands: add --flags to show all flags

License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

Christian Couder committed Oct 17, 2015 at 09:11 UTC 3e2016d987af7ae53b6c4400566bfd1aa780fcd3
1 file changed +15 -6
core/commands/commands.go
+15 -6
@@ -19,8 +19,13 @@ type Command struct {
19 Name string
20 Subcommands []Command
21 Options []cmds.Option
22 + ShowOptions bool
23 }
24
25 +const (
26 + flagsOptionName = "flags"
27 +)
28 +
29 // CommandsCmd takes in a root command,
30 // and returns a command that lists the subcommands in that root
31 func CommandsCmd(root *cmds.Command) *cmds.Command {
@@ -29,10 +34,13 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
34 Tagline: "List all available commands.",
35 ShortDescription: `Lists all available commands (and subcommands) and exits.`,
36 },
32 -
37 + Options: []cmds.Option{
38 + cmds.BoolOption(flagsOptionName, "f", "Show command flags"),
39 + },
40 Run: func(req cmds.Request, res cmds.Response) {
34 - root := cmd2outputCmd("ipfs", root)
35 - res.SetOutput(&root)
41 + showOptions, _, _ := req.Option(flagsOptionName).Bool();
42 + rootCmd := cmd2outputCmd("ipfs", root, showOptions)
43 + res.SetOutput(&rootCmd)
44 },
45 Marshalers: cmds.MarshalerMap{
46 cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -48,16 +56,17 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
56 }
57 }
58
51 -func cmd2outputCmd(name string, cmd *cmds.Command) Command {
59 +func cmd2outputCmd(name string, cmd *cmds.Command, showOptions bool) Command {
60 output := Command{
61 Name: name,
62 Subcommands: make([]Command, len(cmd.Subcommands)),
63 Options: cmd.Options,
64 + ShowOptions: showOptions,
65 }
66
67 i := 0
68 for name, sub := range cmd.Subcommands {
60 - output.Subcommands[i] = cmd2outputCmd(name, sub)
69 + output.Subcommands[i] = cmd2outputCmd(name, sub, showOptions)
70 i++
71 }
72
@@ -71,7 +80,7 @@ func cmdPathStrings(cmd *Command) []string {
80 recurse = func(prefix string, cmd *Command) {
81 newPrefix := prefix + cmd.Name
82 cmds = append(cmds, newPrefix)
74 - if prefix != "" {
83 + if prefix != "" && cmd.ShowOptions {
84 for _, option := range cmd.Options {
85 optName := option.Names()[0]
86 if len(optName) == 1 {