优化命令参数提示
This commit is contained in:
parent
f71ab54a56
commit
576ef0d31e
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -17,6 +18,11 @@ var rootCmd = &cobra.Command{
|
|||||||
Short: "hertz框架脚手架",
|
Short: "hertz框架脚手架",
|
||||||
Long: `以golib.gaore.com/GaoreGo/hertz_demo为模板的脚手架`,
|
Long: `以golib.gaore.com/GaoreGo/hertz_demo为模板的脚手架`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
cmd.Help()
|
||||||
|
} else {
|
||||||
|
fmt.Println("Invalid command")
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ var createCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
|
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
|
||||||
|
createCmd.MarkFlagRequired("project")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCreateCommand(args []string) {
|
func handleCreateCommand(args []string) {
|
||||||
|
19
vendor/github.com/spf13/pflag/flag.go
generated
vendored
19
vendor/github.com/spf13/pflag/flag.go
generated
vendored
@ -27,32 +27,23 @@ unaffected.
|
|||||||
Define flags using flag.String(), Bool(), Int(), etc.
|
Define flags using flag.String(), Bool(), Int(), etc.
|
||||||
|
|
||||||
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
||||||
|
|
||||||
var ip = flag.Int("flagname", 1234, "help message for flagname")
|
var ip = flag.Int("flagname", 1234, "help message for flagname")
|
||||||
|
|
||||||
If you like, you can bind the flag to a variable using the Var() functions.
|
If you like, you can bind the flag to a variable using the Var() functions.
|
||||||
|
|
||||||
var flagvar int
|
var flagvar int
|
||||||
func init() {
|
func init() {
|
||||||
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
||||||
}
|
}
|
||||||
|
|
||||||
Or you can create custom flags that satisfy the Value interface (with
|
Or you can create custom flags that satisfy the Value interface (with
|
||||||
pointer receivers) and couple them to flag parsing by
|
pointer receivers) and couple them to flag parsing by
|
||||||
|
|
||||||
flag.Var(&flagVal, "name", "help message for flagname")
|
flag.Var(&flagVal, "name", "help message for flagname")
|
||||||
|
|
||||||
For such flags, the default value is just the initial value of the variable.
|
For such flags, the default value is just the initial value of the variable.
|
||||||
|
|
||||||
After all flags are defined, call
|
After all flags are defined, call
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
to parse the command line into the defined flags.
|
to parse the command line into the defined flags.
|
||||||
|
|
||||||
Flags may then be used directly. If you're using the flags themselves,
|
Flags may then be used directly. If you're using the flags themselves,
|
||||||
they are all pointers; if you bind to variables, they're values.
|
they are all pointers; if you bind to variables, they're values.
|
||||||
|
|
||||||
fmt.Println("ip has value ", *ip)
|
fmt.Println("ip has value ", *ip)
|
||||||
fmt.Println("flagvar has value ", flagvar)
|
fmt.Println("flagvar has value ", flagvar)
|
||||||
|
|
||||||
@ -63,26 +54,22 @@ The arguments are indexed from 0 through flag.NArg()-1.
|
|||||||
The pflag package also defines some new functions that are not in flag,
|
The pflag package also defines some new functions that are not in flag,
|
||||||
that give one-letter shorthands for flags. You can use these by appending
|
that give one-letter shorthands for flags. You can use these by appending
|
||||||
'P' to the name of any function that defines a flag.
|
'P' to the name of any function that defines a flag.
|
||||||
|
|
||||||
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||||
var flagvar bool
|
var flagvar bool
|
||||||
func init() {
|
func init() {
|
||||||
flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
|
flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
|
||||||
}
|
}
|
||||||
flag.VarP(&flagval, "varname", "v", "help message")
|
flag.VarP(&flagval, "varname", "v", "help message")
|
||||||
|
|
||||||
Shorthand letters can be used with single dashes on the command line.
|
Shorthand letters can be used with single dashes on the command line.
|
||||||
Boolean shorthand flags can be combined with other shorthand flags.
|
Boolean shorthand flags can be combined with other shorthand flags.
|
||||||
|
|
||||||
Command line flag syntax:
|
Command line flag syntax:
|
||||||
|
|
||||||
--flag // boolean flags only
|
--flag // boolean flags only
|
||||||
--flag=x
|
--flag=x
|
||||||
|
|
||||||
Unlike the flag package, a single dash before an option means something
|
Unlike the flag package, a single dash before an option means something
|
||||||
different than a double dash. Single dashes signify a series of shorthand
|
different than a double dash. Single dashes signify a series of shorthand
|
||||||
letters for flags. All but the last shorthand letter must be boolean flags.
|
letters for flags. All but the last shorthand letter must be boolean flags.
|
||||||
|
|
||||||
// boolean flags
|
// boolean flags
|
||||||
-f
|
-f
|
||||||
-abc
|
-abc
|
||||||
@ -940,9 +927,9 @@ func (f *FlagSet) usage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --unknown (args will be empty)
|
//--unknown (args will be empty)
|
||||||
// --unknown --next-flag ... (args will be --next-flag ...)
|
//--unknown --next-flag ... (args will be --next-flag ...)
|
||||||
// --unknown arg ... (args will be arg ...)
|
//--unknown arg ... (args will be arg ...)
|
||||||
func stripUnknownFlagValue(args []string) []string {
|
func stripUnknownFlagValue(args []string) []string {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
//--unknown
|
//--unknown
|
||||||
|
12
vendor/github.com/spf13/pflag/string_slice.go
generated
vendored
12
vendor/github.com/spf13/pflag/string_slice.go
generated
vendored
@ -98,11 +98,8 @@ func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
|
|||||||
// The argument p points to a []string variable in which to store the value of the flag.
|
// The argument p points to a []string variable in which to store the value of the flag.
|
||||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||||
// For example:
|
// For example:
|
||||||
//
|
|
||||||
// --ss="v1,v2" --ss="v3"
|
// --ss="v1,v2" --ss="v3"
|
||||||
//
|
|
||||||
// will result in
|
// will result in
|
||||||
//
|
|
||||||
// []string{"v1", "v2", "v3"}
|
// []string{"v1", "v2", "v3"}
|
||||||
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
|
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||||
f.VarP(newStringSliceValue(value, p), name, "", usage)
|
f.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||||
@ -117,11 +114,8 @@ func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []s
|
|||||||
// The argument p points to a []string variable in which to store the value of the flag.
|
// The argument p points to a []string variable in which to store the value of the flag.
|
||||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||||
// For example:
|
// For example:
|
||||||
//
|
|
||||||
// --ss="v1,v2" --ss="v3"
|
// --ss="v1,v2" --ss="v3"
|
||||||
//
|
|
||||||
// will result in
|
// will result in
|
||||||
//
|
|
||||||
// []string{"v1", "v2", "v3"}
|
// []string{"v1", "v2", "v3"}
|
||||||
func StringSliceVar(p *[]string, name string, value []string, usage string) {
|
func StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||||
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
|
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||||
@ -136,11 +130,8 @@ func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage
|
|||||||
// The return value is the address of a []string variable that stores the value of the flag.
|
// The return value is the address of a []string variable that stores the value of the flag.
|
||||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||||
// For example:
|
// For example:
|
||||||
//
|
|
||||||
// --ss="v1,v2" --ss="v3"
|
// --ss="v1,v2" --ss="v3"
|
||||||
//
|
|
||||||
// will result in
|
// will result in
|
||||||
//
|
|
||||||
// []string{"v1", "v2", "v3"}
|
// []string{"v1", "v2", "v3"}
|
||||||
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
|
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
|
||||||
p := []string{}
|
p := []string{}
|
||||||
@ -159,11 +150,8 @@ func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage str
|
|||||||
// The return value is the address of a []string variable that stores the value of the flag.
|
// The return value is the address of a []string variable that stores the value of the flag.
|
||||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||||
// For example:
|
// For example:
|
||||||
//
|
|
||||||
// --ss="v1,v2" --ss="v3"
|
// --ss="v1,v2" --ss="v3"
|
||||||
//
|
|
||||||
// will result in
|
// will result in
|
||||||
//
|
|
||||||
// []string{"v1", "v2", "v3"}
|
// []string{"v1", "v2", "v3"}
|
||||||
func StringSlice(name string, value []string, usage string) *[]string {
|
func StringSlice(name string, value []string, usage string) *[]string {
|
||||||
return CommandLine.StringSliceP(name, "", value, usage)
|
return CommandLine.StringSliceP(name, "", value, usage)
|
||||||
|
Loading…
Reference in New Issue
Block a user