Skip to content

Commit

Permalink
Merge pull request #9317 from filecoin-project/gstuart/cli-cleanup
Browse files Browse the repository at this point in the history
chore: cli: cleanup and standardize cli
  • Loading branch information
arajasek committed Sep 14, 2022
2 parents f567db6 + 4f75e20 commit 4f46ad3
Show file tree
Hide file tree
Showing 52 changed files with 402 additions and 396 deletions.
4 changes: 2 additions & 2 deletions cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ this command must be within this base path`,
},
ArgsUsage: "[backup file path]",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 1 {
return xerrors.Errorf("expected 1 argument")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

if cctx.Bool("offline") {
Expand Down
12 changes: 6 additions & 6 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,8 @@ var ChainBisectCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.Args().Len() < 4 {
return xerrors.New("need at least 4 args")
if cctx.NArg() < 4 {
return IncorrectNumArgs(cctx)
}

start, err := strconv.ParseUint(cctx.Args().Get(0), 10, 64)
Expand Down Expand Up @@ -1312,8 +1312,8 @@ var chainDecodeParamsCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
if cctx.NArg() != 3 {
return IncorrectNumArgs(cctx)
}

to, err := address.NewFromString(cctx.Args().First())
Expand Down Expand Up @@ -1391,8 +1391,8 @@ var chainEncodeParamsCmd = &cli.Command{
Action: func(cctx *cli.Context) error {
afmt := NewAppFmt(cctx.App)

if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
if cctx.NArg() != 3 {
return IncorrectNumArgs(cctx)
}

method, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
Expand Down
12 changes: 6 additions & 6 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var clientImportCmd = &cli.Command{
ctx := ReqContext(cctx)

if cctx.NArg() != 1 {
return xerrors.New("expected input path as the only arg")
return IncorrectNumArgs(cctx)
}

absPath, err := filepath.Abs(cctx.Args().First())
Expand Down Expand Up @@ -212,8 +212,8 @@ var clientCommPCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.Args().Len() != 1 {
return fmt.Errorf("usage: commP <inputPath>")
if cctx.NArg() != 1 {
return IncorrectNumArgs(cctx)
}

ret, err := api.ClientCalcCommP(ctx, cctx.Args().Get(0))
Expand Down Expand Up @@ -245,8 +245,8 @@ var clientCarGenCmd = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

if cctx.Args().Len() != 2 {
return fmt.Errorf("usage: generate-car <inputPath> <outputPath>")
if cctx.NArg() != 2 {
return IncorrectNumArgs(cctx)
}

ref := lapi.FileRef{
Expand Down Expand Up @@ -376,7 +376,7 @@ The minimum value is 518400 (6 months).`,
afmt := NewAppFmt(cctx.App)

if cctx.NArg() != 4 {
return xerrors.New(expectedArgsMsg)
return IncorrectNumArgs(cctx)
}

// [data, miner, price, dur]
Expand Down
6 changes: 3 additions & 3 deletions cli/client_retr.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ Examples:
}, retrFlagsCommon...),
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 2 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
return IncorrectNumArgs(cctx)
}

if cctx.Bool("car-export-merkle-proof") {
Expand Down Expand Up @@ -405,7 +405,7 @@ var clientRetrieveCatCmd = &cli.Command{
}, retrFlagsCommon...),
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
return IncorrectNumArgs(cctx)
}

ainfo, err := GetAPIInfo(cctx, repo.FullNode)
Expand Down Expand Up @@ -484,7 +484,7 @@ var clientRetrieveLsCmd = &cli.Command{
}, retrFlagsCommon...),
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
return IncorrectNumArgs(cctx)
}

ainfo, err := GetAPIInfo(cctx, repo.FullNode)
Expand Down
10 changes: 5 additions & 5 deletions cli/filplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ var filplusVerifyClientCmd = &cli.Command{
return err
}

if cctx.Args().Len() != 2 {
return fmt.Errorf("must specify two arguments: address and allowance")
if cctx.NArg() != 2 {
return IncorrectNumArgs(cctx)
}

target, err := address.NewFromString(cctx.Args().Get(0))
Expand Down Expand Up @@ -120,7 +120,7 @@ var filplusVerifyClientCmd = &cli.Command{
return err
}

if mwait.Receipt.ExitCode != 0 {
if mwait.Receipt.ExitCode.IsError() {
return fmt.Errorf("failed to add verified client: %d", mwait.Receipt.ExitCode)
}

Expand Down Expand Up @@ -289,8 +289,8 @@ var filplusSignRemoveDataCapProposal = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 3 {
return fmt.Errorf("must specify three arguments: notary address, client address, and allowance to remove")
if cctx.NArg() != 3 {
return IncorrectNumArgs(cctx)
}

api, closer, err := GetFullNodeAPI(cctx)
Expand Down
4 changes: 4 additions & 0 deletions cli/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func ShowHelp(cctx *ufcli.Context, err error) error {
return &PrintHelpErr{Err: err, Ctx: cctx}
}

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

func RunApp(app *ufcli.App) {
if err := app.Run(os.Args); err != nil {
if os.Getenv("LOTUS_DEV") != "" {
Expand Down
8 changes: 4 additions & 4 deletions cli/mpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ var MpoolReplaceCmd = &cli.Command{

var from address.Address
var nonce uint64
switch cctx.Args().Len() {
switch cctx.NArg() {
case 1:
mcid, err := cid.Decode(cctx.Args().First())
if err != nil {
Expand Down Expand Up @@ -610,8 +610,8 @@ var MpoolConfig = &cli.Command{
Usage: "get or set current mpool configuration",
ArgsUsage: "[new-config]",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() > 1 {
return cli.ShowCommandHelp(cctx, cctx.Command.Name)
if cctx.NArg() > 1 {
return IncorrectNumArgs(cctx)
}

afmt := NewAppFmt(cctx.App)
Expand All @@ -624,7 +624,7 @@ var MpoolConfig = &cli.Command{

ctx := ReqContext(cctx)

if cctx.Args().Len() == 0 {
if cctx.NArg() == 0 {
cfg, err := api.MpoolGetConfig(ctx)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 4f46ad3

Please sign in to comment.