diff --git a/commands/args.go b/commands/args.go index 8d08314b..129c7d83 100644 --- a/commands/args.go +++ b/commands/args.go @@ -42,6 +42,7 @@ func (a *Args) Replace(executable, command string, params ...string) { a.Executable = executable a.Command = command a.Params = params + a.GlobalFlags = []string{} } func (a *Args) Commands() []*cmd.Cmd { @@ -54,19 +55,19 @@ func (a *Args) Commands() []*cmd.Cmd { func (a *Args) ToCmd() *cmd.Cmd { c := cmd.New(a.Executable) - args := make([]string, 0) + c.WithArgs(a.GlobalFlags...) if a.Command != "" { - args = append(args, a.Command) + c.WithArg(a.Command) } for _, arg := range a.Params { if arg != "" { - args = append(args, arg) + c.WithArg(arg) } } - return c.WithArgs(args...) + return c } func (a *Args) GetParam(i int) string { diff --git a/commands/args_test.go b/commands/args_test.go index 4d3f67db..3801012f 100644 --- a/commands/args_test.go +++ b/commands/args_test.go @@ -105,3 +105,17 @@ func TestArgs_GlobalFlags_Repeated(t *testing.T) { assert.Equal(t, 0, len(args.Params)) assert.Equal(t, false, args.Noop) } + +func TestArgs_GlobalFlags_Propagate(t *testing.T) { + args := NewArgs([]string{"-c", "key=value", "status"}) + cmd := args.ToCmd() + assert.Equal(t, []string{"-c", "key=value", "status"}, cmd.Args) +} + +func TestArgs_GlobalFlags_Replaced(t *testing.T) { + args := NewArgs([]string{"-c", "key=value", "status"}) + args.Replace("open", "", "-a", "http://example.com") + cmd := args.ToCmd() + assert.Equal(t, "open", cmd.Name) + assert.Equal(t, []string{"-a", "http://example.com"}, cmd.Args) +}