github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/args.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "github.com/jingweno/gh/cmd" 6 "strings" 7 ) 8 9 type Args struct { 10 Executable string 11 Command string 12 Params []string 13 beforeChain []*cmd.Cmd 14 afterChain []*cmd.Cmd 15 Noop bool 16 } 17 18 func (a *Args) Words() []string { 19 aa := make([]string, 0) 20 for _, p := range a.Params { 21 if !strings.HasPrefix(p, "-") { 22 aa = append(aa, p) 23 } 24 } 25 26 return aa 27 } 28 29 func (a *Args) Before(command ...string) { 30 a.beforeChain = append(a.beforeChain, cmd.NewWithArray(command)) 31 } 32 33 func (a *Args) After(command ...string) { 34 a.afterChain = append(a.afterChain, cmd.NewWithArray(command)) 35 } 36 37 func (a *Args) Replace(executable, command string, params ...string) { 38 a.Executable = executable 39 a.Command = command 40 a.Params = params 41 } 42 43 func (a *Args) Commands() []*cmd.Cmd { 44 result := a.beforeChain 45 result = append(result, a.ToCmd()) 46 result = append(result, a.afterChain...) 47 48 return result 49 } 50 51 func (a *Args) ToCmd() *cmd.Cmd { 52 return cmd.New(a.Executable).WithArg(a.Command).WithArgs(a.Params...) 53 } 54 55 func (a *Args) GetParam(i int) string { 56 return a.Params[i] 57 } 58 59 func (a *Args) FirstParam() string { 60 if a.ParamsSize() == 0 { 61 panic(fmt.Sprintf("Index 0 is out of bound")) 62 } 63 64 return a.Params[0] 65 } 66 67 func (a *Args) LastParam() string { 68 if a.ParamsSize()-1 < 0 { 69 panic(fmt.Sprintf("Index %d is out of bound", a.ParamsSize()-1)) 70 } 71 72 return a.Params[a.ParamsSize()-1] 73 } 74 75 func (a *Args) HasSubcommand() bool { 76 return !a.IsParamsEmpty() && a.Params[0][0] != '-' 77 } 78 79 func (a *Args) InsertParam(i int, items ...string) { 80 if i < 0 || (i != 0 && i > a.ParamsSize()-1) { 81 panic(fmt.Sprintf("Index %d is out of bound", i)) 82 } 83 84 newParams := []string{} 85 newParams = append(newParams, a.Params[:i]...) 86 newParams = append(newParams, items...) 87 newParams = append(newParams, a.Params[i:]...) 88 89 a.Params = newParams 90 } 91 92 func (a *Args) RemoveParam(i int) string { 93 newParams, item := removeItem(a.Params, i) 94 a.Params = newParams 95 96 return item 97 } 98 99 func (a *Args) ReplaceParam(i int, item string) { 100 if i < 0 || i > a.ParamsSize()-1 { 101 panic(fmt.Sprintf("Index %d is out of bound", i)) 102 } 103 104 a.Params[i] = item 105 } 106 107 func (a *Args) IndexOfParam(param string) int { 108 for i, p := range a.Params { 109 if p == param { 110 return i 111 } 112 } 113 114 return -1 115 } 116 117 func (a *Args) ParamsSize() int { 118 return len(a.Params) 119 } 120 121 func (a *Args) IsParamsEmpty() bool { 122 return a.ParamsSize() == 0 123 } 124 125 func (a *Args) PrependParams(params ...string) { 126 a.Params = append(params, a.Params...) 127 } 128 129 func (a *Args) AppendParams(params ...string) { 130 a.Params = append(a.Params, params...) 131 } 132 133 func NewArgs(args []string) *Args { 134 var command string 135 var params []string 136 if len(args) == 0 { 137 params = []string{} 138 } else { 139 command = args[0] 140 params = args[1:] 141 } 142 143 return &Args{Executable: "git", Command: command, Params: params, beforeChain: make([]*cmd.Cmd, 0), afterChain: make([]*cmd.Cmd, 0)} 144 } 145 146 func removeItem(slice []string, index int) (newSlice []string, item string) { 147 if index < 0 || index > len(slice)-1 { 148 panic(fmt.Sprintf("Index %d is out of bound", index)) 149 } 150 151 item = slice[index] 152 newSlice = append(slice[:index], slice[index+1:]...) 153 154 return newSlice, item 155 }