Issue by cdmihai
_Wednesday Aug 09, 2017 at 20:14 GMT_
_Originally opened as https://github.com/gsscoder/commandline/issues/467_
Given the following snippet where ParserSettings.IgnoreUnknownArguments = false:
```c#
using System;
using CommandLine;
namespace Cmd
{
class Program
{
static void Main(string[] args)
{
var parser = new Parser(with =>
{
with.IgnoreUnknownArguments = false;
with.HelpWriter = Console.Out;
});
var result = parser.ParseArguments<Options>(args);
result.WithParsed(options => Console.WriteLine(options.anInt));
}
}
class Options {
[Option('i', HelpText = "an int")]
public int anInt { get; set; }
}
}
```
And given the invocation:
app.exe bogus args
I would expect the parser to error out saying it does not understand the unknown arguments bogus args.
Instead, it does not error out.
Context: I have an optional argument (e.g. -a value) and I had forgotten to type in the argument name (-a), I typed in just the value. Instead of having the command line parser fail on encountering value, it continued working, causing quite a bit of debugging time :)
Hi,
I have the same issue...
It is planned to be fixed ?
Thanks for advance
Sybaris
@ericnewton76 Can you give me some pointers where the problem might be? I'm interested in seeing if I can fix this.
A way around this while it is not fixed might be:
//https://github.com/commandlineparser/commandline/issues/156
// @nuget: CommandLineParser -Version 2.3.0
using System;
using CommandLine;
using System.Collections.Generic;
public class myClass {
public static void Main()
{
var p = new Parser(conf => {
conf.EnableDashDash = true;
});
var parserResult = p.ParseArguments<Options>("-n hi hiho -- --name 10 20 30".Split());
parserResult.WithParsed<Options>(options => options.Dump());
parserResult.WithNotParsed<Options>(errs => errs.Dump());
}
}
class Options
{
[Option('n', "name")]
public string Name { get; set; }
[CommandLine.Value(0)]
public string Expected {get;set;}
[CommandLine.Value(1)]
public IEnumerable<string> Unexpected { get; set; }
}
You can try it out.
Hi,
I agree with you RedX2501, what you propose can be a workaround.
To be more explicit on the bug, here an example of 3 uses cases : https://dotnetfiddle.net/lSRQmO
1st raise an error - I agree
2nd does not raise an error - I agree
3th does not raise an error - I disagree
Regards,
Sybaris
Code :
// @nuget: CommandLineParser -Version 2.3.0
using System;
using CommandLine;
namespace Cmd
{
public class Program
{
public static void UseCase(int index, bool IgnoreUnknownArguments, string args)
{
Console.WriteLine("Case "+index);
var parser = new Parser(with =>
{
with.IgnoreUnknownArguments = IgnoreUnknownArguments;
with.HelpWriter = Console.Out;
});
var result = parser.ParseArguments<Options>(args.Split());
result.WithParsed<Options>(options => options.Dump());
result.WithNotParsed<Options>(errs => errs.Dump());
}
public static void Main()
{
UseCase(1, false, "-i 1 -j");
UseCase(2, true, "-i 1 -j");
UseCase(3, true, "-i 1 abc");
}
}
class Options {
[Option('i', HelpText = "an int")]
public int anInt { get; set; }
}
}
This is needed when you need to pass through args to third party tool like Unity Engine.
@moh-hassan But then what is the point of IgnoreUnknownArguments? What you describe is a nice and valid use-case and I would expect one to need to set IgnoreUnknownArguments = true for it to be supported...
@RedX2501
Any option (start with - or --) should have a corresponding property in Option class, otherwise the parser raise an error and fail.
To stop these errors and parser success, set IgnoreUnknownArguments=1
For values that have not a corresponding property it is ignored. This ignorance is valuable in Unity engine and other third party tools when you have no control to resend these args again like #620 .
Also this is a default behaviour of most getopt tools.
If these unknown values is not ignored, you can not pass these args to Unity engine.
Also, the values after -- when EnableDashDash=true are free value options( not named options).
What do you think?
Well I'd expect to have two flags then.
AllowPositionalArguments and AllowUnknownOptions
AllowUnknownOptions (default false) is what you are describing now. Any option beginning with - or -- that is not known raises an error when the option is false. I changed the name because positive worded variables are easier to understand.
AllowUnknownPositionalArguments (default true) allows any argument not starting with - or -- to be given. When false any non parsed argument (that do not begin with - or --) end up raising an error.
Arguments after -- are unknown if for example we are only expecting 3 elements but get more than 3...
Most helpful comment
Hi,
I have the same issue...
It is planned to be fixed ?
Thanks for advance
Sybaris