-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
69 lines (59 loc) · 1.28 KB
/
Copy pathcommand.go
File metadata and controls
69 lines (59 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package command
import (
"os"
"github.com/ProCode-Software/klar/pkg/argparse"
)
var (
ExecName string = "klar"
Commands map[string]*Command
)
type ExampleCmd struct {
Command string
Args []string
Flags []string
Description string
}
type Command struct {
Name string
ShortDescription string
Usage []string
Aliases []string
Run RunFunc
// Shown in command help
Subcommands []*Command
Flags *argparse.Parser
LongDescription string
SeeAlso []string
Examples []ExampleCmd
}
// TODO: documentation URL
type RunFunc func(r *argparse.Parser)
func Lookup(
name string, commands map[string]*Command, aliases map[string]string,
) *Command {
if cmd, ok := commands[name]; ok {
return cmd
}
if aliases != nil {
return commands[aliases[name]]
}
return nil
}
func Run(cmd *Command) {
if cmd == nil {
panic("command: Run(nil)")
}
if cmd.Run == nil {
panic("cannot run command '" + cmd.Name + ": Run function is not defined")
}
if cmd.Flags == nil {
cmd.Flags = argparse.NewParser()
}
cmd.Flags.StartOffset = 1 // Command name
cmd.Flags.InputArgs = os.Args[1:] // klar
if err := cmd.Flags.Parse(); err != nil {
cmd.handleFlagError(err)
return
}
cmd.Run(cmd.Flags)
}