173
180
const helpPurpose = "show help on a command or other topic"
182
// setCommonFlags creates a new "commonflags" flagset, whose
183
// flags are shared with the argument f; this enables us to
184
// add non-global flags to f, which do not carry into subcommands.
185
func (c *SuperCommand) setCommonFlags(f *gnuflag.FlagSet) {
189
f.BoolVar(&c.showHelp, "h", false, helpPurpose)
190
f.BoolVar(&c.showHelp, "help", false, "")
191
// In the case where we are providing the basis for a plugin,
192
// plugins are required to support the --description argument.
193
// The Purpose attribute will be printed (if defined), allowing
194
// plugins to provide a sensible line of text for 'juju help plugins'.
195
f.BoolVar(&c.showDescription, "description", false, "")
197
c.commonflags = gnuflag.NewFlagSet(c.Info().Name, gnuflag.ContinueOnError)
198
c.commonflags.SetOutput(ioutil.Discard)
199
f.VisitAll(func(flag *gnuflag.Flag) {
200
c.commonflags.Var(flag.Value, flag.Name, flag.Usage)
175
204
// SetFlags adds the options that apply to all commands, particularly those
176
205
// due to logging.
177
206
func (c *SuperCommand) SetFlags(f *gnuflag.FlagSet) {
181
f.BoolVar(&c.showHelp, "h", false, helpPurpose)
182
f.BoolVar(&c.showHelp, "help", false, "")
208
// Only flags set by setCommonFlags are passed on to subcommands.
209
// Any flags added below only take effect when no subcommand is
210
// specified (e.g. juju --version).
211
f.BoolVar(&c.showVersion, "version", false, "Show the version of juju")
187
215
// Init initializes the command for running.
188
216
func (c *SuperCommand) Init(args []string) error {
217
if c.showDescription {
218
return CheckEmpty(args)
189
220
if len(args) == 0 {
190
221
c.subcmd = c.subcmds["help"]
207
238
return fmt.Errorf("unrecognized command: %s %s", c.Name, args[0])
210
c.subcmd.SetFlags(c.flags)
211
if err := c.flags.Parse(true, args); err != nil {
241
c.subcmd.SetFlags(c.commonflags)
242
if err := c.commonflags.Parse(true, args); err != nil {
214
args = c.flags.Args()
245
args = c.commonflags.Args()
216
247
// We want to treat help for the command the same way we would if we went "help foo".
217
248
args = []string{c.subcmd.Info().Name}
223
254
// Run executes the subcommand that was selected in Init.
224
255
func (c *SuperCommand) Run(ctx *Context) error {
256
if c.showDescription {
258
fmt.Fprintf(ctx.Stdout, "%s\n", c.Purpose)
260
fmt.Fprintf(ctx.Stdout, "%s: no description available\n", c.Info().Name)
225
264
if c.subcmd == nil {
226
265
panic("Run: missing subcommand; Init failed or not called")
350
390
c.topic = args[0]
352
return fmt.Errorf("extra arguments to command help: %q", args[2:])
392
if c.super.missingCallback == nil {
393
return fmt.Errorf("extra arguments to command help: %q", args[1:])
396
c.topicArgs = args[1:]
357
402
func (c *helpCommand) Run(ctx *Context) error {
403
if c.super.showVersion {
405
v.SetFlags(c.super.flags)
358
410
// If there is no help topic specified, print basic usage.
359
411
if c.topic == "" {
360
412
if _, ok := c.topics["basics"]; ok {
376
428
if helpcmd, ok := c.super.subcmds[c.topic]; ok {
377
429
info := helpcmd.Info()
378
430
info.Name = fmt.Sprintf("%s %s", c.super.Name, info.Name)
431
if c.super.usagePrefix != "" {
432
info.Name = fmt.Sprintf("%s %s", c.super.usagePrefix, info.Name)
379
434
f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError)
380
435
helpcmd.SetFlags(f)
381
436
ctx.Stdout.Write(info.Help(f))
390
445
// If we have a missing callback, call that with --help
391
446
if c.super.missingCallback != nil {
447
helpArgs := []string{"--", "--help"}
448
if len(c.topicArgs) > 0 {
449
helpArgs = append(helpArgs, c.topicArgs...)
392
451
subcmd := &missingCommand{
393
452
callback: c.super.missingCallback,
394
453
superName: c.super.Name,
396
args: []string{"--", "--help"},
398
457
err := subcmd.Run(ctx)
399
458
_, isUnrecognized := err.(*UnrecognizedCommand)