Index: cmd/supercommand_test.go |
=== modified file 'cmd/supercommand_test.go' |
--- cmd/supercommand_test.go 2012-02-09 11:35:24 +0000 |
+++ cmd/supercommand_test.go 2012-04-24 20:57:25 +0000 |
@@ -23,37 +23,35 @@ |
c.Name, "[options]", |
fmt.Sprintf("%s the juju", c.Name), |
"blah doc", |
- true, |
} |
} |
-func (c *TestCommand) InitFlagSet(f *gnuflag.FlagSet) { |
+func (c *TestCommand) Init(f *gnuflag.FlagSet, args []string) error { |
f.StringVar(&c.Value, "value", "", "doc") |
-} |
- |
-func (c *TestCommand) ParsePositional(args []string) error { |
- if len(args) != 0 { |
- return fmt.Errorf("BADARGS: %s", args) |
+ if err := f.Parse(true, args); err != nil { |
+ return err |
} |
- return nil |
+ return cmd.CheckEmpty(f.Args()) |
} |
-func (c *TestCommand) Run() error { |
+func (c *TestCommand) Run(ctx *cmd.Context) error { |
return fmt.Errorf("BORKEN: value is %s.", c.Value) |
} |
-func parseEmpty(args []string) (*cmd.SuperCommand, error) { |
- jc := cmd.NewSuperCommand("jujutest", "") |
- err := cmd.Parse(jc, args) |
- return jc, err |
-} |
- |
-func parseDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { |
- jc := cmd.NewSuperCommand("jujutest", "") |
+func initCmd(c cmd.Command, args []string) error { |
+ return c.Init(gnuflag.NewFlagSet("", gnuflag.ContinueOnError), args) |
+} |
+ |
+func initEmpty(args []string) (*cmd.SuperCommand, error) { |
+ jc := cmd.NewSuperCommand("jujutest", "", "") |
+ return jc, initCmd(jc, args) |
+} |
+ |
+func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { |
+ jc := cmd.NewSuperCommand("jujutest", "", "") |
tc := &TestCommand{Name: "defenestrate"} |
jc.Register(tc) |
- err := cmd.Parse(jc, args) |
- return jc, tc, err |
+ return jc, tc, initCmd(jc, args) |
} |
type CommandSuite struct{} |
@@ -61,28 +59,28 @@ |
var _ = Suite(&CommandSuite{}) |
func (s *CommandSuite) TestSubcommandDispatch(c *C) { |
- jc, err := parseEmpty([]string{}) |
+ jc, err := initEmpty([]string{}) |
c.Assert(err, ErrorMatches, `no command specified`) |
c.Assert(jc.Info().Usage(), Equals, "jujutest <command> [options] ...") |
- _, _, err = parseDefenestrate([]string{"discombobulate"}) |
- c.Assert(err, ErrorMatches, "unrecognised command: discombobulate") |
+ _, _, err = initDefenestrate([]string{"discombobulate"}) |
+ c.Assert(err, ErrorMatches, "unrecognised command: jujutest discombobulate") |
- jc, tc, err := parseDefenestrate([]string{"defenestrate"}) |
+ jc, tc, err := initDefenestrate([]string{"defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(tc.Value, Equals, "") |
c.Assert(jc.Info().Usage(), Equals, "jujutest defenestrate [options]") |
- _, tc, err = parseDefenestrate([]string{"defenestrate", "--value", "firmly"}) |
+ _, tc, err = initDefenestrate([]string{"defenestrate", "--value", "firmly"}) |
c.Assert(err, IsNil) |
c.Assert(tc.Value, Equals, "firmly") |
- _, tc, err = parseDefenestrate([]string{"defenestrate", "gibberish"}) |
- c.Assert(err, ErrorMatches, `BADARGS: \[gibberish\]`) |
+ _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"}) |
+ c.Assert(err, ErrorMatches, `unrecognised args: \[gibberish\]`) |
} |
func (s *CommandSuite) TestRegister(c *C) { |
- jc := cmd.NewSuperCommand("jujutest", "") |
+ jc := cmd.NewSuperCommand("jujutest", "", "") |
jc.Register(&TestCommand{Name: "flip"}) |
jc.Register(&TestCommand{Name: "flap"}) |
@@ -94,62 +92,61 @@ |
} |
func (s *CommandSuite) TestDebug(c *C) { |
- jc, err := parseEmpty([]string{}) |
+ jc, err := initEmpty([]string{}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.Debug, Equals, false) |
- jc, _, err = parseDefenestrate([]string{"defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.Debug, Equals, false) |
- jc, err = parseEmpty([]string{"--debug"}) |
+ jc, err = initEmpty([]string{"--debug"}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.Debug, Equals, true) |
- jc, _, err = parseDefenestrate([]string{"-d", "defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"--debug", "defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.Debug, Equals, true) |
} |
func (s *CommandSuite) TestVerbose(c *C) { |
- jc, err := parseEmpty([]string{}) |
+ jc, err := initEmpty([]string{}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.Verbose, Equals, false) |
- jc, _, err = parseDefenestrate([]string{"defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.Verbose, Equals, false) |
- jc, err = parseEmpty([]string{"--verbose"}) |
+ jc, err = initEmpty([]string{"--verbose"}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.Verbose, Equals, true) |
- jc, _, err = parseDefenestrate([]string{"-v", "defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"-v", "defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.Verbose, Equals, true) |
} |
func (s *CommandSuite) TestLogFile(c *C) { |
- jc, err := parseEmpty([]string{}) |
+ jc, err := initEmpty([]string{}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.LogFile, Equals, "") |
- jc, _, err = parseDefenestrate([]string{"defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.LogFile, Equals, "") |
- jc, err = parseEmpty([]string{"--log-file", "foo"}) |
+ jc, err = initEmpty([]string{"--log-file", "foo"}) |
c.Assert(err, ErrorMatches, "no command specified") |
c.Assert(jc.LogFile, Equals, "foo") |
- jc, _, err = parseDefenestrate([]string{"--log-file", "bar", "defenestrate"}) |
+ jc, _, err = initDefenestrate([]string{"--log-file", "bar", "defenestrate"}) |
c.Assert(err, IsNil) |
c.Assert(jc.LogFile, Equals, "bar") |
} |
func saveLog() func() { |
target, debug := log.Target, log.Debug |
- log.Target, log.Debug = nil, false |
return func() { |
log.Target, log.Debug = target, debug |
} |
@@ -158,10 +155,10 @@ |
func checkRun(c *C, args []string, debug bool, target Checker, logfile string) { |
defer saveLog()() |
args = append([]string{"defenestrate", "--value", "cheese"}, args...) |
- jc, _, err := parseDefenestrate(args) |
+ jc, _, err := initDefenestrate(args) |
c.Assert(err, IsNil) |
- err = jc.Run() |
+ err = jc.Run(cmd.DefaultContext()) |
c.Assert(err, ErrorMatches, "BORKEN: value is cheese.") |
c.Assert(log.Debug, Equals, debug) |