diff --git a/cli/cli.go b/cli/cli.go index 2b31cff..5aec7c5 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -3,6 +3,7 @@ package cli import ( "flag" "fmt" + "os" "regexp" "strings" ) @@ -31,7 +32,9 @@ func New(args []string) *App { } func (c *App) Parse() error { - f := flag.NewFlagSet(usage(), flag.ExitOnError) + f := flag.NewFlagSet("", flag.ExitOnError) + f.Usage = c.PrintUsage + c.flag = f f.StringVar(&c.Alias, "alias", "", "Create a tunnel alias") f.BoolVar(&c.AliasDelete, "delete", false, "delete a tunnel alias (must be used with -alias)") @@ -46,12 +49,6 @@ func (c *App) Parse() error { f.Parse(c.args[1:]) - c.flag = f - - if len(c.args[1:]) == 0 { - return fmt.Errorf("not enough arguments provided") - } - if c.Help { c.Command = "help" } else if c.Version { @@ -76,17 +73,31 @@ func (c *App) Parse() error { } func (c App) Validate() error { - if c.Command == "new-alias" && (c.Remote.String() == "" || c.Server.String() == "") { - return fmt.Errorf("remote and server options are required for new alias") + if len(c.args[1:]) == 0 { + return fmt.Errorf("not enough arguments provided") } + switch c.Command { + case "start", "new-alias": + if c.Remote.String() == "" { + return fmt.Errorf("required flag is missing: -remote") + } else if c.Server.String() == "" { + return fmt.Errorf("required flag is missing: -server") + } + } return nil } // PrintUsage prints, to the standard output, the informational text on how to // use the tool. -func (c App) PrintUsage() { - fmt.Printf("%s\n", usage()) +func (c *App) PrintUsage() { + fmt.Fprintf(os.Stderr, "%s\n\n", `usage: + mole [-v] [-local []:] -remote []: -server [@][:] [-key ] + mole -alias [-v] [-local []:] -remote []: -server [@][:] [-key ] + mole -alias -delete + mole -start + mole -help + mole -version`) c.flag.PrintDefaults() } @@ -135,17 +146,6 @@ func (h HostInput) Address() string { return fmt.Sprintf("%s:%s", h.Host, h.Port) } -func usage() string { - return `usage: - mole [-v] [-local []:] -remote []: -server [@][:] [-key ] - mole -alias [-v] [-local []:] -remote []: -server [@][:] [-key ] - mole -alias -delete - mole -start - mole -help - mole -version - ` -} - func parseServerInput(input string) map[string]string { match := re.FindStringSubmatch(input) result := make(map[string]string) diff --git a/cli/cli_test.go b/cli/cli_test.go index 8b62ad3..5b2bab9 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -55,10 +55,22 @@ func TestValidate(t *testing.T) { args []string expected bool }{ + { + []string{"./mole"}, + false, + }, { []string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"}, true, }, + { + []string{"./mole", "-alias", "xyz", "-remote", ":443"}, + false, + }, + { + []string{"./mole", "-alias", "xyz", "-server", "example1"}, + false, + }, { []string{"./mole", "-alias", "xyz", "-server", "example1"}, false, diff --git a/cmd/mole/main.go b/cmd/mole/main.go index 110aa25..2c6db71 100644 --- a/cmd/mole/main.go +++ b/cmd/mole/main.go @@ -16,6 +16,7 @@ func main() { app := cli.New(os.Args) err := app.Parse() if err != nil { + fmt.Printf("%v\n", err) app.PrintUsage() os.Exit(1) }