Skip to content

Commit

Permalink
chore: cli
Browse files Browse the repository at this point in the history
Standarize cli/code functions similar to: #9317

- cctx.NArg() instead of cctx.Args().xxx
- Add check for args and print help on functions that did not have it
  • Loading branch information
rjan90 committed Jan 25, 2023
1 parent 4682e8f commit 34f376e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 71 deletions.
32 changes: 26 additions & 6 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ var ChainGetBlock = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass cid of block to print")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

bcid, err := cid.Decode(cctx.Args().First())
Expand Down Expand Up @@ -198,6 +198,10 @@ var ChainReadObjCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

c, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse cid input: %s", err)
Expand Down Expand Up @@ -233,6 +237,10 @@ var ChainDeleteObjCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

c, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse cid input: %s", err)
Expand Down Expand Up @@ -276,6 +284,10 @@ var ChainStatObjCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

obj, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse cid input: %s", err)
Expand Down Expand Up @@ -308,8 +320,8 @@ var ChainGetMsgCmd = &cli.Command{
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)

if !cctx.Args().Present() {
return fmt.Errorf("must pass a cid of a message to get")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -374,6 +386,10 @@ var ChainSetHeadCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

var ts *types.TipSet

if cctx.Bool("genesis") {
Expand Down Expand Up @@ -734,6 +750,10 @@ var ChainGetCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

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

p := path.Clean(cctx.Args().First())
if strings.HasPrefix(p, "/pstate") {
p = p[len("/pstate"):]
Expand Down Expand Up @@ -1071,8 +1091,8 @@ var ChainExportCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify filename to export chain to")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

rsrs := abi.ChainEpoch(cctx.Int64("recent-stateroots"))
Expand Down
41 changes: 20 additions & 21 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ var clientDropCmd = &cli.Command{
Usage: "Remove import",
ArgsUsage: "[import ID...]",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return xerrors.Errorf("no imports specified")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -996,9 +996,8 @@ var clientFindCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
fmt.Println("Usage: find [CID]")
return nil
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

file, err := cid.Parse(cctx.Args().First())
Expand Down Expand Up @@ -1063,8 +1062,7 @@ var clientQueryRetrievalAskCmd = &cli.Command{
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)
if cctx.NArg() != 2 {
afmt.Println("Usage: retrieval-ask [minerAddress] [data CID]")
return nil
return IncorrectNumArgs(cctx)
}

maddr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -1639,8 +1637,7 @@ var clientQueryAskCmd = &cli.Command{
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)
if cctx.NArg() != 1 {
afmt.Println("Usage: query-ask [minerAddress]")
return nil
return IncorrectNumArgs(cctx)
}

maddr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -1944,8 +1941,8 @@ var clientGetDealCmd = &cli.Command{
Usage: "Print detailed deal information",
ArgsUsage: "[proposalCID]",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return cli.ShowCommandHelp(cctx, cctx.Command.Name)
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -2058,8 +2055,8 @@ var clientStat = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if !cctx.Args().Present() || cctx.NArg() != 1 {
return fmt.Errorf("must specify cid of data")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

dataCid, err := cid.Parse(cctx.Args().First())
Expand All @@ -2080,8 +2077,9 @@ var clientStat = &cli.Command{
}

var clientRestartTransfer = &cli.Command{
Name: "restart-transfer",
Usage: "Force restart a stalled data transfer",
Name: "restart-transfer",
Usage: "Force restart a stalled data transfer",
ArgsUsage: "[transferID]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "peerid",
Expand All @@ -2094,8 +2092,8 @@ var clientRestartTransfer = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return cli.ShowCommandHelp(cctx, cctx.Command.Name)
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
Expand Down Expand Up @@ -2140,8 +2138,9 @@ var clientRestartTransfer = &cli.Command{
}

var clientCancelTransfer = &cli.Command{
Name: "cancel-transfer",
Usage: "Force cancel a data transfer",
Name: "cancel-transfer",
Usage: "Force cancel a data transfer",
ArgsUsage: "[transferID]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "peerid",
Expand All @@ -2159,8 +2158,8 @@ var clientCancelTransfer = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return cli.ShowCommandHelp(cctx, cctx.Command.Name)
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cli/disputer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ var disputerMsgCmd = &cli.Command{
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 3 {
fmt.Println("Usage: dispute [minerAddress index postIndex]")
return nil
return IncorrectNumArgs(cctx)
}

ctx := ReqContext(cctx)
Expand Down
4 changes: 2 additions & 2 deletions cli/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var FetchParamCmd = &cli.Command{
Usage: "Fetch proving parameters",
ArgsUsage: "[sectorSize]",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return xerrors.Errorf("must pass sector size to fetch params for (specify as \"32GiB\", for instance)")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}
sectorSizeInt, err := units.RAMInBytes(cctx.Args().First())
if err != nil {
Expand Down
57 changes: 29 additions & 28 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ var StateMinerProvingDeadlineCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to get information for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -142,8 +142,8 @@ var StateMinerInfo = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to get information for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -397,8 +397,8 @@ var StateSectorsCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to list sectors for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

maddr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -437,8 +437,8 @@ var StateActiveSectorsCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to list sectors for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

maddr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -469,8 +469,8 @@ var StateExecTraceCmd = &cli.Command{
Usage: "Get the execution trace of a given message",
ArgsUsage: "<messageCid>",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return ShowHelp(cctx, fmt.Errorf("must pass message cid"))
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

mcid, err := cid.Decode(cctx.Args().First())
Expand Down Expand Up @@ -612,8 +612,8 @@ var StateGetDealSetCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify deal ID")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

dealid, err := strconv.ParseUint(cctx.Args().First(), 10, 64)
Expand Down Expand Up @@ -756,8 +756,8 @@ var StateGetActorCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass address of actor to get")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -810,8 +810,8 @@ var StateLookupIDCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass address of actor to get")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -854,8 +854,8 @@ var StateSectorSizeCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass miner's address")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -891,8 +891,8 @@ var StateReadStateCmd = &cli.Command{

ctx := ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass address of actor to get")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

addr, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -1473,8 +1473,8 @@ var StateWaitMsgCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must specify message cid to wait for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -1510,8 +1510,8 @@ var StateSearchMsgCmd = &cli.Command{
Usage: "Search to see whether a message has appeared on chain",
ArgsUsage: "[messageCid]",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must specify message cid to search for")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -1842,11 +1842,12 @@ var StateMarketCmd = &cli.Command{
}

var stateMarketBalanceCmd = &cli.Command{
Name: "balance",
Usage: "Get the market balance (locked and escrowed) for a given account",
Name: "balance",
Usage: "Get the market balance (locked and escrowed) for a given account",
ArgsUsage: "[address]",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return ShowHelp(cctx, fmt.Errorf("must specify address to print market balance for"))
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down
Loading

0 comments on commit 34f376e

Please sign in to comment.