public void ArgExistsNoValue_Set_UpdatesOptionsPathWithDefault() { var argList = new List <string>(new [] { "-p", null }); var options = new TestOptions(); var info = new OptionInfo("Path", typeof(string)) { TranslatorType = typeof(DefaultTranslator <string>), DefaultValue = @"c:\temp\pkg" }; var setter = new OptionalOptionSetter <TestOptions>(argList, options); setter.Set(info, "-p"); options.Path.ShouldBe(@"c:\temp\pkg"); }
public void NullArg_Set_UpdatesOptionsPathWithDefault() { var argList = new List <string>(new [] { "-p", @"c:\temp" }); var options = new TestOptions(); var info = new OptionInfo("Path", typeof(string)) { DefaultValue = @"c:\temp\pkg" }; var setter = new OptionalOptionSetter <TestOptions>(argList, options); setter.Set(info, null); options.Path.ShouldBe(@"c:\temp\pkg"); }
/// <summary> /// Parses the args into the target class. /// </summary> /// <param name="args">Command line args</param> /// <param name="outputFormatter">Optional output formatter - none proved then the default is used</param> /// <returns>Parsing result</returns> public ParseResult <TOptions> Parse(string[] args, IOutputFormatter outputFormatter = null) { var argList = args.ToList(); var result = CheckForHelpRequest(argList, outputFormatter ?? new DefaultOutputFormatter()); if (result != default) { return(result); } var options = new TOptions(); var optionalSetter = new OptionalOptionSetter <TOptions>(argList, options); var requiredSetter = new RequiredOptionSetter <TOptions>(argList, options); foreach (var info in _options) { var arg = FindArg(argList, info); try { if (info.IsOptional) { optionalSetter.Set(info, arg); } else { requiredSetter.Set(info, arg); } } catch (Exception error) { _errors.Add($"Unexpected error occurred processing {info.Name}: {error.Message}"); } } return(HandleResult(options, outputFormatter ?? new DefaultOutputFormatter())); }