Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove the annoying tail message shows on every command with -h or --help #1490

Merged
merged 12 commits into from
Jan 14, 2024
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 @@

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
Expand All @@ -17,9 +16,5 @@
}

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:])

Check warning on line 19 in gno.land/cmd/gnokey/main.go

View check run for this annotation

Codecov / codecov/patch

gno.land/cmd/gnokey/main.go#L19

Added line #L19 was not covered by tests
}
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 @@

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
Expand All @@ -13,10 +12,7 @@
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:])

Check warning on line 15 in gno.land/cmd/gnoland/root.go

View check run for this annotation

Codecov / codecov/patch

gno.land/cmd/gnoland/root.go#L15

Added line #L15 was not covered by tests
}

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
Loading