/// <summary> /// Parses the provided args collection and uses its values to set the appropriate properties on the parsable object. /// </summary> /// <param name="parsable"></param> /// <param name="args"></param> /// <returns></returns> public static CliParseResult Parse(Parsable parsable, IEnumerable<string> args) { if (args == null) throw new CliParseException("Parameter 'args' cannot be null."); if (parsable == null) throw new CliParseException("Parameter 'parsable' cannot be null."); var result = new CliParseResult(); try { // single enumeration. var argumentList = args as IList<string> ?? args.ToList(); parsable.PreParse(argumentList, result); if (result.Successful == false || result.ShowHelp) return result; result = MapArguments(parsable, argumentList); parsable.PostParse(argumentList, result); } catch (CliParseException ex) { result.AddMessageFromException(ex); } return result; }
public override void PreParse(IEnumerable<string> args, CliParseResult result) { base.PreParse(args, result); Path = Environment.CurrentDirectory; }
/// <summary> /// Executes after parsing has been performed. /// </summary> /// <param name="args"></param> /// <param name="result"></param> public virtual void PostParse(IEnumerable<string> args, CliParseResult result) { }
private static CliParseResult MapArguments(Parsable parsable, IEnumerable<string> args) { var result = new CliParseResult(); var parsableClass = Helper.GetObjectAttribute(parsable, typeof(ParsableClassAttribute)) as ParsableClassAttribute; var allowedPrefixes = GetParsableClassAllowedPrefixs(parsableClass); var ignoreUnknowns = parsableClass != null && parsableClass.IgnoreUnknowns; var tokens = Tokenizer.Tokenize(args, allowedPrefixes).ToList(); result.ShowHelp = tokens.Any(token=>IsHelpToken(token, parsable)); if (tokens.Count == 0) { if (parsableClass == null || parsableClass.ShowHelpWhenNoArgumentsProvided) result.ShowHelp = true; } var parsableType = parsable.GetType(); List<PropertyInfo> unsetProperties = parsableType.GetProperties().ToList(); List<PropertyInfo> tmpSetProperties = new List<PropertyInfo>(); // find by names foreach (var prop in unsetProperties) { foreach (var argument in prop.GetCustomAttributes(true).OfType<ParsableArgumentAttribute>()) { // find by name var token = GetTokenForArgumentByName(tokens, argument); var propertySet = false; if(token != null) propertySet = SetPropertyValue(parsable, token, tokens, argument, prop); if (!propertySet) { // find by position token = GetTokenForArgumentByPosition(tokens, argument); propertySet = SetPropertyValue(parsable, token, tokens, argument, prop); } // flag property as set and remove later. if (propertySet) { tmpSetProperties.Add(prop); } } } tmpSetProperties.ForEach(x => unsetProperties.Remove(x)); foreach (var unsetProperty in unsetProperties) { foreach (var argument in unsetProperty.GetCustomAttributes(true).OfType<ParsableArgumentAttribute>()) { if(argument.Required) result.AddErrorMessage(string.Format(CultureInfo.CurrentCulture,"Required argument '{0}' was not supplied.", argument.Name)); } } // unknown/unused aruments if (!result.ShowHelp && !ignoreUnknowns) { tokens.Where(x => !x.Taken) .ToList() .ForEach( x => result.AddErrorMessage(string.Format(CultureInfo.CurrentCulture, "Unknown argument '{0}' was supplied.", x.Value.ToString()))); } return result; }