-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll.go
53 lines (44 loc) · 1.04 KB
/
roll.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
package roll
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/dmtaylor/costanza/internal/parser"
)
var printEBNF bool
// Cmd rollCmd represents the roll command
var Cmd = &cobra.Command{
Use: "roll",
Short: "Parse & do roll",
Long: `Testing command to perform basic roll via the cli.
This should approximate the 'roll' slash command`,
RunE: runRoll,
}
func init() {
Cmd.PersistentFlags().BoolVarP(
&printEBNF,
"print-ebnf",
"p",
false,
"Print EBNF for basic roll rather than parse expr",
)
}
func runRoll(_ *cobra.Command, args []string) error {
input := strings.Join(args, " ")
rollParser, err := parser.NewDNotationParser()
if err != nil {
return fmt.Errorf("failed to create rollParser: %w", err)
}
if printEBNF {
fmt.Printf("EBNF:\n")
fmt.Printf("%s\n", rollParser.GetEBNF())
} else {
results, err := rollParser.DoParse(input)
if err != nil {
return fmt.Errorf("failed to do parse: %w", err)
}
fmt.Printf("%s = %s\n", results.StrValue, strconv.Itoa(results.Value))
}
return nil
}