Skip to content

refactor: refactoring Narg checks #12814

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,10 @@ var SlashConsensusFault = &cli.Command{
a := srv.FullNodeAPI()
ctx := ReqContext(cctx)

if cctx.NArg() != 2 {
return IncorrectNumArgs(cctx)
}

c1, err := cid.Parse(cctx.Args().Get(0))
if err != nil {
return xerrors.Errorf("parsing cid 1: %w", err)
Expand Down
31 changes: 28 additions & 3 deletions cli/disputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,27 @@ var disputerMsgCmd = &cli.Command{
Name: "dispute",
Usage: "Send a specific DisputeWindowedPoSt message",
ArgsUsage: "[minerAddress index postIndex]",
Flags: []cli.Flag{},
Flags: []cli.Flag{
&cli.Uint64Flag{
Name: "max-fee",
Usage: "maximum fees to spend on a dispute message",
},
&cli.StringFlag{
Name: "from",
Usage: "the address to send dispute messages from if not the default wallet address",
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 3 {
return IncorrectNumArgs(cctx)
}

ctx := ReqContext(cctx)

if cctx.NArg() != 3 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand Down Expand Up @@ -137,8 +150,16 @@ var disputerStartCmd = &cli.Command{
ArgsUsage: "[minerAddress]",
Flags: []cli.Flag{
&cli.Uint64Flag{
Name: "start-epoch",
Usage: "only start disputing PoSts after this epoch ",
Name: "height",
Usage: "only start disputing PoSts after this height ",
},
&cli.Uint64Flag{
Name: "max-fee",
Usage: "maximum fees to spend on a dispute message",
},
&cli.StringFlag{
Name: "from",
Usage: "the address to send dispute messages from if not the default wallet address",
},
},
Action: func(cctx *cli.Context) error {
Expand All @@ -150,6 +171,10 @@ var disputerStartCmd = &cli.Command{

ctx := ReqContext(cctx)

if cctx.NArg() != 0 {
return IncorrectNumArgs(cctx)
}

fromAddr, err := getSender(ctx, api, cctx.String("from"))
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cli/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ var EvmDeployCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if argc := cctx.Args().Len(); argc != 1 {
return xerrors.Errorf("must pass the contract init code")
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "contract init code required")
}

contract, err := os.ReadFile(cctx.Args().First())
Expand Down Expand Up @@ -338,8 +338,8 @@ var EvmInvokeCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if argc := cctx.Args().Len(); argc != 2 {
return xerrors.Errorf("must pass the address and calldata")
if cctx.NArg() != 2 {
return IncorrectNumArgsWithHint(cctx, "address and calldata requried")
}

addr, err := address.NewFromString(cctx.Args().Get(0))
Expand Down
8 changes: 4 additions & 4 deletions cli/f3.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ var f3SubCmdCerts = &cli.Command{
f3FlagOutput,
},
Before: func(cctx *cli.Context) error {
if count := cctx.NArg(); count > 1 {
return fmt.Errorf("too many arguments: expected at most 1 but got %d", count)
if cctx.NArg() > 1 {
return IncorrectNumArgsWithHint(cctx, "expected 1 at most")
}
return nil
},
Expand Down Expand Up @@ -397,8 +397,8 @@ Examples:
f3FlagReverseOrder,
},
Before: func(cctx *cli.Context) error {
if count := cctx.NArg(); count > 1 {
return fmt.Errorf("too many arguments: expected at most 1 but got %d", count)
if cctx.NArg() > 1 {
return IncorrectNumArgsWithHint(cctx, "expected 1 at most")
}
return nil
},
Expand Down
4 changes: 4 additions & 0 deletions cli/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func IncorrectNumArgs(cctx *ufcli.Context) error {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments, got %d", cctx.NArg()))
}

func IncorrectNumArgsWithHint(cctx *ufcli.Context, hint string) error {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments, got %d, %s", cctx.NArg(), hint))
}

func RunApp(app *ufcli.App) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
Expand Down
4 changes: 2 additions & 2 deletions cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ var LogSetLevel = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("level is required")
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "level required")
}

systems := cctx.StringSlice("system")
Expand Down
4 changes: 2 additions & 2 deletions cli/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ var msigInspectCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return ShowHelp(cctx, fmt.Errorf("must specify address of multisig to inspect"))
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "specify address of multisig")
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down
31 changes: 29 additions & 2 deletions cli/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ var NetDisconnect = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() == 0 {
return IncorrectNumArgsWithHint(cctx, "must provide at least one peerID")
}

ids := cctx.Args().Slice()
for _, id := range ids {
pid, err := peer.Decode(id)
Expand Down Expand Up @@ -399,8 +403,7 @@ var NetFindPeer = &cli.Command{
ArgsUsage: "[peerId]",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
fmt.Println("Usage: findpeer [peer ID]")
return nil
return IncorrectNumArgsWithHint(cctx, "must provide a peerID")
}

pid, err := peer.Decode(cctx.Args().First())
Expand Down Expand Up @@ -585,6 +588,10 @@ var NetBlockAddPeer = &cli.Command{
Usage: "Block a peer",
ArgsUsage: "<Peer> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -611,6 +618,10 @@ var NetBlockAddIP = &cli.Command{
Usage: "Block an IP address",
ArgsUsage: "<IP> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -627,6 +638,10 @@ var NetBlockAddSubnet = &cli.Command{
Usage: "Block an IP subnet",
ArgsUsage: "<CIDR> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -653,6 +668,10 @@ var NetBlockRemovePeer = &cli.Command{
Usage: "Unblock a peer",
ArgsUsage: "<Peer> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -679,6 +698,10 @@ var NetBlockRemoveIP = &cli.Command{
Usage: "Unblock an IP address",
ArgsUsage: "<IP> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -695,6 +718,10 @@ var NetBlockRemoveSubnet = &cli.Command{
Usage: "Unblock an IP subnet",
ArgsUsage: "<CIDR> ...",
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cli/spcli/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ func ActorProposeChangeWorkerCmd(getActor ActorAddressGetter) *cli.Command {
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must pass address of new worker address")
if cctx.NArg() != 1 {
return lcli.IncorrectNumArgsWithHint(cctx, "must pass address of the new worker address")
}

api, acloser, err := lcli.GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -980,8 +980,8 @@ func ActorConfirmChangeWorkerCmd(getActor ActorAddressGetter) *cli.Command {
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must pass address of new worker address")
if cctx.NArg() != 1 {
return lcli.IncorrectNumArgsWithHint(cctx, "must pass address of new worker address")
}

api, acloser, err := lcli.GetFullNodeAPI(cctx)
Expand Down
24 changes: 12 additions & 12 deletions cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ var SyncMarkBadCmd = &cli.Command{
Usage: "Mark the given block as bad, will prevent syncing to a chain that contains it",
ArgsUsage: "[blockCid]",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "must specify block cid to mark")
}

napi, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify block cid to mark")
}

bcid, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to decode input as a cid: %s", err)
Expand All @@ -142,6 +142,10 @@ var SyncUnmarkBadCmd = &cli.Command{
},
ArgsUsage: "[blockCid]",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "must specify block cid to unmark")
}

napi, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand All @@ -153,10 +157,6 @@ var SyncUnmarkBadCmd = &cli.Command{
return napi.SyncUnmarkAllBad(ctx)
}

if !cctx.Args().Present() {
return fmt.Errorf("must specify block cid to unmark")
}

bcid, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to decode input as a cid: %s", err)
Expand All @@ -171,6 +171,10 @@ var SyncCheckBadCmd = &cli.Command{
Usage: "check if the given block was marked bad, and for what reason",
ArgsUsage: "[blockCid]",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return IncorrectNumArgsWithHint(cctx, "must specify block cid to check")
}

afmt := NewAppFmt(cctx.App)

napi, closer, err := GetFullNodeAPI(cctx)
Expand All @@ -180,10 +184,6 @@ var SyncCheckBadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify block cid to check")
}

bcid, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to decode input as a cid: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ var walletMarketAdd = &cli.Command{
afmt := NewAppFmt(cctx.App)

// Get amount param
if cctx.NArg() < 1 {
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}
f, err := types.ParseFIL(cctx.Args().First())
Expand Down