Skip to content

Commit

Permalink
fix: remove the annoying tail message shows on every command with -h …
Browse files Browse the repository at this point in the history
…or --help (gnolang#1490)

This PR fixed the tail error message on every command with -h or --help:
`error parsing commandline arguments: flag: help requested`

Before:

![image](https://github.com/gnolang/gno/assets/25278203/8bc998d6-8d6a-45e0-aeca-7957cc15b96a)

After:

![image](https://github.com/gnolang/gno/assets/25278203/a2ab7b5a-0dd2-4bcc-a8d1-49e71e1e3d51)

---------

Co-authored-by: Hariom Verma <[email protected]>
  • Loading branch information
2 people authored and gfanton committed Jan 18, 2024
1 parent f3c4f2d commit 0ac3f97
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 60 deletions.
5 changes: 1 addition & 4 deletions contribs/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 1 addition & 6 deletions contribs/gnokeykc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions docs/getting-started/working-with-key-pairs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions gno.land/cmd/genesis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"flag"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions gno.land/cmd/gnofaucet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -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:])
}
7 changes: 1 addition & 6 deletions gno.land/cmd/gnokey/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
Expand All @@ -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:])
}
6 changes: 1 addition & 5 deletions gno.land/cmd/gnoland/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions gno.land/cmd/gnotxsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"flag"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions gnovm/cmd/gno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
! gno precompile

! stdout .+
stderr 'flag: help requested'
stderr 'USAGE'
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/testdata/gno_test/no_args.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
! gno test

! stdout .+
stderr 'flag: help requested'
stderr 'USAGE'
7 changes: 1 addition & 6 deletions misc/genproto/genproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/amino"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions misc/goscan/goscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions tm2/pkg/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0ac3f97

Please sign in to comment.