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

If parsing results in an error, can I make the app exit with non-zero status? #660

Closed
gbirchmeier opened this issue Jul 7, 2020 · 1 comment

Comments

@gbirchmeier
Copy link

I notice that if I leave out required options, my app shows an error message and help, but still exits with zero (non-error) status.

Is there a way to change this without overriding/re-implementing the help-text? If there's a param error, it should return an error status.

(That honestly seems like an oversight. Am I overlooking an option?)

@gbirchmeier
Copy link
Author

gbirchmeier commented Jul 7, 2020

Ah, I figured it out.

You just need to check the result.Tag to see if the command was invoked correctly, e.g.:

        static bool IsHelpOrVersion(string[] args)
        {
            string[] goodArgs = { "--help", "--version" };
            if (args.Count() == 1 && goodArgs.Contains(args[0]))
                return true;
            return false;
        }

        static void Main(string[] args)
        {
            var result = Parser.Default.ParseArguments<Options>(args)
                    .WithParsed<Options>(o =>
                    {
                        Start(o);
                    });

            if (result.Tag == ParserResultType.NotParsed)
            {
                Environment.Exit(1);
            }
        }

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

1 participant