A simple .NET CLI arguments parser library using C# attributes.
Tired of reading the string[] args passed to your main method yourself? This simple library lets you parse the arguments into any standard .NET object.
dotnet add package CommandLine.Parser
Here are a few examples:
Take this simple program for example:
copy.exe --source "./my-stuff.txt" --destination "./new-folder"We want the --source and --destination values.
publicclassFileCopyModel{publicstringsource{get;set;}publicstringdestination{get;set;}}publicclassProgram{publicstaticvoidMain(string[]args){CommandLineParserparser=newCommandLineParser(args);FileCopyModelcopyInfo=parser.Parse<FileCopyModel>();/* copyinfo contains: { "source": "./my-stuff.txt", "destination": "./new-folder", } */Console.Read();}}If for the same program above, we wanted to use short-hand option-name representation instead. E.g.
copy.exe -s "./my-stuff.txt" -d "./new-folder"Our FileCopyModel class would have to change a bit
usingCommandLineParser.Attributes;publicclassFileCopyModel{[Flag("s")]publicstringsource{get;set;}[Flag("d")]publicstringdestination{get;set;}}Our Program class would be exactly the same. The FlagAttribute class can be found here
It might be neccessary for some flags/options to be made compulsory so the program would not proceed unless their values are provided. Set the required boolean parameter in the Flag attribute constructor to true on the property you want to be compulsory
[Flag("s",required:true)]publicstringsource{get;set;}The full definition for FlagsAttribute is:
[Flag(shortName:"s",name:"source",required:true)]In command-line interfaces, help information is neccessary for navigating through command APIs. Even here, CommandLineParser has got your back.
[Help("Handles file-copy actions")]publicclassFileCopyModel{[Flag("s",required:true)]publicstringsource{get;set;}[Flag("d")]publicstringdestination{get;set;}}publicclassProgram{publicstaticvoidMain(string[]args){CommandLineParserparser=newCommandLineParser(args);stringhelpText=parser.GetHelpInfo<FileCopyModel>();if(!string.IsNullOrEmpty(helpText)){Console.WriteLine(helpText);}Console.Read();}}Now run in command-line:
copy.exe --helpResults:
========== Help Information ==========
Handles file-copy actions
copy.exe -s [-d]
========== End Help Information ==========Your users can query help information for a particular option. E.g.
copy.exe --source --helpResult:
--source (The original uri location of the file)[Help("Handles file-copy actions")]publicclassFileCopyModel{[Flag("s",required:true)][Help("The original uri location of the file")]publicstringsource{get;set;}[Flag("d")][Help("The uri location the file is to be replicated in")]publicstringdestination{get;set;}}- String
- Integer
- Long
- Boolean
- DateTime
- Enum
- .NET Complex Objects
When one of the flags or options has a string value or values that you intend to convert into a complex type like say AddressModel for instance, you can use the [TransformAttribute] attribute on the corresponding property.
The TransformAttribute takes a Type and string parameter. The string parameter should be the name of an existing method that takes a string parameter, converts it to and returns an object of the desired complex type.
The Type parameter should be the Type of the parent class that contains the method discussed above.
E.g.
contact.exe --add "{"name":"Mykeels","phone":8012345678,"email":"contact@example.com"}"publicclassContactSetupModel{[Flag(name:"add")][Transform(typeof(ContactSetupModel),nameof(ConvertToContact))]publicContactInsertModelContact{get;set;}publicContactInsertModelConvertToContact(stringcontactInfo){returnNewtonsoft.Json.JsonConvert.DeserializeObject<ContactInsertModel>(contactInfo);}publicclassContactInsertModel{publicstringname{get;set;}publicstringphone{get;set;}publicstringemail{get;set;}}}