fix: accept boolean flag values written with a space - #1037
Conversation
"kosli attest generic Dockerfile --compliant false" failed with "accepts at most 1 arg(s), received 2". pflag's NoOptDefVal makes a boolean flag never consume the next token, so "false" was left behind as a positional argument. The bare "--compliant" form and the space form are gated by that same single field in opposite directions, so no custom pflag.Value type can fix it, because the consume-or-not decision is made before Set() is ever called on the value. Normalize the argument slice before pflag sees it, rewriting the space form into the "=" form for boolean flags only. Both places that feed args to cobra do this: innerMain for the normal path, and getMultiOpts for the multi-host path, where the stray positional previously emptied MultiOpts and silently downgraded a multi-host call to a single host. The golden test harness normalizes too, so tests exercise the same args path as the real CLI instead of asserting the pre-fix behaviour. Deliberately left alone, each still failing loudly with the link to the boolean flags FAQ: grouped shorthands such as "-qC false", literals pflag accepts via "=" but Kosli does not document such as "TRUE" and "1", and anything following a "--" terminator. Refs kosli-dev/server#6235
|
Claude finished @JonJagger's task in 5m 4s —— View job Code Review
Static analysis
(I couldn't run the suite here — the command tests need the local Kosli server from Assessment — this is a strong PRThe core insight in the description is correct and well-explained: pflag's Things I specifically checked and am satisfied with:
The doc comments are excellent — the two accepted ambiguities (positional literally named Minor / optional notes (nothing blocking)
Nice work — the reasoning, the docs, and the test coverage are all above the bar. |
Review of #1037 spotted an input the rewrite reads differently from pflag and that nothing recorded: "--name --compliant false". pflag gives --name the value "--compliant" and leaves "false" positional, whereas the rewrite sees a space-form boolean and joins it. Left as is. It sits in the same family as the already-accepted positional named "true", and telling the two apart would mean tracking which tokens pflag consumes as values, turning a token pre-pass into a second parser. What was missing was the boundary being written down, so a later reader does not rediscover it as a bug. The doc comment now names the rewrite as positional and lists both inputs, and a test pins the behaviour the way the sibling ambiguity already is. Comment and test only, no behaviour change.
|
Looks okay, just a note - this fix works for and correctly handles global flags positioned after the subcommand, but it does not address global Perhaps we can leave it like this to avoid potentially swallowing even more |
Marko pointed out on #1037 that the space-form fix only covered flags placed after the subcommand. Written before it, "kosli --debug false list flows" printed the root help and exited 0: the command silently never ran, yet the caller was told it had succeeded. The flags in scope depend on where the token sits, so a single pass cannot cover both positions. root.Find cannot resolve the command while a leading bool flag is still in the space form, because its value is left as a stray positional that cobra reads as an unknown subcommand. Normalizing from root's own flags first is what lets Find succeed, so the second pass can still reach the flags declared on the resolved command. Refs #1037
|
Good spot. New commit pushed to fix this. |
|
Awesome, thanks @JonJagger ! We should also probably clean up flag messages and docs, e.g. |
The CLI now accepts boolean flag values written with a space, so the --compliant and --new-compliance-status help text no longer needs to send users to the FAQ to learn the --flag=value form. Pointing at the FAQ from these two flags alone was also inconsistent: none of the other ~23 boolean flags carried the link.
|
Agreed. New commit on its way |
"kosli attest generic Dockerfile --compliant false" failed with
"accepts at most 1 arg(s), received 2". pflag's NoOptDefVal makes a
boolean flag never consume the next token, so "false" was left behind
as a positional argument. The bare "--compliant" form and the space
form are gated by that same single field in opposite directions, so no
custom pflag.Value type can fix it, because the consume-or-not decision
is made before Set() is ever called on the value.
Normalize the argument slice before pflag sees it, rewriting the space
form into the "=" form for boolean flags only. Both places that feed
args to cobra do this: innerMain for the normal path, and getMultiOpts
for the multi-host path, where the stray positional previously emptied
MultiOpts and silently downgraded a multi-host call to a single host.
The golden test harness normalizes too, so tests exercise the same args
path as the real CLI instead of asserting the pre-fix behaviour.
Deliberately left alone, each still failing loudly with the link to the
boolean flags FAQ: grouped shorthands such as "-qC false", literals
pflag accepts via "=" but Kosli does not document such as "TRUE" and
"1", and anything following a "--" terminator.
Refs kosli-dev/server#6235