hub/commands/args.go

74 строки
1.1 KiB
Go
Исходник Обычный вид История

2013-06-25 22:45:14 +04:00
package commands
2013-06-26 19:58:15 +04:00
import (
"fmt"
)
2013-06-25 22:45:14 +04:00
type Args struct {
args []string
}
func (a *Args) Get(i int) string {
return a.args[i]
}
func (a *Args) First() string {
return a.args[0]
}
2013-06-26 19:58:15 +04:00
func (a *Args) Last() string {
return a.args[a.Size()-1]
}
2013-06-25 22:45:14 +04:00
func (a *Args) Rest() []string {
return a.args[1:]
}
func (a *Args) Remove(i int) string {
2013-06-26 19:58:15 +04:00
newArgs, item := removeItem(a.args, i)
2013-06-25 22:45:14 +04:00
a.args = newArgs
return item
}
2013-06-26 19:48:34 +04:00
func (a *Args) IndexOf(arg string) int {
for i, aa := range a.args {
if aa == arg {
return i
}
}
return -1
}
2013-06-25 22:45:14 +04:00
func (a *Args) Size() int {
return len(a.args)
}
2013-06-26 19:48:34 +04:00
func (a *Args) IsEmpty() bool {
return a.Size() == 0
}
func (a *Args) Array() []string {
return a.args
}
func (a *Args) Append(args ...string) {
a.args = append(a.args, args...)
}
2013-06-25 22:45:14 +04:00
func NewArgs(args []string) *Args {
return &Args{args}
}
2013-06-26 19:58:15 +04:00
func removeItem(slice []string, index int) (newSlice []string, item string) {
if index > len(slice)-1 {
panic(fmt.Sprintf("Index %d is out of bound", index))
}
item = slice[index]
newSlice = append(slice[:index], slice[index+1:]...)
return newSlice, item
}