Skip to content

IgnoreUnknownArguments does not work for values #156

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

Closed
ericnewton76 opened this issue Nov 4, 2017 · 8 comments
Closed

IgnoreUnknownArguments does not work for values #156

ericnewton76 opened this issue Nov 4, 2017 · 8 comments
Labels

Comments

@ericnewton76
Copy link
Member

Issue by cdmihai
Wednesday Aug 09, 2017 at 20:14 GMT
Originally opened as gsscoder/commandline#467


Given the following snippet where ParserSettings.IgnoreUnknownArguments = false:

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 :)

@sybaris
Copy link

sybaris commented Jan 10, 2018

Hi,

I have the same issue...
It is planned to be fixed ?

Thanks for advance
Sybaris

@RedX2501
Copy link

@ericnewton76 Can you give me some pointers where the problem might be? I'm interested in seeing if I can fix this.

@RedX2501
Copy link

RedX2501 commented Mar 31, 2019

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.

@sybaris
Copy link

sybaris commented Apr 3, 2019

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; }
    }
}

@moh-hassan
Copy link
Collaborator

This is needed when you need to pass through args to third party tool like Unity Engine.

@RedX2501
Copy link

@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...

@moh-hassan
Copy link
Collaborator

moh-hassan commented May 22, 2020

@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?

@RedX2501
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants