Skip to content
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

Can I add validate feature in Set parameter, and throw exception, and show usage? #283

Open
Wenduan-Mou opened this issue Apr 27, 2018 · 3 comments

Comments

@Wenduan-Mou
Copy link

Can I add validate feature in Set parameter, and throw exception, and show usage?
is there exception handling in command line parameter parsing?

[Option(
'f', "filename",
HelpDesk = "Specifies the input file")]
public string Name
{
get { return mName; }
set
{
Regex regex = new Regex("^[0-9a-z]+.txt$");

     if (regex.Match(value).Success )
        throw new InvalidOptionValueException(
            "The name must be txt file", false);
     mName = value;
  }

}
Private string mName;

@e673
Copy link
Contributor

e673 commented May 5, 2018

No, there is no such exception handling.
Try this code:

try
{
    var result = Parser.Default.ParseArguments(...);
}
catch (InvalidOperationException e)
{
    if (e.InnerException is TargetInvocationException targetInvocationException)
    {
        var innerException = targetInvocationException.InnerException;
        Console.Error.WriteLine(innerException.Message);
    }
}

P.S. I will try to implement this feature now.

e673 added a commit to e673/commandline that referenced this issue May 5, 2018
…s parsing errors

ToDo: add tests and nameinfo
e673 added a commit to e673/commandline that referenced this issue May 5, 2018
@e673
Copy link
Contributor

e673 commented May 5, 2018

I've implementing it here:
https://github.com/e673/commandline/tree/issue283
I will make a pull request after adding tests.

@mmajcica
Copy link

I just tried this and it works as expected! Thanks

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

No branches or pull requests

3 participants