|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -package cmd |
| 15 | +package cli |
16 | 16 |
|
17 | 17 | import ( |
18 | 18 | "context" |
@@ -64,22 +64,7 @@ func (h *Help) SetFlags(*flag.FlagSet) {} |
64 | 64 | func (h *Help) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { |
65 | 65 | switch f.NArg() { |
66 | 66 | case 0: |
67 | | - fmt.Fprintf(h.cdr.Output, "Usage: %s <flags> <subcommand> <subcommand args>\n\n", h.cdr.Name()) |
68 | | - fmt.Fprintf(h.cdr.Output, `runsc is the gVisor container runtime. |
69 | | -
|
70 | | -Functionality is provided by subcommands. For help with a specific subcommand, |
71 | | -use "%s %s <subcommand>". |
72 | | -
|
73 | | -`, h.cdr.Name(), h.Name()) |
74 | | - h.cdr.VisitGroups(func(g *subcommands.CommandGroup) { |
75 | | - h.cdr.ExplainGroup(h.cdr.Output, g) |
76 | | - }) |
77 | | - |
78 | | - fmt.Fprintf(h.cdr.Output, "Additional help topics (Use \"%s %s <topic>\" to see help on the topic):\n", h.cdr.Name(), h.Name()) |
79 | | - for _, cmd := range h.commands { |
80 | | - fmt.Fprintf(h.cdr.Output, "\t%-15s %s\n", cmd.Name(), cmd.Synopsis()) |
81 | | - } |
82 | | - fmt.Fprintf(h.cdr.Output, "\nUse \"%s flags\" for a list of top-level flags\n", h.cdr.Name()) |
| 67 | + h.printTopLevelHelp() |
83 | 68 | return subcommands.ExitSuccess |
84 | 69 | default: |
85 | 70 | // Look for commands registered to the commander and print help explanation if found. |
@@ -114,7 +99,64 @@ use "%s %s <subcommand>". |
114 | 99 | return subcommands.ExitUsageError |
115 | 100 | } |
116 | 101 |
|
| 102 | +func (h *Help) printTopLevelHelp() { |
| 103 | + fmt.Fprintf(h.cdr.Output, "Usage: %s <flags> <subcommand> <subcommand args>\n\n", h.cdr.Name()) |
| 104 | + fmt.Fprintf(h.cdr.Output, `runsc is the gVisor container runtime. |
| 105 | +
|
| 106 | +Functionality is provided by subcommands. For help with a specific subcommand, |
| 107 | +use "%s %s <subcommand>". |
| 108 | +
|
| 109 | +`, h.cdr.Name(), h.Name()) |
| 110 | + h.cdr.VisitGroups(func(g *subcommands.CommandGroup) { |
| 111 | + h.cdr.ExplainGroup(h.cdr.Output, g) |
| 112 | + }) |
| 113 | + |
| 114 | + fmt.Fprintf(h.cdr.Output, "Additional help topics (Use \"%s %s <topic>\" to see help on the topic):\n", h.cdr.Name(), h.Name()) |
| 115 | + for _, cmd := range h.commands { |
| 116 | + fmt.Fprintf(h.cdr.Output, "\t%-15s %s\n", cmd.Name(), cmd.Synopsis()) |
| 117 | + } |
| 118 | + fmt.Fprintf(h.cdr.Output, "\nUse \"%s flags\" for a list of top-level flags\n", h.cdr.Name()) |
| 119 | +} |
| 120 | + |
117 | 121 | // Register registers a new help command. |
118 | 122 | func (h *Help) Register(cmd subcommands.Command) { |
119 | 123 | h.commands = append(h.commands, cmd) |
120 | 124 | } |
| 125 | + |
| 126 | +// helpCommandWrapper implements subcommands.Command by wrapping another |
| 127 | +// subcommands.Command and adding -h and -help flags. |
| 128 | +type helpCommandWrapper struct { |
| 129 | + wrapped subcommands.Command |
| 130 | + help bool |
| 131 | +} |
| 132 | + |
| 133 | +// Name implements subcommands.Command.Name. |
| 134 | +func (h *helpCommandWrapper) Name() string { |
| 135 | + return h.wrapped.Name() |
| 136 | +} |
| 137 | + |
| 138 | +// Synopsis implements subcommands.Command.Synopsis. |
| 139 | +func (h *helpCommandWrapper) Synopsis() string { |
| 140 | + return h.wrapped.Synopsis() |
| 141 | +} |
| 142 | + |
| 143 | +// Usage implements subcommands.Command.Usage. |
| 144 | +func (h *helpCommandWrapper) Usage() string { |
| 145 | + return h.wrapped.Usage() |
| 146 | +} |
| 147 | + |
| 148 | +// SetFlags implements subcommands.Command.SetFlags. |
| 149 | +func (h *helpCommandWrapper) SetFlags(f *flag.FlagSet) { |
| 150 | + f.BoolVar(&h.help, "help", false, "show this message and exit") |
| 151 | + f.BoolVar(&h.help, "h", false, "equivalent to the 'help' flag") |
| 152 | + h.wrapped.SetFlags(f) |
| 153 | +} |
| 154 | + |
| 155 | +// Execute implements subcommands.Command.Execute. |
| 156 | +func (h *helpCommandWrapper) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus { |
| 157 | + if h.help { |
| 158 | + f.Usage() |
| 159 | + return subcommands.ExitSuccess |
| 160 | + } |
| 161 | + return h.wrapped.Execute(ctx, f, args...) |
| 162 | +} |
0 commit comments