diff --git a/contribs/gnodev/main.go b/contribs/gnodev/main.go index 0ee9e17ded6..ea9c3fe2451 100644 --- a/contribs/gnodev/main.go +++ b/contribs/gnodev/main.go @@ -59,10 +59,7 @@ additional specified paths.`, return execDev(cfg, args, stdio) }) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - fmt.Fprintf(os.Stderr, "%+v\n", err) - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func (c *devCfg) RegisterFlags(fs *flag.FlagSet) { fs.StringVar( diff --git a/contribs/gnokeykc/main.go b/contribs/gnokeykc/main.go index 8060f8cb1e3..9171066fb5b 100644 --- a/contribs/gnokeykc/main.go +++ b/contribs/gnokeykc/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -16,11 +15,7 @@ func main() { cmd := client.NewRootCmd(wrappedio) cmd.AddSubCommands(newKcCmd(stdio)) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } type wrappedIO struct { diff --git a/docs/getting-started/working-with-key-pairs.md b/docs/getting-started/working-with-key-pairs.md index 5617e8339eb..93a66e3fedc 100644 --- a/docs/getting-started/working-with-key-pairs.md +++ b/docs/getting-started/working-with-key-pairs.md @@ -51,8 +51,6 @@ FLAGS -insecure-password-stdin=false WARNING! take password from stdin -quiet=false suppress output during execution -remote 127.0.0.1:26657 remote node URL - -error parsing commandline arguments: flag: help requested ``` In this example, the directory where `gnokey` will store working data diff --git a/gno.land/cmd/genesis/main.go b/gno.land/cmd/genesis/main.go index 507d004e8ed..53bb48c3c64 100644 --- a/gno.land/cmd/genesis/main.go +++ b/gno.land/cmd/genesis/main.go @@ -3,7 +3,6 @@ package main import ( "context" "flag" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -13,11 +12,7 @@ func main() { io := commands.NewDefaultIO() cmd := newRootCmd(io) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func newRootCmd(io commands.IO) *commands.Command { diff --git a/gno.land/cmd/gnofaucet/main.go b/gno.land/cmd/gnofaucet/main.go index dc6c16bac78..af64672d0f5 100644 --- a/gno.land/cmd/gnofaucet/main.go +++ b/gno.land/cmd/gnofaucet/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -22,9 +21,5 @@ func main() { newServeCmd(), ) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } diff --git a/gno.land/cmd/gnokey/main.go b/gno.land/cmd/gnokey/main.go index 57a58bfee9c..1dd61f22793 100644 --- a/gno.land/cmd/gnokey/main.go +++ b/gno.land/cmd/gnokey/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/gnovm/pkg/gnoenv" @@ -17,9 +16,5 @@ func main() { } cmd := client.NewRootCmdWithBaseConfig(commands.NewDefaultIO(), baseCfg) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } diff --git a/gno.land/cmd/gnoland/root.go b/gno.land/cmd/gnoland/root.go index dfb595347c0..5b87b9452c7 100644 --- a/gno.land/cmd/gnoland/root.go +++ b/gno.land/cmd/gnoland/root.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -13,10 +12,7 @@ import ( func main() { cmd := newRootCmd(commands.NewDefaultIO()) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func newRootCmd(io commands.IO) *commands.Command { diff --git a/gno.land/cmd/gnotxsync/main.go b/gno.land/cmd/gnotxsync/main.go index cdd65f61e8a..02a613d5dc8 100644 --- a/gno.land/cmd/gnotxsync/main.go +++ b/gno.land/cmd/gnotxsync/main.go @@ -3,7 +3,6 @@ package main import ( "context" "flag" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -35,11 +34,7 @@ func main() { newExportCommand(cfg), ) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func (c *config) RegisterFlags(fs *flag.FlagSet) { diff --git a/gnovm/cmd/gno/main.go b/gnovm/cmd/gno/main.go index a2f80a9d7a4..aecb5d0f1e5 100644 --- a/gnovm/cmd/gno/main.go +++ b/gnovm/cmd/gno/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/commands" @@ -11,11 +10,7 @@ import ( func main() { cmd := newGnocliCmd(commands.NewDefaultIO()) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func newGnocliCmd(io commands.IO) *commands.Command { diff --git a/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar b/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar index 944c74ee615..239d3860e11 100644 --- a/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar +++ b/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar @@ -3,4 +3,4 @@ ! gno precompile ! stdout .+ -stderr 'flag: help requested' +stderr 'USAGE' diff --git a/gnovm/cmd/gno/testdata/gno_test/no_args.txtar b/gnovm/cmd/gno/testdata/gno_test/no_args.txtar index e69d0994fdc..bd9cd4fc965 100644 --- a/gnovm/cmd/gno/testdata/gno_test/no_args.txtar +++ b/gnovm/cmd/gno/testdata/gno_test/no_args.txtar @@ -3,4 +3,4 @@ ! gno test ! stdout .+ -stderr 'flag: help requested' +stderr 'USAGE' diff --git a/misc/genproto/genproto.go b/misc/genproto/genproto.go index fd5c4e4e2e4..b9b97efbe37 100644 --- a/misc/genproto/genproto.go +++ b/misc/genproto/genproto.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "os" "github.com/gnolang/gno/tm2/pkg/amino" @@ -38,11 +37,7 @@ func main() { execGen, ) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func execGen(_ context.Context, _ []string) error { diff --git a/misc/goscan/goscan.go b/misc/goscan/goscan.go index 38089868c35..348ada45cdf 100644 --- a/misc/goscan/goscan.go +++ b/misc/goscan/goscan.go @@ -22,11 +22,7 @@ func main() { execScan, ) - if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) - - os.Exit(1) - } + cmd.Execute(context.Background(), os.Args[1:]) } func execScan(_ context.Context, args []string) error { diff --git a/tm2/pkg/commands/command.go b/tm2/pkg/commands/command.go index e6fe36e3d6c..bc5f6f36cc5 100644 --- a/tm2/pkg/commands/command.go +++ b/tm2/pkg/commands/command.go @@ -5,6 +5,7 @@ import ( "errors" "flag" "fmt" + "os" "strings" "text/tabwriter" @@ -105,6 +106,19 @@ func (c *Command) AddSubCommands(cmds ...*Command) { } } +// Execute is a helper function for command entry. It wraps ParseAndRun and +// handles the flag.ErrHelp error, ensuring that every command with -h or +// --help won't show an error message: +// 'error parsing commandline arguments: flag: help requested' +func (c *Command) Execute(ctx context.Context, args []string) { + if err := c.ParseAndRun(ctx, args); err != nil { + if !errors.Is(err, flag.ErrHelp) { + _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err) + } + os.Exit(1) + } +} + // ParseAndRun is a helper function that calls Parse and then Run in a single // invocation. It's useful for simple command trees that don't need two-phase // setup.