-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadme
More file actions
139 lines (114 loc) · 5.77 KB
/
readme
File metadata and controls
139 lines (114 loc) · 5.77 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#  CLI Commands & Flags
[](https://github.com/codemodify/systemkit-clicmdflags/releases/latest)


[](https://github.com/codemodify/TheFreeLicense)


[](https://goreportcard.com/report/github.com/codemodify/systemkit-clicmdflags)
[](https://godoc.org/github.com/codemodify/systemkit-clicmdflags)






#### Robust CLI commands and flags for your Go app. Elegant + the smallest footprint there is.
#  What it does
my-cli-app `help` __PATH/TO/COMMAND__
#  Why this is better than `libX` or `libY`
- In comparison to `spf13/cobra` + `spf13/pflag`
- __clean, lean, simple and small footprint code with similar functionality__
- __global flags are recognised regardless of the position: front, middle or at the end__
- uses native __Go structs__ defined by the user
- multiple comands in one line, each with its own flags
- memory is freed after you execute a command, it matters if 10+ commands in a daemon
- In comparison to `Golang flag`
- __clean, lean, simple and small footprint code with similar functionality__
- __global flags are recognised regardless of the position: front, middle or at the end__
- uses native __Go structs__ defined by the user
- has __command__ and __sub-commands__ concept
#  Install
```go
go get github.com/codemodify/systemkit-clicmdflags
```
#  API
|
--- | ---
`flagName` | Flag name
`flagRequired` | Marks a flag as required - needs inpuut from user
`flagDefault` | Would be the value if the flag is not set by the user
`flagDescription` | Text to show in help command
```go
// {flagRequired} and {flagDefault} are MUTUALLY EXCLUSIVE
type fourCmdFlags struct {
FourCmdFlags1 bool `flagName:"fourCmdFlags1" flagRequired:"true" flagDescription:"fourCmdFlags1 description"`
FourCmdFlags2 bool `flagName:"fourCmdFlags2" flagDefault:"false" flagDescription:"fourCmdFlags2 description"`
FourCmdFlags3 bool `flagName:"fourCmdFlags3" flagRequired:"true" flagDescription:"fourCmdFlags3 description"`
FourCmdFlags4 bool `flagName:"fourCmdFlags4" flagDefault:"false" flagDescription:"fourCmdFlags4 description"`
}
```
#  Example
```go
import (
"fmt"
"os"
"path/filepath"
"log"
clicmdflags "github.com/codemodify/systemkit-clicmdflags"
)
// AppRootCmdFlags -
type AppRootCmdFlags struct {
JSON bool `flagName:"json" flagDefault:"false" flagDescription:"Enables JSON output"`
Verbose bool `flagName:"verbose" flagDefault:"false" flagDescription:"Enables verbose output"`
}
// ExtendedInfoCmdFlags -
type ExtendedInfoCmdFlags struct {
DumpCPUInfo bool `flagName:"dumpCpuInfo" flagRequired:"true" flagDescription:"Outputs also CPU info"`
}
func main() {
var appRootCmd = &clicmdflags.Command{
Name: filepath.Base(os.Args[0]),
Description: "Displays PC information",
Examples: []string{
filepath.Base(os.Args[0]) + " -json",
filepath.Base(os.Args[0]) + " -json true",
},
Flags: AppRootCmdFlags{},
}
appRootCmd.AddCommand(&clicmdflags.Command{
Name: "version",
Description: "Displays product version",
Examples: []string{
filepath.Base(os.Args[0]) + " version",
},
Handler: func(command *clicmdflags.Command) {
fmt.Println("v1.0")
},
})
appRootCmd.AddCommand(&clicmdflags.Command{
Name: "info",
Description: "Displays extended information",
Examples: []string{
filepath.Base(os.Args[0]) + " info",
filepath.Base(os.Args[0]) + " info -dumpCpuInfo",
},
Flags: ExtendedInfoCmdFlags{},
Handler: func(command *clicmdflags.Command) {
fmt.Println("SSD size is 1TB")
flags, ok := command.Flags.(ExtendedInfoCmdFlags)
if ok && flags.DumpCPUInfo {
fmt.Println("CPU is 64bit capable")
}
},
})
if err := appRootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
```




