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

Subject: Parsing Command Line Arguments in .NET || Topic: Add basic arg validation #50

Closed
Felix-CodingClimber opened this issue Dec 23, 2022 · 2 comments

Comments

@Felix-CodingClimber
Copy link

Felix-CodingClimber commented Dec 23, 2022

First, thanks a lot for the great starting point on building my own very simple command line arg helper.

One very simple thing I added was an last parsing step, to remove options passed without an name (only - or --).

I changed the method to return an info about the parsing process:
public static IReadOnlyList<CommandLineOption> ParseOptions(string[] arguments, out string info) { ...

And as a very last step, I added:

// Clean up invalid options (e.g. - or -- without name)
int invalidOptions = results.RemoveAll(option => string.IsNullOrEmpty(option.Name));

info = $"Parsed <{results.Count}> valid and <{invalidOptions}> invalid command line args";
return results;
@bytefish
Copy link
Owner

Thanks for the feedback. Great, that it's useful. 👍 Probably one could also add some options like passing a ParserOptions.Strict, so it either fails with an Exception for an empty option name or just ignores empty arguments. You could also think about returning a Parse Result instead of an out Parameter like:

public class ParseResult 
{
    public string[] Arguments { get; set; }

    public IReadOnlyList<CommandLineOption> Result { get; set; }

    public int[] ValidArguments { get; set; }

    public int[] InvalidArguments { get; set; }
}

Where ValidArguments and InvalidArguments could be the indices, that are problematic.

@bytefish
Copy link
Owner

bytefish commented Dec 23, 2022

I see System.CommandLine is still in PREVIEW. Do you know by chance, what's the official recommendation by Microsoft for CLI applications?

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

2 participants