This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·84 lines (74 loc) · 1.76 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"errors"
"fmt"
"github.com/c-bata/go-prompt"
"github.com/starkriedesel/prompter"
)
func main() {
// Basic command
helloCmd := prompter.Command{
Name: "hello",
Description: "print hello world",
Executor: func(_ prompter.CmdArgs) error {
fmt.Println("Hello World")
return nil
},
}
// Command with 1 sub command
sayCmd := prompter.Command{
Name: "say",
Description: "say some words",
}
fooSubCmd := prompter.Command{
Name: "foo",
Description: "say foo",
Executor: func(_ prompter.CmdArgs) error {
fmt.Println("Prompter says \"foo\"")
return nil
},
}
sayCmd.AddSubCommands(fooSubCmd)
// Greet Command
nameArg := prompter.Argument{
Name: "--name",
Description: "your name",
ArgumentCompleter: nameCompletor,
}
greetCmd := prompter.Command{
Name: "greet",
Description: "say a greeting",
Executor: greetFunction,
}
greetCmd.AddArguments(nameArg)
// Exit command
exitCmd := prompter.ExitCommand("exit", "exit the application")
// Create the prompter completer
completer := prompter.NewCompleter()
completer.RegisterCommands(helloCmd, sayCmd, greetCmd, exitCmd)
// Start go-prompt
p := prompt.New(completer.Execute, completer.Complete,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("pewpew"),
prompt.OptionPrefixTextColor(prompt.White),
)
p.Run()
}
func greetFunction(args prompter.CmdArgs) error {
if !args.Contains("--name") {
return errors.New("must provide a name")
}
name, err := args.GetFirstValue("--name")
if err != nil {
return err
}
fmt.Printf("Hello to %s\n", name)
return nil
}
func nameCompletor(_ string, _ []string) []prompt.Suggest {
return []prompt.Suggest{
{Text: "alice"},
{Text: "bob"},
{Text: "charles"},
}
}