Skip to content

Commit c5ad5bb

Browse files
zhijian-prodavies
andauthored
refuse more than expected positional arguments (#6154)
Co-authored-by: Davies Liu <[email protected]>
1 parent 7d2fafb commit c5ad5bb

File tree

8 files changed

+17
-9
lines changed

8 files changed

+17
-9
lines changed

cmd/compact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func cmdCompact() *cli.Command {
5151
}
5252

5353
func compact(ctx *cli.Context) error {
54-
setup(ctx, 1)
54+
setup0(ctx, 1, 0)
5555

5656
coCnt := ctx.Int("threads")
5757
if coCnt <= 0 {

cmd/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func dumpMeta(m meta.Meta, dst string, threads int, keepSecret, fast, skipTrash,
139139
}
140140

141141
func dump(ctx *cli.Context) error {
142-
setup(ctx, 1)
142+
setup0(ctx, 1, 2)
143143
metaUri := ctx.Args().Get(0)
144144
var dst string
145145
if ctx.Args().Len() > 1 {

cmd/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ $ juicefs info -i 100`,
7373
}
7474

7575
func info(ctx *cli.Context) error {
76-
setup(ctx, 1)
76+
setup0(ctx, 1, 0)
7777
var recursive, strict, raw uint8
7878
if ctx.Bool("recursive") {
7979
recursive = 1

cmd/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func convert(path string, key, algo string) (string, error) {
196196
}
197197

198198
func load(ctx *cli.Context) error {
199-
setup(ctx, 1)
199+
setup0(ctx, 1, 2)
200200

201201
key, algo := ctx.String("encrypt-rsa-key"), ctx.String("encrypt-algo")
202202
src := ctx.Args().Get(1)

cmd/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,18 @@ func reorderOptions(app *cli.App, args []string) []string {
266266

267267
// Check number of positional arguments, set logger level and setup agent if needed
268268
func setup(c *cli.Context, n int) {
269-
if c.NArg() < n {
270-
fmt.Printf("ERROR: This command requires at least %d arguments\n", n)
269+
setup0(c, n, n)
270+
}
271+
272+
func setup0(c *cli.Context, min, max int) {
273+
if c.NArg() < min {
274+
fmt.Printf("ERROR: This command requires at least %d arguments\n", min)
271275
fmt.Printf("USAGE:\n juicefs %s [command options] %s\n", c.Command.Name, c.Command.ArgsUsage)
272276
os.Exit(1)
277+
} else if max > 0 && c.NArg() > max {
278+
fmt.Printf("ERROR: This command accept at most %d arguments but got %+v\n", max, c.Args().Slice())
279+
fmt.Printf("USAGE:\n juicefs %s [command options] %s\n", c.Command.Name, c.Command.ArgsUsage)
280+
logger.Exit(1)
273281
}
274282

275283
switch c.String("log-level") {

cmd/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $ juicefs restore redis://localhost/1 2023-05-10-01`,
4242
}
4343

4444
func restore(ctx *cli.Context) error {
45-
setup(ctx, 2)
45+
setup0(ctx, 2, 0)
4646
if os.Getuid() != 0 && runtime.GOOS != "windows" {
4747
return fmt.Errorf("only root can restore files from trash")
4848
}

cmd/rmr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func openController(dpath string) (*os.File, error) {
6969
}
7070

7171
func rmr(ctx *cli.Context) error {
72-
setup(ctx, 1)
72+
setup0(ctx, 1, 0)
7373
var flag uint8
7474
var numThreads int
7575

cmd/warmup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func sendCommand(cf *os.File, action vfs.CacheAction, batch []string, threads ui
188188
}
189189

190190
func warmup(ctx *cli.Context) error {
191-
setup(ctx, 0)
191+
setup0(ctx, 1, 0)
192192

193193
evict, check := ctx.Bool("evict"), ctx.Bool("check")
194194
if evict && check {

0 commit comments

Comments
 (0)