Skip to content

Commit

Permalink
add default args validate
Browse files Browse the repository at this point in the history
  • Loading branch information
futugyou authored Jul 25, 2024
1 parent 9ba94e5 commit c3f85e3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
8 changes: 5 additions & 3 deletions tour/cmd/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ var dynamoLongDesc = strings.Join([]string{
}, "\n")

var dynamoCmd = &cobra.Command{
Use: "dynamo",
Short: "dynamodb to golang struct and base repository",
Long: dynamoLongDesc,
Use: "dynamo",
Short: "dynamodb to golang struct and base repository",
Long: dynamoLongDesc,
ValidArgs: []string{"generate"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
log.Printf("dynamo have no commands named %s, plaese use dynamo --help to see detail", strings.Join(args, ","))
Expand Down
8 changes: 5 additions & 3 deletions tour/cmd/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ var mongoLongDesc = strings.Join([]string{
}, "\n")

var mongoCmd = &cobra.Command{
Use: "mongo",
Short: "mongodb to golang struct and base repository",
Long: mongoLongDesc,
Use: "mongo",
Short: "mongodb to golang struct and base repository",
Long: mongoLongDesc,
ValidArgs: []string{"generate"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
log.Printf("mongo have no commands named %s, plaese use mongo --help to see detail", strings.Join(args, ","))
Expand Down
11 changes: 5 additions & 6 deletions tour/cmd/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import (
)

var openapiCmd = &cobra.Command{
Use: "openapi",
Short: "openapi generate",
Long: "openapi generate",
Use: "openapi",
Short: "openapi generate",
Long: "openapi generate",
ValidArgs: []string{"generate", "swag2openapi"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
log.Printf("openapi have no commands named %s, plaese use openapi --help to see detail", strings.Join(args, ","))
}
},
}

Expand Down
8 changes: 5 additions & 3 deletions tour/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
)

var rootCmd = &cobra.Command{
Use: "",
Short: "",
Long: "",
Use: "",
Short: "",
Long: "",
ValidArgs: []string{"dynamo", "mongo", "openapi", "sql", "time", "word"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {

},
Expand Down
10 changes: 6 additions & 4 deletions tour/cmd/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
)

var sqlCmd = &cobra.Command{
Use: "sql",
Short: "sql",
Long: "sql",
Run: func(cmd *cobra.Command, Args []string) {},
Use: "sql",
Short: "sql",
Long: "sql",
ValidArgs: []string{"struct"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, Args []string) {},
}

var sql2structCmd = &cobra.Command{
Expand Down
8 changes: 5 additions & 3 deletions tour/cmd/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
)

var timeCmd = &cobra.Command{
Use: "time",
Short: "time format",
Long: "time format",
Use: "time",
Short: "time format",
Long: "time format",
ValidArgs: []string{"now", "calc"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
// nowTime := timer.GetNowTime()
// log.Printf("output %s, %d", nowTime.Format("2006-01-02 15:04:05"), nowTime.Unix())
Expand Down
1 change: 1 addition & 0 deletions tour/cmd/word.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var wordCmd = &cobra.Command{
Use: "word",
Short: "change word",
Long: desc,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
var content string
switch mode {
Expand Down

0 comments on commit c3f85e3

Please sign in to comment.