cli icon indicating copy to clipboard operation
cli copied to clipboard

What's the best practice to report an error when using a bool flag in the wrong way?

Open blockchaindevsh opened this issue 9 months ago • 11 comments

The --attack is a bool flag, but when run as:

./bin/op-challenger move -attack 1 --claim 0xffff --private-key $PK

--attack 1 makes the remaining parameters empty(e.g., --private-key will be interpreted as empty), which will cause confusion to users.

What's the best way to notify user that the 1 behind --attack is unnecessary and will cause trouble in this situation?

blockchaindevsh avatar Feb 12 '25 09:02 blockchaindevsh

Maybe you should provide more info? I, for my part, have no clue what op-challenger is and what its source code, defining parameters and commands is.

Skeeve avatar Feb 12 '25 10:02 Skeeve

The op-challenger binary is irrelevant here; It has a attack flag which is defined like this:

	AttackFlag = &cli.BoolFlag{
		Name:    "attack",
		Usage:   "An attack move. If true, the defend flag must not be set.",
	}

blockchaindevsh avatar Feb 12 '25 11:02 blockchaindevsh

That's not sufficient. You should provide the full cli definition.

OTOH: --attack is a bool flag. Correct me if I'm wrong, but bool flags do not expect any parameters. So the 1 is not a parameter to --attack but simply a command argument.

So when you check your arguments, you will have 1 as the first and --private-key as the next.

So maybe check you arguments for anything which looks like a flag?

Skeeve avatar Feb 12 '25 11:02 Skeeve

That's not sufficient. You should provide the full cli definition.

The full definition is here but as I said it's irrelevant.

OTOH: --attack is a bool flag. Correct me if I'm wrong, but bool flags do not expect any parameters. So the 1 is not a parameter to --attack but simply a command argument.

So when you check your arguments, you will have 1 as the first and --private-key as the next.

So maybe check you arguments for anything which looks like a flag?

Yeah that's correct, but my question is about how to automatically remind the user that he/she has specified --attack 1 by mistake? Or some kind of warning at least.

blockchaindevsh avatar Feb 12 '25 11:02 blockchaindevsh

The user did not specify --attack 1. The user specified --attack and gave a command line parameter, maybe a filename or a count with the string representation of 1.

So there is no automated way to achieve what you ask for. It's your task to inform the user.

Any program I would write would happily accept the 1 as whatever command line argument I expect at that position. Let's assume I'd expect a filename here, it would either open a file called 1 or complain if it couldn't.

The next parameter --private-key would then be interpreted as the next command line argument and maybe my program would then complain about that parameter not looking like the expected argument.

Just a hint: you're using v2 which is, afaik, no longer in development.

Skeeve avatar Feb 12 '25 12:02 Skeeve

I can understand there's no way to reliably detect it 100%.

But if there's a 1 after a bool flag , followed by other flags, it's very probably a mistake, I've met this kind of mistake before several times. So if cli can do some warning, it'll be very helpful.

blockchaindevsh avatar Feb 12 '25 12:02 blockchaindevsh

So if cli can do some warning, it'll be very helpful.

So go ahead, switch tocli/v3, implement such a warning and make it a Pull Request.

OTOH: I do not think this is very useful, because you will be unable to use e.g. the filename 1or 0 after a bool flag.

Skeeve avatar Feb 12 '25 12:02 Skeeve

So go ahead, switch tocli/v3, implement such a warning and make it a Pull Request.

It's a big project not owned by me:) I just met this issue while using it lol.

OTOH: I do not think this is very useful, because you will be unable to use e.g. the filename 1or 0 after a bool flag.

We can always improve the auto-detection during iterations.

blockchaindevsh avatar Feb 12 '25 12:02 blockchaindevsh

So if it's not your project, what's the reason for asking for improvements in cli/v2?

We can always improve the auto-detection during iterations.

Who is "we"? If "we" includes "you", go ahead and create a PR for this auto-detection in cli/v3. While it won't be of use for the project you're interested in, it will give you what you ask for in the next iteration of cli. If you don't do it, I doubt anyone will pick this up.

Skeeve avatar Feb 12 '25 12:02 Skeeve

So if it's not your project, what's the reason for asking for improvements in cli/v2?

Because I see it as a general issue when using cli flags.

I don't have the bandwidth to do a PR now, but may do it in the future.

blockchaindevsh avatar Feb 12 '25 13:02 blockchaindevsh

@blockchaindevsh I think this has come up in the past as well. How does cli "know" that 1 is not a positional argument for the command under question ? This is basic command line knowledge. The user needs to take a little bit of time to understand how the command in question works. Here's an example for the unix ls command where B is a bool flag.

$ ls -B -Q
"CODE_OF_CONDUCT.md"  "cli.go"           "errors.go"         "flag_float64.go"        "flag_test.go"          "help.go"            "sort.go"
"LICENSE"             "cmd"              "errors_test.go"    "flag_float64_slice.go"  "flag_timestamp.go"     "help_test.go"       "sort_test.go"
$$ ls -B 1 -Q
ls: cannot access '1': No such file or directory

dearchap avatar Mar 05 '25 13:03 dearchap