-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciibits.go
73 lines (54 loc) · 1.81 KB
/
asciibits.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
package main
import (
"fmt"
"os"
"github.com/teris-io/cli"
)
func main() {
asciiCmd := cli.NewCommand("ascii", "convert string of decimals to ascii characters.").
WithShortcut("a").
WithArg(cli.NewArg("string", "string of decimals")).
WithOption(cli.NewOption("separator", "Separator used when parsing decimals").WithChar('s').WithType(cli.TypeString)).
WithAction(ascii)
decimalCmd := cli.NewCommand("decimal", "convert string of ascii characters to array of decimals.").
WithShortcut("d").
WithArg(cli.NewArg("string", "string of ascii characters")).
WithAction(decimal)
versionCmd := cli.NewCommand("version", "output current version").
WithShortcut("v").
WithAction(version)
app := cli.New("asciibits - used to convert array of decimals in a string to human readable ascii characters or vice versa").
WithOption(cli.NewOption("separator", "Separator to use when parsing string").WithChar('s').WithType(cli.TypeString)).
WithCommand(asciiCmd).
WithCommand(decimalCmd).
WithCommand(versionCmd)
os.Exit(app.Run(os.Args, os.Stdout))
}
func version(args []string, options map[string]string) int {
fmt.Fprintln(os.Stdout, Version)
return 0
}
func ascii(args []string, options map[string]string) int {
if options["separator"] == "" {
options["separator"] = " "
}
t, err := ParseDecimals(args[0], options["separator"])
if err != nil {
fmt.Fprintf(os.Stderr, "failed parsing decimals: %s", err)
return 1
}
fmt.Fprintln(os.Stdout, t.DecimalsToASCIIString())
return 0
}
func decimal(args []string, options map[string]string) int {
if options["separator"] == "" {
options["separator"] = " "
}
t, err := StringToDecimals(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "failed parsing string: %s", err)
return 1
}
fmt.Fprintln(os.Stdout, t.String(options["separator"]))
return 0
}