-
-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A boolean
flag followed by a positional argument leads the the error Flags cannot be assigned a value.
#825
Comments
boolean
flag and a positional argument leads the the error Flags cannot be assigned a value.
boolean
flag followed by a positional argument leads the the error Flags cannot be assigned a value.
Added a small
Since this is an issue with UX, I think this is a serious one. I would really appreciate ANY feedback...?! Maybe I'm doing something wrong? |
@patriksvensson Sorry to adress you directly but I just wanted to ask, if this is the right place to report these kind of issues or am I doing something wrong? |
This seems a duplicate of #193 @patriksvensson |
@FrankRay78 You are right, sorry, I did not find this. If you are looking for a reason, why this is not working as expected, you should take a look this.
I think the tokenizer treats every argument the same way and is not aware of it's context - which is a real problem when using flags (boolean), because the tokenzier / lexer only knows, that the next argument cannot be a value, if it is aware of the type of already parsed arguments... In my opinion all these issues are related to each other. Maybe the right way to do this would be:
If I am right, currently the parser treats every argument the same without looking at the context. |
Hi @sandreas, it's interesting your comment above, and also your comment here #959 I think we are saying the same thing but I would appreciate if you could cast your eyes over my most recent comment here: #193 Also, the CommandTreeParser is a surprisingly long class with most of the methods marked as private (ie. small number of entry points combined with complex internal logic), which as you indicated elsewhere, doesn't bode well for test coverage. |
Thank you for your quick response. I did read your most recent comment - maybe I did not fully understand, because I'm not too much into the The command line parser does indeed handle boolean options (flags) the wrong way under specific circumstances, but NOT ONLY flags. Afaik it does not distinguish between FLAGS, OPTIONS, VALUES, ARGUMENTS and OPTION=VALUE combinations, depending on the context it currently has. This seems to cause the flag issue, but also to the Example - let's say you have the following Options-Class: public class TestCommandSettings: CommandSettings
{
[CommandOption("--dry-run")] public bool? DryRun { get; init; } = false;
[CommandOption("--order-by")] public string? OrderBy { get; set; }
[Description("Input files or folders")]
[CommandArgument(0, "[input]")]
public string[] Input { get; set; } = Array.Empty<string>();
} # 1. FLAG, next token MUST be OPTION, OPTION=VALUE, ARGUMENT or end of tokens
# --dry-run
cli-tester --dry-run
# 2. OPTION, next token MUST be VALUE (e.g. for OrderBy-Option)
# --order-by
cli-tester --order-by "size"
# 3. OPTION=VALUE, next token MUST be OPTION, OPTION=VALUE, ARGUMENT or end of tokens
# --order-by="size"
cli-tester --order-by="size"
# 4. ARGUMENT, next token MUST be OPTION, OPTION=VALUE, ARGUMENT or end of tokens
# "my-directory/"
cli-tester "my-directory/"
# combined example
cli-tester --order-by "-size" --dry-run "my-directory" So in my opinion the parser needs to be redesigned to:
The only real "parsing" needs to be done, when there is OPTION=VALUE, in every other case it just needs a check, if it is a valid FLAG or OPTION followed by an untouched VALUE or it can be an ARGUMENT. In short: Why "parse" I hope this was understandable... pretty long text for a pretty simple algorithm ;) |
Excellent write up and example @sandreas. I plan to fix the other issue, which is one step forward and will let me learn the parser and improve code coverage. Then come back to this issue. |
Some small additions / edge-cases:
Just a few of my other thoughts... |
@sandreas -df is already supported. |
Didn't know that. thx |
Would it help, if I compose a set of edge case parameter sets (e.g. foreach (var arg in args)
{
Console.WriteLine(arg);
} |
This issue can be closed/marked as complete @patriksvensson, now that PR #1048 has been successfully merged. |
Information
0.43.0
Describe the bug
A combination of a
boolean
flag and a positional argument leads the the errorFlags cannot be assigned a value.
, which is wrong in my opinion.To Reproduce
Use the following settings:
And call the app like this:
Expected behavior
As
--dry-run
is a flag, I would expectspectre.console
to handle the fact, taht there is a positional argument in the settings and parse this correctly. In my opinion this should work without error messages.Workaround
Putting the flag at the end works like expected:
The text was updated successfully, but these errors were encountered: