提交 82dedbf6 编写于 作者: J Jingwen Owen Ou

Slurp `--noop` during parsing of args

上级 94349d47
......@@ -2,8 +2,9 @@ package commands
import (
"fmt"
"github.com/github/hub/cmd"
"strings"
"github.com/github/hub/cmd"
)
type Args struct {
......@@ -131,8 +132,14 @@ func (a *Args) AppendParams(params ...string) {
}
func NewArgs(args []string) *Args {
var command string
var params []string
var (
command string
params []string
noop bool
)
args, noop = slurpGlobalFlags(args)
if len(args) == 0 {
params = []string{}
} else {
......@@ -140,7 +147,26 @@ func NewArgs(args []string) *Args {
params = args[1:]
}
return &Args{Executable: "git", Command: command, Params: params, beforeChain: make([]*cmd.Cmd, 0), afterChain: make([]*cmd.Cmd, 0)}
return &Args{
Executable: "git",
Command: command,
Params: params,
Noop: noop,
beforeChain: make([]*cmd.Cmd, 0),
afterChain: make([]*cmd.Cmd, 0),
}
}
func slurpGlobalFlags(args []string) (aa []string, noop bool) {
aa = args
for i, arg := range args {
if arg == "--noop" {
noop = true
aa, _ = removeItem(args, i)
}
}
return
}
func removeItem(slice []string, index int) (newSlice []string, item string) {
......
package commands
import (
"github.com/bmizerany/assert"
"testing"
"github.com/bmizerany/assert"
)
func TestNewArgs(t *testing.T) {
......@@ -17,6 +18,11 @@ func TestNewArgs(t *testing.T) {
args = NewArgs([]string{"command", "args"})
assert.Equal(t, "command", args.Command)
assert.Equal(t, 1, args.ParamsSize())
args = NewArgs([]string{"--noop", "command", "args"})
assert.Equal(t, "command", args.Command)
assert.Equal(t, 1, args.ParamsSize())
assert.T(t, args.Noop)
}
func TestArgs_Words(t *testing.T) {
......
......@@ -70,7 +70,6 @@ func (r *Runner) Execute() ExecError {
utils.Check(err)
expandAlias(args)
slurpGlobalFlags(args)
cmd := r.Lookup(args.Command)
if cmd != nil && cmd.Runnable() {
......@@ -100,15 +99,6 @@ func (r *Runner) Call(cmd *Command, args *Args) ExecError {
return newExecError(err)
}
func slurpGlobalFlags(args *Args) {
for i, p := range args.Params {
if p == "--noop" {
args.Noop = true
args.RemoveParam(i)
}
}
}
func printCommands(cmds []*cmd.Cmd) {
for _, c := range cmds {
fmt.Println(c)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册