|
| 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 | +} |
0 commit comments