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

refactor: refactoring Narg checks #12814

Open
wants to merge 3 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
8 changes: 8 additions & 0 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ var ChainSetHeadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

if !cctx.Bool("genesis") && !cctx.IsSet("epoch") && cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}
Expand Down Expand Up @@ -1299,6 +1303,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
8 changes: 8 additions & 0 deletions cli/disputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var disputerMsgCmd = &cli.Command{

ctx := ReqContext(cctx)

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

api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
Expand Down Expand Up @@ -150,6 +154,10 @@ var disputerStartCmd = &cli.Command{

ctx := ReqContext(cctx)

if cctx.NArg() != 1 {
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 IncorrectNumArgs(cctx)
}

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 IncorrectNumArgs(cctx)
}

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 IncorrectNumArgs(cctx)
}
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 IncorrectNumArgs(cctx)
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions cli/filplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ var filplusListAllocationsCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() > 1 {
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

Expand Down Expand Up @@ -401,7 +401,7 @@ var filplusListClaimsCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() > 1 {
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

Expand Down
4 changes: 4 additions & 0 deletions cli/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var LogSetLevel = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

if !cctx.Args().Present() {
return fmt.Errorf("level is required")
}
Expand Down
10 changes: 7 additions & 3 deletions cli/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ var msigInspectCmd = &cli.Command{

store := adt.WrapStore(ctx, cbor.NewCborStore(blockstore.NewAPIBlockstore(api)))

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

maddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
Expand Down Expand Up @@ -399,7 +403,7 @@ var msigProposeCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 3 {
if cctx.NArg() != 3 {
return ShowHelp(cctx, fmt.Errorf("must pass at least multisig address, destination, and value"))
}

Expand Down Expand Up @@ -521,7 +525,7 @@ var msigApproveCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 2 {
if cctx.NArg() != 2 {
return ShowHelp(cctx, fmt.Errorf("must pass at least multisig address and message ID"))
}

Expand Down Expand Up @@ -658,7 +662,7 @@ var msigCancelCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 2 {
if cctx.NArg() != 2 {
return ShowHelp(cctx, fmt.Errorf("must pass at least multisig address and message ID"))
}

Expand Down
40 changes: 40 additions & 0 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() != 1 {
return IncorrectNumArgs(cctx)
}

ids := cctx.Args().Slice()
for _, id := range ids {
pid, err := peer.Decode(id)
Expand Down Expand Up @@ -416,6 +420,10 @@ var NetFindPeer = &cli.Command{

ctx := ReqContext(cctx)

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

addrs, err := api.NetFindPeer(ctx, pid)

if err != nil {
Expand Down Expand Up @@ -592,6 +600,10 @@ var NetBlockAddPeer = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

var peers []peer.ID
for _, s := range cctx.Args().Slice() {
p, err := peer.Decode(s)
Expand All @@ -618,6 +630,10 @@ var NetBlockAddIP = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

return api.NetBlockAdd(ctx, atypes.NetBlockList{IPAddrs: cctx.Args().Slice()})
},
}
Expand All @@ -634,6 +650,10 @@ var NetBlockAddSubnet = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

return api.NetBlockAdd(ctx, atypes.NetBlockList{IPSubnets: cctx.Args().Slice()})
},
}
Expand All @@ -660,6 +680,10 @@ var NetBlockRemovePeer = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

var peers []peer.ID
for _, s := range cctx.Args().Slice() {
p, err := peer.Decode(s)
Expand All @@ -686,6 +710,10 @@ var NetBlockRemoveIP = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

return api.NetBlockRemove(ctx, atypes.NetBlockList{IPAddrs: cctx.Args().Slice()})
},
}
Expand All @@ -702,6 +730,10 @@ var NetBlockRemoveSubnet = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

return api.NetBlockRemove(ctx, atypes.NetBlockList{IPSubnets: cctx.Args().Slice()})
},
}
Expand Down Expand Up @@ -958,6 +990,10 @@ var NetProtectAdd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

pids, err := decodePeerIDsFromArgs(cctx)
if err != nil {
return err
Expand Down Expand Up @@ -988,6 +1024,10 @@ var NetProtectRemove = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.NArg() < 1 {
return IncorrectNumArgs(cctx)
}

pids, err := decodePeerIDsFromArgs(cctx)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions cli/spcli/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func ActorSetAddrsCmd(getActor ActorAddressGetter) *cli.Command {

ctx := lcli.ReqContext(cctx)

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

var addrs []abi.Multiaddrs
for _, a := range args {
maddr, err := ma.NewMultiaddr(a)
Expand Down Expand Up @@ -746,6 +750,10 @@ func ActorProposeChangeWorkerCmd(getActor ActorAddressGetter) *cli.Command {

ctx := lcli.ReqContext(cctx)

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

na, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
Expand Down Expand Up @@ -992,6 +1000,10 @@ func ActorConfirmChangeWorkerCmd(getActor ActorAddressGetter) *cli.Command {

ctx := lcli.ReqContext(cctx)

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

na, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cli/spcli/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ func TerminateSectorCmd(getActorAddress ActorAddressGetter) *cli.Command {
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
if cctx.NArg() != 1 {
return lcli.ShowHelp(cctx, fmt.Errorf("at least one sector must be specified"))
}

Expand Down
16 changes: 16 additions & 0 deletions cli/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ var SyncMarkBadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

if !cctx.Args().Present() {
return fmt.Errorf("must specify block cid to mark")
}
Expand Down Expand Up @@ -149,6 +153,10 @@ var SyncUnmarkBadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

if cctx.Bool("all") {
return napi.SyncUnmarkAllBad(ctx)
}
Expand Down Expand Up @@ -180,6 +188,10 @@ var SyncCheckBadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

if !cctx.Args().Present() {
return fmt.Errorf("must specify block cid to check")
}
Expand Down Expand Up @@ -222,6 +234,10 @@ var SyncCheckpointCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

var ts *types.TipSet

if cctx.IsSet("epoch") {
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
2 changes: 1 addition & 1 deletion cmd/lotus-miner/proving.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ var provingRecoverFaultsCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
if cctx.NArg() != 1 {
return lcli.ShowHelp(cctx, xerrors.Errorf("must pass at least 1 sector number"))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/lotus-miner/sealing.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ var sealingDataCidCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 || cctx.NArg() > 2 {
if cctx.NArg() != 1 || cctx.NArg() > 2 {
return lcli.ShowHelp(cctx, xerrors.Errorf("expected 1 or 2 arguments"))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/lotus-shed/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var terminateSectorPenaltyEstimationCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.NArg() < 1 {
if cctx.NArg() != 1 {
return lcli.ShowHelp(cctx, fmt.Errorf("at least one sector must be specified"))
}

Expand Down
Loading