/// <summary> /// Parses the arguments into a <see cref="Args"/> /// </summary> /// <param name="args">e.g. "-env:prod", "-config:prod.xml", "-date:T-1", "20"</param> /// <param name="prefix">Prefix used for named arguments. E.g. "-" as in "-env:prod"</param> /// <param name="separator">Separator used between name and value of named arguments. E.g. ":" as in "-env:prod"</param> /// <param name="argsSpec">List of expected argument items(both named and positional).</param> /// <returns></returns> public static BoolMessageItem <Args> Parse(string[] args, string prefix, string separator, List <ArgAttribute> argsSpec) { // Parse the args first. BoolMessageItem <Args> parseResult = Parse(args, prefix, separator); if (!parseResult.Success) { return(parseResult); } Args resultArgs = parseResult.Item; // 1. Set the schema. resultArgs.Schema.Items = argsSpec; // 2. Parse interpreted values like ${today}. ArgsHelper.InterpretValues(resultArgs); // 3. Validate the values. var errors = new List <string>(); if (argsSpec != null && argsSpec.Count > 0) { ArgsValidator.Validate(resultArgs, argsSpec, errors, null); } string singleMessage = string.Empty; foreach (string error in errors) { singleMessage += error + Environment.NewLine; } return(new BoolMessageItem <Args>(resultArgs, errors.Count == 0, singleMessage)); }
/// <summary> /// Parses the arguments and checks for named arguments and non-named arguments. /// </summary> /// <param name="args">e.g. "-env:prod", "-config:prod.xml", "-date:T-1", "20"</param> /// <param name="prefix">Prefix used for named arguments. E.g. "-" as in "-env:prod"</param> /// <param name="separator">Separator used between name and value of named arguments. E.g. ":" as in "-env:prod"</param> /// <param name="argReciever">The object to apply the argument values to. This must have ArgAttributes on it's properties.</param> /// <returns></returns> public static BoolMessageItem <Args> Parse(string[] args, string prefix, string separator, object argReciever) { // Parse the args first. BoolMessageItem <Args> parseResult = Parse(args, prefix, separator); if (!parseResult.Success) { return(parseResult); } Args resultArgs = parseResult.Item; // 1. Set the schema. List <ArgAttribute> schemaItems = ArgsHelper.GetArgsFromReciever(argReciever); resultArgs.Schema.Items = schemaItems; // 2. Parse interpreted values like ${today}. ArgsHelper.InterpretValues(resultArgs); // 3. Apply the values to the reciever and store any errors. var errors = new List <string>(); // Set the named argument values on the object's properties. if (argReciever != null) { ArgsHelper.CheckAndApplyArgs(resultArgs, argReciever, errors); } string singleMessage = string.Empty; foreach (string error in errors) { singleMessage += error + Environment.NewLine; } return(new BoolMessageItem <Args>(resultArgs, errors.Count == 0, singleMessage)); }