refuse more than expected positional arguments (#6154)

Co-authored-by: Davies Liu <davies@juicedata.io>
This commit is contained in:
zhijian
2025-05-29 16:14:57 +08:00
committed by GitHub
parent 7d2fafb678
commit c5ad5bb109
8 changed files with 17 additions and 9 deletions

View File

@@ -51,7 +51,7 @@ func cmdCompact() *cli.Command {
}
func compact(ctx *cli.Context) error {
setup(ctx, 1)
setup0(ctx, 1, 0)
coCnt := ctx.Int("threads")
if coCnt <= 0 {

View File

@@ -139,7 +139,7 @@ func dumpMeta(m meta.Meta, dst string, threads int, keepSecret, fast, skipTrash,
}
func dump(ctx *cli.Context) error {
setup(ctx, 1)
setup0(ctx, 1, 2)
metaUri := ctx.Args().Get(0)
var dst string
if ctx.Args().Len() > 1 {

View File

@@ -73,7 +73,7 @@ $ juicefs info -i 100`,
}
func info(ctx *cli.Context) error {
setup(ctx, 1)
setup0(ctx, 1, 0)
var recursive, strict, raw uint8
if ctx.Bool("recursive") {
recursive = 1

View File

@@ -196,7 +196,7 @@ func convert(path string, key, algo string) (string, error) {
}
func load(ctx *cli.Context) error {
setup(ctx, 1)
setup0(ctx, 1, 2)
key, algo := ctx.String("encrypt-rsa-key"), ctx.String("encrypt-algo")
src := ctx.Args().Get(1)

View File

@@ -266,10 +266,18 @@ func reorderOptions(app *cli.App, args []string) []string {
// Check number of positional arguments, set logger level and setup agent if needed
func setup(c *cli.Context, n int) {
if c.NArg() < n {
fmt.Printf("ERROR: This command requires at least %d arguments\n", n)
setup0(c, n, n)
}
func setup0(c *cli.Context, min, max int) {
if c.NArg() < min {
fmt.Printf("ERROR: This command requires at least %d arguments\n", min)
fmt.Printf("USAGE:\n juicefs %s [command options] %s\n", c.Command.Name, c.Command.ArgsUsage)
os.Exit(1)
} else if max > 0 && c.NArg() > max {
fmt.Printf("ERROR: This command accept at most %d arguments but got %+v\n", max, c.Args().Slice())
fmt.Printf("USAGE:\n juicefs %s [command options] %s\n", c.Command.Name, c.Command.ArgsUsage)
logger.Exit(1)
}
switch c.String("log-level") {

View File

@@ -42,7 +42,7 @@ $ juicefs restore redis://localhost/1 2023-05-10-01`,
}
func restore(ctx *cli.Context) error {
setup(ctx, 2)
setup0(ctx, 2, 0)
if os.Getuid() != 0 && runtime.GOOS != "windows" {
return fmt.Errorf("only root can restore files from trash")
}

View File

@@ -69,7 +69,7 @@ func openController(dpath string) (*os.File, error) {
}
func rmr(ctx *cli.Context) error {
setup(ctx, 1)
setup0(ctx, 1, 0)
var flag uint8
var numThreads int

View File

@@ -188,7 +188,7 @@ func sendCommand(cf *os.File, action vfs.CacheAction, batch []string, threads ui
}
func warmup(ctx *cli.Context) error {
setup(ctx, 0)
setup0(ctx, 1, 0)
evict, check := ctx.Bool("evict"), ctx.Bool("check")
if evict && check {