func (c *DestroyEnvironmentCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func (v *VersionCommand) Init(f *gnuflag.FlagSet, args []string) error { v.out.AddFlags(f, "smart", cmd.DefaultFormatters) if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func (c *RelationGetCommand) Init(f *gnuflag.FlagSet, args []string) error { // TODO FWER implement --format shell lp:1033511 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) f.Var(newRelationIdValue(c.ctx, &c.RelationId), "r", "specify a relation by id") if err := f.Parse(true, args); err != nil { return err } if c.RelationId == -1 { return fmt.Errorf("no relation id specified") } args = f.Args() c.Key = "" if len(args) > 0 { if c.Key = args[0]; c.Key == "-" { c.Key = "" } args = args[1:] } if name, found := c.ctx.RemoteUnitName(); found { c.UnitName = name } if len(args) > 0 { c.UnitName = args[0] args = args[1:] } if c.UnitName == "" { return fmt.Errorf("no unit id specified") } return cmd.CheckEmpty(args) }
func (c *portCommand) Init(f *gnuflag.FlagSet, args []string) error { if err := f.Parse(true, args); err != nil { return err } args = f.Args() if args == nil { return errors.New("no port specified") } parts := strings.Split(args[0], "/") if len(parts) > 2 { return fmt.Errorf("expected %s; got %q", portFormat, args[0]) } port, err := strconv.Atoi(parts[0]) if err != nil { return badPort(parts[0]) } if port < 1 || port > 65535 { return badPort(port) } protocol := "tcp" if len(parts) == 2 { protocol = strings.ToLower(parts[1]) if protocol != "tcp" && protocol != "udp" { return fmt.Errorf(`protocol must be "tcp" or "udp"; got %q`, protocol) } } c.Port = port c.Protocol = protocol return cmd.CheckEmpty(args[1:]) }
func (m *Manager) handleFlags(command Command, name string, args []string) (Command, []string, error) { var flagset *gnuflag.FlagSet if flagged, ok := command.(FlaggedCommand); ok { flagset = flagged.Flags() } else { flagset = gnuflag.NewFlagSet(name, gnuflag.ExitOnError) } var helpRequested bool flagset.SetOutput(m.stderr) if flagset.Lookup("help") == nil { flagset.BoolVar(&helpRequested, "help", false, "Display help and exit") } if flagset.Lookup("h") == nil { flagset.BoolVar(&helpRequested, "h", false, "Display help and exit") } err := flagset.Parse(true, args) if err != nil { return nil, nil, err } if helpRequested { command = m.Commands["help"] args = []string{name} } else { args = flagset.Args() } return command, args, nil }
func (c *RemoteCommand) Init(f *gnuflag.FlagSet, args []string) error { f.StringVar(&c.msg, "error", "", "if set, fail") if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func (c *BootstrapCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) f.BoolVar(&c.UploadTools, "upload-tools", false, "upload local version of tools before bootstrapping") if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func (c *RpcCommand) Init(f *gnuflag.FlagSet, args []string) error { f.StringVar(&c.Value, "value", "", "doc") f.BoolVar(&c.Slow, "slow", false, "doc") if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func (c *TestCommand) Init(f *gnuflag.FlagSet, args []string) error { if !c.Minimal { f.StringVar(&c.Option, "option", "", "option-doc") } if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
// ParseArgs encapsulate the parsing of the args so this function can be // called from the testing module too. func ParseArgs(c Command, f *gnuflag.FlagSet, args []string) error { // If the command is a SuperCommand, we want to parse the args with // allowIntersperse=false (i.e. the first parameter to Parse. This will // mean that the args may contain other options that haven't been defined // yet, and that only options that relate to the SuperCommand itself can // come prior to the subcommand name. _, isSuperCommand := c.(*SuperCommand) return f.Parse(!isSuperCommand, args) }
// Init initializes the command for running. func (a *MachineAgent) Init(f *gnuflag.FlagSet, args []string) error { a.Conf.addFlags(f) f.StringVar(&a.MachineId, "machine-id", "", "id of the machine to run") if err := f.Parse(true, args); err != nil { return err } if !state.IsMachineId(a.MachineId) { return fmt.Errorf("--machine-id option must be set, and expects a non-negative integer") } return a.Conf.checkArgs(f.Args()) }
func (c *StatusCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{ "yaml": cmd.FormatYaml, "json": cmd.FormatJson, }) if err := f.Parse(true, args); err != nil { return err } return cmd.CheckEmpty(f.Args()) }
func initEnvCommandBase(c *gc.C, name string) *envcmd.EnvCommandBase { var flags gnuflag.FlagSet var cmd envcmd.EnvCommandBase cmd.SetFlags(&flags) var args []string if name != "" { args = []string{"-e", name} } err := flags.Parse(false, args) c.Assert(err, gc.IsNil) return &cmd }
func (c *DestroyRelationCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) if err := f.Parse(true, args); err != nil { return err } args = f.Args() if len(args) != 2 { return fmt.Errorf("a relation must involve two services") } c.Endpoints = args return nil }
func (c *UnexposeCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) if err := f.Parse(true, args); err != nil { return err } args = f.Args() if len(args) == 0 { return errors.New("no service name specified") } c.ServiceName = args[0] return cmd.CheckEmpty(args[1:]) }
func (c *ConfigGetCommand) Init(f *gnuflag.FlagSet, args []string) error { c.out.AddFlags(f, "smart", cmd.DefaultFormatters) if err := f.Parse(true, args); err != nil { return err } args = f.Args() if args == nil { return nil } c.Key = args[0] return cmd.CheckEmpty(args[1:]) }
func (c *JujuLogCommand) Init(f *gnuflag.FlagSet, args []string) error { f.BoolVar(&c.Debug, "debug", false, "log at debug level") f.StringVar(&c.Level, "l", "INFO", "Send log message at the given level") if err := f.Parse(true, args); err != nil { return err } args = f.Args() if args == nil { return errors.New("no message specified") } c.Message = strings.Join(args, " ") return nil }
func (c *SCPCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) if err := f.Parse(true, args); err != nil { return err } switch len(f.Args()) { case 0, 1: return errors.New("at least two arguments required") default: c.Args = f.Args() } return nil }
// prepareEnvCommand prepares a Command for a call to Init, // returning the Command and a pointer to a string that will // contain the environment name after the Command's Init method // has been called. func prepareEnvCommand(c *gc.C, name string) (cmd.Command, *string) { var flags gnuflag.FlagSet var cmd testCommand wrapped := envcmd.Wrap(&cmd) wrapped.SetFlags(&flags) var args []string if name != "" { args = []string{"-e", name} } err := flags.Parse(false, args) c.Assert(err, gc.IsNil) return wrapped, &cmd.EnvName }
// Init initializes the command for running. func (a *UnitAgent) Init(f *gnuflag.FlagSet, args []string) error { a.Conf.addFlags(f) f.StringVar(&a.UnitName, "unit-name", "", "name of the unit to run") if err := f.Parse(true, args); err != nil { return err } if a.UnitName == "" { return requiredError("unit-name") } if !state.IsUnitName(a.UnitName) { return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`) } return a.Conf.checkArgs(f.Args()) }
// Init initializes the command for running. func (c *BootstrapCommand) Init(f *gnuflag.FlagSet, args []string) error { c.Conf.addFlags(f) f.StringVar(&c.InstanceId, "instance-id", "", "instance id of this machine") yamlBase64Var(f, &c.EnvConfig, "env-config", "", "initial environment configuration (yaml, base64 encoded)") if err := f.Parse(true, args); err != nil { return err } if c.InstanceId == "" { return requiredError("instance-id") } if len(c.EnvConfig) == 0 { return requiredError("env-config") } return c.Conf.checkArgs(f.Args()) }
func (c *UnitGetCommand) Init(f *gnuflag.FlagSet, args []string) error { c.out.AddFlags(f, "smart", cmd.DefaultFormatters) if err := f.Parse(true, args); err != nil { return err } args = f.Args() if args == nil { return errors.New("no setting specified") } if args[0] != "private-address" && args[0] != "public-address" { return fmt.Errorf("unknown setting %q", args[0]) } c.Key = args[0] return cmd.CheckEmpty(args[1:]) }
func (c *DestroyUnitCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) if err := f.Parse(true, args); err != nil { return err } c.UnitNames = f.Args() if len(c.UnitNames) == 0 { return errors.New("no units specified") } for _, name := range c.UnitNames { if !state.IsUnitName(name) { return fmt.Errorf("invalid unit name: %q", name) } } return nil }
func (c *SetCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) f.Var(&c.Config, "config", "path to yaml-formatted service config") if err := f.Parse(true, args); err != nil { return err } args = f.Args() if len(args) == 0 || len(strings.Split(args[0], "=")) > 1 { return errors.New("no service name specified") } if len(c.Config.Path) > 0 && len(args) > 1 { return errors.New("cannot specify --config when using key=value arguments") } c.ServiceName, c.Options = args[0], args[1:] return nil }
func (c *RelationIdsCommand) Init(f *gnuflag.FlagSet, args []string) error { c.out.AddFlags(f, "smart", cmd.DefaultFormatters) if err := f.Parse(true, args); err != nil { return err } args = f.Args() if r, found := c.ctx.HookRelation(); found { c.Name = r.Name() } if len(args) > 0 { c.Name = args[0] args = args[1:] } else if c.Name == "" { return fmt.Errorf("no relation name specified") } return cmd.CheckEmpty(args) }
// Init initializes the command for running. func (c *SuperCommand) Init(f *gnuflag.FlagSet, args []string) error { if c.Log != nil { c.Log.AddFlags(f) } if err := f.Parse(false, args); err != nil { return err } subargs := f.Args() if len(subargs) == 0 { return fmt.Errorf("no command specified") } found := false if c.subcmd, found = c.subcmds[subargs[0]]; !found { return fmt.Errorf("unrecognized command: %s %s", c.Info().Name, subargs[0]) } return c.subcmd.Init(f, subargs[1:]) }
func (c *GetCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) // TODO(dfc) add json formatting ? c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{ "yaml": cmd.FormatYaml, }) // TODO(dfc) add --schema-only if err := f.Parse(true, args); err != nil { return err } args = f.Args() if len(args) == 0 { return errors.New("no service name specified") } c.ServiceName = args[0] return cmd.CheckEmpty(args[1:]) }
func (c *RelationListCommand) Init(f *gnuflag.FlagSet, args []string) (err error) { c.out.AddFlags(f, "smart", cmd.DefaultFormatters) if err := f.Parse(true, args); err != nil { return err } args = f.Args() v := newRelationIdValue(c.ctx, &c.RelationId) if len(args) > 0 { if err := v.Set(args[0]); err != nil { return err } args = args[1:] } if c.RelationId == -1 { return fmt.Errorf("no relation id specified") } return cmd.CheckEmpty(args) }
func (c *ResolvedCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) f.BoolVar(&c.Retry, "r", false, "re-execute failed hooks") f.BoolVar(&c.Retry, "retry", false, "") if err := f.Parse(true, args); err != nil { return err } args = f.Args() if len(args) > 0 { c.UnitName = args[0] if !state.IsUnitName(c.UnitName) { return fmt.Errorf("invalid unit name %q", c.UnitName) } args = args[1:] } else { return fmt.Errorf("no unit specified") } return cmd.CheckEmpty(args) }
func (c *AddUnitCommand) Init(f *gnuflag.FlagSet, args []string) error { addEnvironFlags(&c.EnvName, f) f.IntVar(&c.NumUnits, "n", 1, "number of service units to add") f.IntVar(&c.NumUnits, "num-units", 1, "") if err := f.Parse(true, args); err != nil { return err } args = f.Args() switch len(args) { case 1: c.ServiceName = args[0] case 0: return errors.New("no service specified") default: return cmd.CheckEmpty(args[1:]) } if c.NumUnits < 1 { return errors.New("must add at least one unit") } return nil }