2024-10-29 07:22:28 +01:00
|
|
|
package command
|
|
|
|
|
2024-10-31 07:26:03 +01:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
2024-10-30 07:25:05 +01:00
|
|
|
|
|
|
|
type CommandType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
Single CommandType = "single"
|
|
|
|
Collection CommandType = "collection"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Flag struct {
|
|
|
|
Name string
|
|
|
|
Short string
|
|
|
|
Description string
|
|
|
|
Required bool
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:28 +01:00
|
|
|
type Command struct {
|
2024-10-30 07:25:05 +01:00
|
|
|
Name string
|
2024-10-31 07:26:03 +01:00
|
|
|
LocalID int
|
2024-10-30 07:25:05 +01:00
|
|
|
Type CommandType
|
|
|
|
Description string
|
|
|
|
Flags []*Flag
|
|
|
|
Action func([]*Flag) error
|
2024-10-29 07:22:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type CLI struct {
|
2024-10-31 07:26:03 +01:00
|
|
|
cmds []*Command
|
2024-10-30 07:25:05 +01:00
|
|
|
}
|
|
|
|
|
2024-10-31 07:26:03 +01:00
|
|
|
func (cli *CLI) ParseArg(args []string) *Command {
|
|
|
|
name, args := args[0], args[1:]
|
2024-10-30 07:25:05 +01:00
|
|
|
|
2024-10-31 07:26:03 +01:00
|
|
|
cmds := cli.collection
|
|
|
|
id, err := strconv.Atoi(name)
|
2024-10-30 07:25:05 +01:00
|
|
|
if err == nil {
|
2024-10-31 07:26:03 +01:00
|
|
|
name, args = args[0], args[1:]
|
|
|
|
cmds = cli.single
|
2024-10-30 07:25:05 +01:00
|
|
|
}
|
|
|
|
|
2024-10-31 07:26:03 +01:00
|
|
|
for _, c := range cmds {
|
|
|
|
if c.Name == name {
|
|
|
|
c.LocalID = id
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := make([]*Flag, 0, len(args))
|
|
|
|
for _, arg := range args {
|
|
|
|
|
|
|
|
}
|
2024-10-30 07:25:05 +01:00
|
|
|
}
|