github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/args.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/github/hub/cmd" 8 ) 9 10 type Args struct { 11 Executable string 12 Command string 13 Params []string 14 beforeChain []*cmd.Cmd 15 afterChain []*cmd.Cmd 16 Noop bool 17 } 18 19 func (a *Args) Words() []string { 20 aa := make([]string, 0) 21 for _, p := range a.Params { 22 if !strings.HasPrefix(p, "-") { 23 aa = append(aa, p) 24 } 25 } 26 27 return aa 28 } 29 30 func (a *Args) Before(command ...string) { 31 a.beforeChain = append(a.beforeChain, cmd.NewWithArray(command)) 32 } 33 34 func (a *Args) After(command ...string) { 35 a.afterChain = append(a.afterChain, cmd.NewWithArray(command)) 36 } 37 38 func (a *Args) Replace(executable, command string, params ...string) { 39 a.Executable = executable 40 a.Command = command 41 a.Params = params 42 } 43 44 func (a *Args) Commands() []*cmd.Cmd { 45 result := a.beforeChain 46 result = append(result, a.ToCmd()) 47 result = append(result, a.afterChain...) 48 49 return result 50 } 51 52 func (a *Args) ToCmd() *cmd.Cmd { 53 return cmd.New(a.Executable).WithArg(a.Command).WithArgs(a.Params...) 54 } 55 56 func (a *Args) GetParam(i int) string { 57 return a.Params[i] 58 } 59 60 func (a *Args) FirstParam() string { 61 if a.ParamsSize() == 0 { 62 panic(fmt.Sprintf("Index 0 is out of bound")) 63 } 64 65 return a.Params[0] 66 } 67 68 func (a *Args) LastParam() string { 69 if a.ParamsSize()-1 < 0 { 70 panic(fmt.Sprintf("Index %d is out of bound", a.ParamsSize()-1)) 71 } 72 73 return a.Params[a.ParamsSize()-1] 74 } 75 76 func (a *Args) HasSubcommand() bool { 77 return !a.IsParamsEmpty() && a.Params[0][0] != '-' 78 } 79 80 func (a *Args) InsertParam(i int, items ...string) { 81 if i < 0 || (i != 0 && i > a.ParamsSize()-1) { 82 panic(fmt.Sprintf("Index %d is out of bound", i)) 83 } 84 85 newParams := []string{} 86 newParams = append(newParams, a.Params[:i]...) 87 newParams = append(newParams, items...) 88 newParams = append(newParams, a.Params[i:]...) 89 90 a.Params = newParams 91 } 92 93 func (a *Args) RemoveParam(i int) string { 94 newParams, item := removeItem(a.Params, i) 95 a.Params = newParams 96 97 return item 98 } 99 100 func (a *Args) ReplaceParam(i int, item string) { 101 if i < 0 || i > a.ParamsSize()-1 { 102 panic(fmt.Sprintf("Index %d is out of bound", i)) 103 } 104 105 a.Params[i] = item 106 } 107 108 func (a *Args) IndexOfParam(param string) int { 109 for i, p := range a.Params { 110 if p == param { 111 return i 112 } 113 } 114 115 return -1 116 } 117 118 func (a *Args) ParamsSize() int { 119 return len(a.Params) 120 } 121 122 func (a *Args) IsParamsEmpty() bool { 123 return a.ParamsSize() == 0 124 } 125 126 func (a *Args) PrependParams(params ...string) { 127 a.Params = append(params, a.Params...) 128 } 129 130 func (a *Args) AppendParams(params ...string) { 131 a.Params = append(a.Params, params...) 132 } 133 134 func (a *Args) HasFlags(flags ...string) bool { 135 for _, f := range flags { 136 if i := a.IndexOfParam(f); i != -1 { 137 return true 138 } 139 } 140 141 return false 142 } 143 144 func NewArgs(args []string) *Args { 145 var ( 146 command string 147 params []string 148 noop bool 149 ) 150 151 args, noop = slurpGlobalFlags(args) 152 153 if len(args) == 0 { 154 params = []string{} 155 } else { 156 command = args[0] 157 params = args[1:] 158 } 159 160 return &Args{ 161 Executable: "git", 162 Command: command, 163 Params: params, 164 Noop: noop, 165 beforeChain: make([]*cmd.Cmd, 0), 166 afterChain: make([]*cmd.Cmd, 0), 167 } 168 } 169 170 func slurpGlobalFlags(args []string) (aa []string, noop bool) { 171 aa = args 172 for i, arg := range args { 173 if arg == "--noop" { 174 noop = true 175 aa, _ = removeItem(args, i) 176 } else if arg == "--version" || arg == "--help" { 177 aa[i] = strings.TrimPrefix(arg, "--") 178 } 179 } 180 181 return 182 } 183 184 func removeItem(slice []string, index int) (newSlice []string, item string) { 185 if index < 0 || index > len(slice)-1 { 186 panic(fmt.Sprintf("Index %d is out of bound", index)) 187 } 188 189 item = slice[index] 190 newSlice = append(slice[:index], slice[index+1:]...) 191 192 return newSlice, item 193 }