-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharguments.go
59 lines (46 loc) · 1.54 KB
/
arguments.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
package main
import (
"fmt"
"os"
"slices"
"github.com/akamensky/argparse"
)
var subcommand *string
var parser = argparse.NewParser("skrive", "Log doses via the terminal")
var fileArg = parser.String("f", "file", &argparse.Options{Required: false, Help: "Set the doses file path"})
var helpFlag = parser.Flag("h", "help", &argparse.Options{Help: "Print help information"})
var positionalArguments = []string{}
func parse() error {
// Our custom help flag is used for properly displaying usage information
// The default behavior does not account for the usage of the subcommands
// defined in printHelp()
parser.DisableHelp()
subcommands := []string{"log"}
var remainingArgs []string
if len(os.Args) >= 2 && slices.Contains(subcommands, os.Args[1]) {
subcommand = &os.Args[1]
remainingArgs = slices.Insert(os.Args[2:], 0, "skrive")
} else {
remainingArgs = os.Args
}
positionalParameters := [4]*string{}
if subcommand != nil && *subcommand == "log" {
options := argparse.Options{Help: argparse.DisableDescription}
for i := range positionalParameters {
positionalParameters[i] = parser.StringPositional(&options)
}
}
var err = parser.Parse(remainingArgs)
for i := range positionalParameters {
if positionalParameters[i] == nil || *positionalParameters[i] == "" {
continue
}
positionalArguments = slices.Insert(positionalArguments, i, *positionalParameters[i])
}
return err
}
func printHelp(err error) {
parser.NewCommand("<blank>", "Open the TUI")
parser.NewCommand("log", "Log a dose")
fmt.Print(parser.Usage(err))
}