func RunPlugin(ctx *cmd.Context, subcommand string, args []string) error { cmdName := JujuPluginPrefix + subcommand plugin := modelcmd.Wrap(&PluginCommand{name: cmdName}) // We process common flags supported by Juju commands. // To do this, we extract only those supported flags from the // argument list to avoid confusing flags.Parse(). flags := gnuflag.NewFlagSet(cmdName, gnuflag.ContinueOnError) flags.SetOutput(ioutil.Discard) plugin.SetFlags(flags) jujuArgs := extractJujuArgs(args) if err := flags.Parse(false, jujuArgs); err != nil { return err } if err := plugin.Init(args); err != nil { return err } err := plugin.Run(ctx) _, execError := err.(*exec.Error) // exec.Error results are for when the executable isn't found, in // those cases, drop through. if !execError { return err } return &cmd.UnrecognizedCommand{Name: subcommand} }
// HelpText returns a command's formatted help text. func HelpText(command cmd.Command, name string) string { buff := &bytes.Buffer{} info := command.Info() info.Name = name f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError) command.SetFlags(f) buff.Write(info.Help(f)) return buff.String() }
func (c *helpCommand) globalOptions() string { buf := &bytes.Buffer{} fmt.Fprintf(buf, `Global Options These options may be used with any command, and may appear in front of any command. `) f := gnuflag.NewFlagSet("", gnuflag.ContinueOnError) c.super.SetCommonFlags(f) f.SetOutput(buf) f.PrintDefaults() return buf.String() }
func (c *helpCommand) getCommandHelp(super *SuperCommand, command Command, alias string) []byte { info := command.Info() if command != super { logger.Tracef("command not super") // If the alias is to a subcommand of another super command // the alias string holds the "super sub" name. if alias == "" { info.Name = fmt.Sprintf("%s %s", super.Name, info.Name) } else { info.Name = fmt.Sprintf("%s %s", super.Name, alias) } } if super.usagePrefix != "" { logger.Tracef("adding super prefix") info.Name = fmt.Sprintf("%s %s", super.usagePrefix, info.Name) } f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError) command.SetFlags(f) return info.Help(f) }
// NewFlagSet creates a new flag set using the standard options, particularly // the option to stop the gnuflag methods from writing to StdErr or StdOut. func NewFlagSet() *gnuflag.FlagSet { fs := gnuflag.NewFlagSet("", gnuflag.ContinueOnError) fs.SetOutput(ioutil.Discard) return fs }