Skip to content

Commit

Permalink
Merge pull request #6 from jwaldrip/subcmd-err
Browse files Browse the repository at this point in the history
Subcmd err
  • Loading branch information
jwaldrip committed Oct 13, 2014
2 parents 32ba1e6 + 682f38a commit e429114
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ func (cmd *CLI) ErrOutput() io.Writer {
return cmd.errOutput
}

// SetErrOutput sets the error output for the command
func (cmd *CLI) SetErrOutput(writer io.Writer) {
cmd.errOutput = writer
}

// SetStdOutput sets the standard output for the command
func (cmd *CLI) SetStdOutput(writer io.Writer) {
cmd.stdOutput = writer
}

// Mute mutes the output
func (cmd *CLI) Mute() {
var err error
Expand Down
3 changes: 2 additions & 1 deletion cli/subCommand_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ func (cmd *CLI) parseSubCommands(args []string) ([]string, bool) {
name := args[0]
subcmd, ok := cmd.subCommands[name]
if !ok {
subcmd.errf("invalid command: %s", name)
cmd.errf("invalid command: %s", name)
return args, false
}
subcmd.Start(args...)

Expand Down
6 changes: 6 additions & 0 deletions cli/subcommand_parsing_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli_test

import (
"bytes"

. "github.com/jwaldrip/odin/cli"

. "github.com/onsi/ginkgo"
Expand All @@ -13,8 +15,10 @@ var _ = Describe("CLI Start", func() {
var cmd Command
var didRun bool
var didRunSub bool
var errout *bytes.Buffer

BeforeEach(func() {
errout = bytes.NewBufferString("")
didRun = false
runFn := func(c Command) {
cmd = c
Expand All @@ -23,6 +27,7 @@ var _ = Describe("CLI Start", func() {
cli = New("v1.0.0", "sample description", runFn)
cli.ErrorHandling = PanicOnError
cli.Mute()
cli.SetErrOutput(errout)
didRunSub = false
cli.DefineSubCommand("razzle", "razzle dazzle me", func(c Command) {
didRunSub = true
Expand All @@ -39,6 +44,7 @@ var _ = Describe("CLI Start", func() {
Context("when the subcommand is not valid", func() {
It("should raise an error", func() {
Expect(func() { cli.Start("cmd", "bad") }).Should(Panic())
Expect(errout.String()).To(ContainSubstring("invalid command: bad"))
})
})

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import odin "github.com/jwaldrip/odin/cli"

// VERSION is the odin version
var VERSION = "1.3.0"
var VERSION = "1.3.1"

var cli = odin.New(VERSION, "a command line DSL for go-lang", func(cmd odin.Command) { cmd.Usage() })

Expand Down

0 comments on commit e429114

Please sign in to comment.