-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkingpin-hintaction.go
61 lines (46 loc) · 1.57 KB
/
kingpin-hintaction.go
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
package main
import (
"log"
"os"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("kingpin-hintaction", "demo how hintaction breaks arg order")
correctOrder = app.Command("correctorder", "Command with correct arg order")
correctOrderArg1 = correctOrder.Arg("arg1", "Argument 1").Required().HintAction(giveHintStatic).String()
correctOrderArg2 = correctOrder.Arg("arg2", "Argument 2").Required().String()
brokenOrder = app.Command("brokenorder", "Command with broken arg order")
brokenOrderArg1 = brokenOrder.Arg("barg1", "Argument 1").Required().HintAction(giveHintFromVar).String()
brokenOrderArg2 = brokenOrder.Arg("barg2", "Argument 2").Required().String()
argSet = app.Flag("argset", "set of arguments to use").Default("x").String()
)
type correctOrderCommandStruct struct{}
type brokenOrderCommandStruct struct{}
func giveHintStatic() []string {
return []string{"foo", "bar"}
}
var args = []string{"foo", "bar"}
func giveHintFromVar() []string {
return args
}
func (n *correctOrderCommandStruct) run(c *kingpin.ParseContext) error {
log.Printf("correct order command")
return nil
}
func (n *brokenOrderCommandStruct) run(c *kingpin.ParseContext) error {
log.Printf("broken order command")
return nil
}
func main() {
app.Version("0.1")
app.HelpFlag.Short('h')
correctOderCommand := &correctOrderCommandStruct{}
correctOrder.Action(correctOderCommand.run)
brokenOderCommand := &brokenOrderCommandStruct{}
brokenOrder.Action(brokenOderCommand.run)
_, err := app.Parse(os.Args[1:])
if err != nil {
log.Printf("Error: %v", err)
os.Exit(1)
}
}