Skip to content

Commit 8cb9a9f

Browse files
committed
Add args to Execute
1 parent 4fb441f commit 8cb9a9f

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

‎cobrakai.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type root struct {
1111
c *Commandeer
1212
}
1313

14-
// Execute executes the root command and returns the commandeer that was executed.
15-
func (r *root) Execute(ctx context.Context) (*Commandeer, error) {
14+
func (r *root) Execute(ctx context.Context, args []string) (*Commandeer, error) {
15+
r.c.CobraCommand.SetArgs(args)
1616
cobraCommand, err := r.c.CobraCommand.ExecuteContextC(ctx)
1717
if err != nil {
1818
return nil, err
@@ -69,8 +69,9 @@ func (c *Commandeer) compile() error {
6969
}
7070

7171
// Executer is the execution entry point.
72+
// The args are usually filled with os.Args[1:].
7273
type Executer interface {
73-
Execute(ctx context.Context) (*Commandeer, error)
74+
Execute(ctx context.Context, args []string) (*Commandeer, error)
7475
}
7576

7677
// Commander is the interface that must be implemented by all commands.

��cobrakai_test.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cobrakai_test
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"testing"
87

98
ck "github.com/bep/cobrakai"
@@ -19,7 +18,6 @@ func TestCobraKai(t *testing.T) {
1918
fooBazCommand = &testComand2{name: "foo_baz"}
2019
)
2120

22-
os.Args = []string{"hugo", "foo", "--localFlagName", "foo_local", "--persistentFlagName", "foo_persistent"}
2321
c := qt.New(t)
2422
r, err := ck.R(
2523
&testComand1{name: "hugo"},
@@ -33,17 +31,18 @@ func TestCobraKai(t *testing.T) {
3331
// This can be anything, just used to make sure the same context is passed all the way.
3432
type key string
3533
ctx := context.WithValue(context.Background(), key("foo"), "bar")
36-
cdeer, err := r.Execute(ctx)
34+
args := []string{"foo", "--localFlagName", "foo_local", "--persistentFlagName", "foo_persistent"}
35+
cdeer, err := r.Execute(ctx, args)
3736
c.Assert(err, qt.IsNil)
3837
c.Assert(cdeer.Command.Name(), qt.Equals, "foo")
3938
tc := cdeer.Command.(*testComand1)
4039
c.Assert(tc.ctx, qt.Equals, ctx)
4140
c.Assert(tc.localFlagName, qt.Equals, "foo_local")
4241
c.Assert(tc.persistentFlagName, qt.Equals, "foo_persistent")
4342

44-
os.Args = []string{"hugo", "foo", "foo_baz", "--localFlagName", "foo_local2", "--persistentFlagName", "foo_persistent2"}
43+
args = []string{"foo", "foo_baz", "--localFlagName", "foo_local2", "--persistentFlagName", "foo_persistent2"}
4544
ctx = context.WithValue(context.Background(), key("bar"), "baz")
46-
cdeer2, err := r.Execute(ctx)
45+
cdeer2, err := r.Execute(ctx, args)
4746
c.Assert(err, qt.IsNil)
4847
c.Assert(cdeer2.Command.Name(), qt.Equals, "foo_baz")
4948
tc2 := cdeer2.Command.(*testComand2)

0 commit comments

Comments
 (0)