Skip to content

Commit 25d002e

Browse files
committedNov 2, 2018
#198 adding console module
1 parent e4f23f6 commit 25d002e

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
 

‎console/console.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
2+
// Source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package console
6+
7+
import (
8+
"flag"
9+
10+
"github.com/urfave/cli"
11+
)
12+
13+
// NOTE: console package type alias declarations using library `github.com/urfave/cli`.
14+
// It keeps decoupled from thrid party library for aah user and also opens up avenue
15+
// for smooth migration to new library if need be.
16+
17+
type (
18+
// App is the main structure of a console application.
19+
// It is recommended that an app be created with the func `console.NewApp()`.
20+
App = cli.App
21+
22+
// Command returns the named command on App. Returns nil if the command
23+
// does not exist
24+
Command = cli.Command
25+
26+
// Args contains apps console arguments
27+
Args = cli.Args
28+
29+
// Context is a type that is passed through to each Handler action in a console application.
30+
// Context can be used to retrieve context-specific Args and parsed command-line options.
31+
Context = cli.Context
32+
33+
// Flag is a common interface related to parsing flags in console. For more
34+
// advanced flag parsing techniques, it is recommended that this interface
35+
// be implemented.
36+
Flag = cli.Flag
37+
38+
// StringFlag is a flag with type string
39+
StringFlag = cli.StringFlag
40+
41+
// BoolFlag is a flag with type bool
42+
BoolFlag = cli.BoolFlag
43+
44+
// IntFlag is a flag with type int
45+
IntFlag = cli.IntFlag
46+
47+
// Int64Flag is a flag with type int64
48+
Int64Flag = cli.Int64Flag
49+
50+
// Float64Flag is a flag with type float64
51+
Float64Flag = cli.Float64Flag
52+
53+
// IntSlice is an opaque type for []int to satisfy flag.Value and flag.
54+
IntSlice = cli.IntSlice
55+
56+
// StringSlice is an opaque type for []string to satisfy flag.Value and flag.
57+
StringSlice = cli.StringSlice
58+
59+
// Author represents someone who has contributed to a console project.
60+
Author = cli.Author
61+
62+
// FlagsByName is to sorter interface for flags
63+
FlagsByName = cli.FlagsByName
64+
)
65+
66+
// NewApp creates a new console Application with some reasonable
67+
// defaults for Name, Usage, Version and Action.
68+
func NewApp() *App {
69+
return cli.NewApp()
70+
}
71+
72+
// NewContext creates a new context. For use in when invoking an App or Command action.
73+
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
74+
return cli.NewContext(app, set, parentCtx)
75+
}
76+
77+
// ShowAppHelp is an action that displays the help.
78+
func ShowAppHelp(c *Context) error {
79+
return cli.ShowAppHelp(c)
80+
}
81+
82+
// ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code.
83+
func ShowAppHelpAndExit(c *Context, exitCode int) {
84+
cli.ShowAppHelpAndExit(c, exitCode)
85+
}
86+
87+
// ShowCommandHelp prints help for the given command
88+
func ShowCommandHelp(c *Context, cmd string) error {
89+
return cli.ShowCommandHelp(c, cmd)
90+
}
91+
92+
// ShowCommandHelpAndExit - exits with code after showing help
93+
func ShowCommandHelpAndExit(c *Context, cmd string, code int) {
94+
cli.ShowCommandHelpAndExit(c, cmd, code)
95+
}
96+
97+
// ShowSubcommandHelp prints help for the given subcommand.
98+
func ShowSubcommandHelp(c *Context) error {
99+
return cli.ShowSubcommandHelp(c)
100+
}
101+
102+
// ShowVersion prints the version number of the App.
103+
func ShowVersion(c *Context) {
104+
cli.ShowVersion(c)
105+
}
106+
107+
// VersionFlag method customized flag name, desc for VersionFlag.
108+
func VersionFlag(f BoolFlag) {
109+
cli.VersionFlag = f
110+
}
111+
112+
// VersionPrinter method set custom func for version printer.
113+
func VersionPrinter(vp func(*Context)) {
114+
cli.VersionPrinter = vp
115+
}
116+
117+
func init() {
118+
cli.HelpFlag = cli.BoolFlag{
119+
Name: "h, help",
120+
Usage: "Shows help",
121+
}
122+
123+
cli.AppHelpTemplate = `Usage:
124+
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}
125+
{{if .Commands}}
126+
Commands:
127+
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t " }}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
128+
Global Options:
129+
{{range .VisibleFlags}}{{.}}
130+
{{end}}{{end}}
131+
`
132+
133+
cli.CommandHelpTemplate = `Name:
134+
{{.HelpName}} - {{.Usage}}
135+
136+
Usage:
137+
{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{if .Category}}
138+
139+
Category:
140+
{{.Category}}{{end}}{{if .Description}}
141+
142+
Description:
143+
{{.Description}}{{end}}{{if .VisibleFlags}}
144+
145+
Options:
146+
{{range .VisibleFlags}}{{.}}
147+
{{end}}{{end}}
148+
`
149+
}

‎console/console_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
2+
// Source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package console
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/urfave/cli"
12+
)
13+
14+
func TestConsoleMisc(t *testing.T) {
15+
app := NewApp()
16+
assert.NotNil(t, app)
17+
18+
ctx := NewContext(app, nil, nil)
19+
assert.NotNil(t, ctx)
20+
21+
VersionPrinter(func(c *Context) {
22+
t.Log("came here for version print")
23+
})
24+
25+
ShowVersion(ctx)
26+
27+
VersionFlag(BoolFlag{
28+
Name: "v, version",
29+
Usage: "Prints test app version",
30+
})
31+
32+
cli.AppHelpTemplate = ""
33+
cli.CommandHelpTemplate = ""
34+
cli.SubcommandHelpTemplate = ""
35+
ShowAppHelp(ctx)
36+
ShowCommandHelp(ctx, "help")
37+
ShowSubcommandHelp(ctx)
38+
}

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/gobwas/ws v1.0.0
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
1313
github.com/stretchr/testify v1.2.2
14+
github.com/urfave/cli v1.20.0
1415
golang.org/x/crypto v0.0.0-20181012144002-a92615f3c490
1516
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1
1617
golang.org/x/oauth2 v0.0.0-20181003184128-c57b0facaced

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2020
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2121
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
2222
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
23+
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
24+
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
2325
golang.org/x/crypto v0.0.0-20181012144002-a92615f3c490 h1:va0qYsIOza3Nlf2IncFyOql4/3XUq3vfge/Ad64bhlM=
2426
golang.org/x/crypto v0.0.0-20181012144002-a92615f3c490/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
2527
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1 h1:Y/KGZSOdz/2r0WJ9Mkmz6NJBusp0kiNx1Cn82lzJQ6w=

0 commit comments

Comments
 (0)
Please sign in to comment.