Merge branch 'preserve-global-flags'

Fixes #1105
This commit is contained in:
Mislav Marohnić 2016-02-15 18:21:53 +13:00
Родитель f412a353b8 40e0cd22fd
Коммит 7d5a40b271
2 изменённых файлов: 19 добавлений и 4 удалений

Просмотреть файл

@ -42,6 +42,7 @@ func (a *Args) Replace(executable, command string, params ...string) {
a.Executable = executable a.Executable = executable
a.Command = command a.Command = command
a.Params = params a.Params = params
a.GlobalFlags = []string{}
} }
func (a *Args) Commands() []*cmd.Cmd { func (a *Args) Commands() []*cmd.Cmd {
@ -54,19 +55,19 @@ func (a *Args) Commands() []*cmd.Cmd {
func (a *Args) ToCmd() *cmd.Cmd { func (a *Args) ToCmd() *cmd.Cmd {
c := cmd.New(a.Executable) c := cmd.New(a.Executable)
args := make([]string, 0) c.WithArgs(a.GlobalFlags...)
if a.Command != "" { if a.Command != "" {
args = append(args, a.Command) c.WithArg(a.Command)
} }
for _, arg := range a.Params { for _, arg := range a.Params {
if arg != "" { if arg != "" {
args = append(args, arg) c.WithArg(arg)
} }
} }
return c.WithArgs(args...) return c
} }
func (a *Args) GetParam(i int) string { func (a *Args) GetParam(i int) string {

Просмотреть файл

@ -105,3 +105,17 @@ func TestArgs_GlobalFlags_Repeated(t *testing.T) {
assert.Equal(t, 0, len(args.Params)) assert.Equal(t, 0, len(args.Params))
assert.Equal(t, false, args.Noop) 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)
}