/// <summary> /// Prints the usage for given type <typeparamref name="T"/> /// </summary> /// <typeparam name="T">class that mape arguments for CLI</typeparam> public static void PrintOnConsoleFor <T>(string alias) where T : ICLIArguments { // analyze the target type and build the model for usage var analyzer = new ArgumentModelAnalyzer <T>(); analyzer.Analyze(); var model = analyzer.BuildModel(); // print usage var helper = new ConsoleUsagePrinter(model, alias); helper.Print(); }
public void ParseConsoleArguments_WorksFine() { //******** GIVEN string[] arguments = new string[] { "/copy", @"--src='C:\Temp\Pluto'", "--singleOutput", "--v=WARN" }; var analyizer = new ArgumentModelAnalyzer <TestArguments>(); analyizer.Analyze(); var model = analyizer.BuildModel(); var aggregator = new CLIArgumentAggregator(model); //******** WHEN string[] parsed = aggregator.AdaptFromCLI(arguments); //******** ASSERT Assert.IsNotNull(parsed); Assert.AreEqual(arguments.Length - 1, parsed.Length); Assert.AreEqual(@"/copy --src='C:\Temp\Pluto'", parsed[0]); Assert.AreEqual(arguments[3], parsed[1]); Assert.AreEqual(arguments[2], parsed[2]); }
public void ExampleOnRootArgumentIsSuccesfullyPrinted() { //******** GIVEN var testArguments = new OnlyOptionsArguments(); var analyzer = new ArgumentModelAnalyzer <OnlyOptionsArguments>(); analyzer.Analyze(); var model = analyzer.BuildModel(); this._Printer = new StringBuilderUsagePrinter(model, "my.exe"); //******** WHEN this._Printer.Print(); //******** ASSERT var text = this._Printer.GetText(); Assert.IsNotNull(text); Assert.IsTrue(text.Contains(testArguments.Examples.ToList()[0].HelpText), $"Unable to find help text {testArguments.Examples.ToList()[0].HelpText}"); Assert.IsTrue(text.Contains(testArguments.Examples.ToList()[1].HelpText), $"Unable to find help text {testArguments.Examples.ToList()[1].HelpText}"); }
public void ExampleOnVerbAreSuccesfullyPrinted() { //******** GIVEN var testArguments = new OptionsPlusVerbArguments() { CopyWithArguments = new CopyFilesWith1MandatoryOption() }; var analyzer = new ArgumentModelAnalyzer <OptionsPlusVerbArguments>(); analyzer.Analyze(); var model = analyzer.BuildModel(); this._Printer = new StringBuilderUsagePrinter(model, "my.exe"); //******** WHEN this._Printer.Print(); //******** ASSERT var text = this._Printer.GetText(); Assert.IsNotNull(text); Assert.IsTrue(text.Contains(testArguments.CopyWithArguments.Examples.ToList()[0].HelpText), $"Unable to find help text {testArguments.CopyWithArguments.Examples.ToList()[0].HelpText}"); }
/// <summary> /// Parses the cli arguments and stores internally the proper models /// </summary> /// <param name="args">string arguments of the CLI</param> /// <exception cref="System.ArgumentException">Thrown if <paramref name="args"/> is null, empty or whitespace.</exception> public T Parse <T>(string[] args) where T : CLIArguments { string[] _Arguments = args ?? throw new ArgumentNullException(nameof(args)); // parsing if (this._ArgumentsAdapter != null) { _Arguments = this._ArgumentsAdapter.Invoke(args); } // initialize the output T parsedArguments = null; try { // analyze the target type and build the model for usage var analyzer = new ArgumentModelAnalyzer <T>(); analyzer.Analyze(); this._Model = analyzer.BuildModel(); // if needed, I aggregate options for verbs with the verb itself if (!_UseAggregatedArgumentsAsInputForVerbs) { var aggregator = new CLIArgumentAggregator(this._Model); _Arguments = aggregator.AdaptFromCLI(_Arguments); } // instance a new item parsedArguments = (T)this._ArgumentsActivator(); List <KeyValuePair <PropertyInfo, Option> > options; List <KeyValuePair <PropertyInfo, Verb> > verbs; ParseArguments(_Arguments, parsedArguments, out options, out verbs); // check mandatory fields foreach (var opt in options.Where(x => x.Value.Mandatory)) { var currentValue = opt.Key.GetValue(parsedArguments); if (currentValue == null) { throw new InvalidCLIArgumentException($"Invalid Argument: {opt.Value.LongCode} is missing", opt.Value.LongCode); } } // success if (this._OnSuccessCallback != null) { this._OnSuccessCallback(parsedArguments); } } catch (Exception ex) { //store the error this._OccurredError = ex; // callback if (this._OnErrorCallback != null) { this._OnErrorCallback(ex); } } return(parsedArguments); }