未验证 提交 8ce7ebd5 编写于 作者: A Avi Aryan

show help when invalid params are passed #53

上级 26a86693
......@@ -8,7 +8,8 @@ import (
// runApps runs `apps` command
func runApps(args []string) error {
flagset := baseFlagSet("apps")
flagset.Usage = usageFor(flagset, "abc apps")
basicUsage := "abc apps"
flagset.Usage = usageFor(flagset, basicUsage)
if err := flagset.Parse(args); err != nil {
return err
}
......@@ -18,7 +19,7 @@ func runApps(args []string) error {
case 0:
return app.ShowUserApps()
default:
fmt.Println("No such option. See --help")
showShortHelp(basicUsage)
}
return nil
}
......@@ -26,7 +27,8 @@ func runApps(args []string) error {
// runApp runs `app` command
func runApp(args []string) error {
flagset := baseFlagSet("app")
flagset.Usage = usageFor(flagset, "abc app [-c|--creds] [-m|--metrics] [ID|Appname]")
basicUsage := "abc app [-c|--creds] [-m|--metrics] [ID|Appname]"
flagset.Usage = usageFor(flagset, basicUsage)
creds := flagset.BoolP("creds", "c", false, "show app credentials")
metrics := flagset.BoolP("metrics", "m", false, "show app metrics")
if err := flagset.Parse(args); err != nil {
......@@ -37,14 +39,15 @@ func runApp(args []string) error {
if len(args) == 1 {
return app.ShowAppDetails(args[0], *creds, *metrics)
}
fmt.Println("No such option. See --help")
showShortHelp(basicUsage)
return nil
}
// runCreate runs `create` command
func runCreate(args []string) error {
flagset := baseFlagSet("create")
flagset.Usage = usageFor(flagset, "abc create [--es2|--es6] [--category=category] AppName")
basicUsage := "abc create [--es2|--es6] [--category=category] AppName"
flagset.Usage = usageFor(flagset, basicUsage)
// https://gobyexample.com/command-line-flags
isEs6 := flagset.Bool("es6", false, "is app es6")
isEs2 := flagset.Bool("es2", true, "is app es2")
......@@ -65,14 +68,15 @@ func runCreate(args []string) error {
return nil
}
}
fmt.Println("No such option. See --help")
showShortHelp(basicUsage)
return nil
}
// runDelete runs `delete` command
func runDelete(args []string) error {
flagset := baseFlagSet("delete")
flagset.Usage = usageFor(flagset, "abc delete [AppID|AppName]")
basicUsage := "abc delete [AppID|AppName]"
flagset.Usage = usageFor(flagset, basicUsage)
if err := flagset.Parse(args); err != nil {
return err
}
......@@ -80,6 +84,6 @@ func runDelete(args []string) error {
if len(args) == 1 {
return app.RunAppDelete(args[0])
}
fmt.Println("No such option. See --help")
showShortHelp(basicUsage)
return nil
}
......@@ -4,7 +4,6 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/appbaseio/abc/appbase/common"
"github.com/appbaseio/abc/appbase/importer"
......@@ -35,14 +34,12 @@ var destParamMap = map[string]string{
"dest.type": "_name_",
}
const importInfo string = `
abc import --src.type {DBType} --src.uri {URI} [-t|--tail] [Uri|Appname]
`
const basicUsage string = `abc import --src.type {DBType} --src.uri {URI} [-t|--tail] [Uri|Appname]`
// runImport runs the import command
func runImport(args []string) error {
flagset := baseFlagSet("import")
flagset.Usage = usageFor(flagset, importInfo)
flagset.Usage = usageFor(flagset, basicUsage)
// custom flags
tail := flagset.BoolP("tail", "t", false, "allow tail feature")
......@@ -81,7 +78,8 @@ func runImport(args []string) error {
if len(args) == 1 {
destURL = args[0]
} else {
return errors.New("Invalid set of parameters")
showShortHelp(basicUsage)
return nil
}
// create source config
......
......@@ -9,7 +9,8 @@ import (
// runLogin runs the login command
func runLogin(args []string) error {
flagset := baseFlagSet("login")
flagset.Usage = usageFor(flagset, "abc login [google|github]")
basicUsage := "abc login [google|github]"
flagset.Usage = usageFor(flagset, basicUsage)
if err := flagset.Parse(args); err != nil {
return err
}
......@@ -24,7 +25,7 @@ func runLogin(args []string) error {
fmt.Println("Logging in..")
return login.StartUserLogin(args[0])
default:
fmt.Println("Wrong number of parameters. See help (--help).")
showShortHelp(basicUsage)
}
return nil
}
......
package main
import (
"fmt"
"github.com/appbaseio/abc/appbase/logout"
)
// runLogout runs the logout command
func runLogout(args []string) error {
flagset := baseFlagSet("logout")
flagset.Usage = usageFor(flagset, "abc logout")
basicUsage := "abc logout"
flagset.Usage = usageFor(flagset, basicUsage)
if err := flagset.Parse(args); err != nil {
return err
}
......@@ -20,7 +20,7 @@ func runLogout(args []string) error {
return logout.UserLogout()
}
default:
fmt.Println("Wrong number of parameters. See help (--help).")
showShortHelp(basicUsage)
}
return nil
}
package main
import (
"fmt"
"github.com/appbaseio/abc/appbase/user"
)
func runUser(args []string) error {
flagset := baseFlagSet("user")
flagset.Usage = usageFor(flagset, "abc user")
basicUsage := "abc user"
flagset.Usage = usageFor(flagset, basicUsage)
if err := flagset.Parse(args); err != nil {
return err
}
......@@ -18,7 +18,7 @@ func runUser(args []string) error {
return user.ShowUserDetails()
}
} else {
fmt.Println("No such option. See --help")
showShortHelp(basicUsage)
}
return nil
}
......@@ -75,3 +75,9 @@ func usageFor(fs *flag.FlagSet, short string) func() {
fmt.Fprintf(os.Stderr, "\n")
}
}
func showShortHelp(short string) {
fmt.Fprintf(os.Stderr, "USAGE\n")
fmt.Fprintf(os.Stderr, " %s\n\n", short)
fmt.Println("Use --help option for more info.")
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册