-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.go
86 lines (75 loc) · 2.54 KB
/
cli.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
85
86
package main
import (
"flag"
"fmt"
"os"
"github.com/fatih/color"
toml "github.com/pelletier/go-toml"
"pkg.amethysts.studio/renpy-graphviz/parser"
)
// isFlag checks if a string is a flag (starts with a dash)
func isFlag(arg string) bool {
return len(arg) > 0 && arg == "-"
}
// PlugCLI handles the Command Line Interface
func PlugCLI() (string, parser.RenpyGraphOptions) {
flag.Usage = func() {
color.Set(color.Bold)
fmt.Fprintf(os.Stderr, "Usage of: ")
color.Blue(os.Args[0])
color.Unset()
fmt.Println(" args\n\tPath to your Ren'Py game folder")
flag.PrintDefaults()
}
var hideEdgesLabels bool
var showAtoms bool
var silent bool
var openFile bool
var hideScreens bool
var hideNestedScreens bool
var fullDebug bool
var skipFilesRegex string
// TOML
getDefaultConfig()
config, err := toml.LoadFile("renpy-graphviz.config")
if err != nil {
fmt.Println("Error ", err.Error())
} else {
showAtoms = config.Get("config.atoms").(bool)
hideEdgesLabels = !config.Get("config.edges").(bool)
openFile = config.Get("config.open").(bool)
hideScreens = !config.Get("config.screens").(bool)
hideNestedScreens = !config.Get("config.nested-screens").(bool)
silent = config.Get("config.silent").(bool)
fullDebug = config.Get("config.debug").(bool)
skipFilesRegex = config.Get("config.skip-files").(string)
}
// CLI overrides TOML
flag.BoolVar(&showAtoms, "atoms", showAtoms, "Show atoms (lonely nodes)")
flag.BoolVar(&hideEdgesLabels, "hide-edges", hideEdgesLabels, "Hide choice labels on edges")
flag.BoolVar(&silent, "silent", silent, "Display nothing to the stdout")
flag.BoolVar(&openFile, "open", openFile, "Open file in default image viewer")
flag.BoolVar(&hideScreens, "hide-screens", hideScreens, "Hide screens")
flag.BoolVar(&hideNestedScreens, "hide-nested", hideNestedScreens, "Hide nested screens")
flag.BoolVar(&fullDebug, "debug", fullDebug, "Debug")
flag.StringVar(&skipFilesRegex, "skip-files", skipFilesRegex, "Regex pattern for excluding files")
// Manually handle the non-flag argument
var path string
if len(os.Args) > 1 && !isFlag(os.Args[1]) {
path = os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...) // Remove the non-flag argument from os.Args
} else {
path = "."
}
flag.Parse()
return path, parser.RenpyGraphOptions{
ShowEdgesLabels: !hideEdgesLabels,
ShowAtoms: showAtoms,
Silent: silent,
OpenFile: openFile,
ShowScreens: !hideScreens,
ShowNestedScreens: !hideNestedScreens,
FullDebug: fullDebug,
SkipFilesRegex: skipFilesRegex,
}
}