private string GetPropertyUsage(CmdLineProperty prop) { object id; if (prop.Aliases.Length == 0) { id = prop.Name; } else { id = prop.Aliases[0]; } if (!string.IsNullOrEmpty(prop.Usage)) { return(string.Format("{0}{1} <{2}>", Options.ArgumentPrefix, id, prop.Usage)); } if (prop.PropertyType == typeof(bool)) { return(string.Format("{0}{1}[-]", Options.ArgumentPrefix, id)); } return(string.Format("{0}{1}{2}<{3}>", Options.ArgumentPrefix, id, Options.AssignmentDelimiter, prop.Name)); }
public void InitializeFromQueryString(string queryStr) { InitializeBy(() => { Initialize_Internal(); if (Properties.Count == 0) { return; } // the query string is empty so just return. if (string.IsNullOrEmpty(queryStr)) { return; } var ps = new UrlParamList(queryStr); foreach (UrlParam p in ps) { CmdLineProperty prop = Properties[p.Name]; if (prop == null) { continue; } prop.Value = p.Value; } }); }
/// <summary> /// Gets the usage display name for the property. /// </summary> /// <param name="prop"></param> /// <returns></returns> protected string GetUsageName(CmdLineProperty prop) { if (prop.Aliases.Length == 0) { return(prop.Name); } return(prop.Aliases[0]); }
/// <summary> /// Gets the display value for the usage type. /// </summary> /// <param name="prop"></param> /// <returns></returns> protected string GetPropertyTypeDisplay(CmdLineProperty prop) { if (prop.PropertyType.IsEnum) { var enumVals = string.Join("|", Enum.GetNames(prop.PropertyType)); return(enumVals); } else { return(prop.PropertyType.Name); } }
public bool RestoreFromXml(string path) { string xmlStr = GetXml(path); // No point in throwing an exception, just return; if (string.IsNullOrEmpty(xmlStr)) { return(false); } if (Properties == null) { Initialize_Internal(); } var xml = new XmlDocument(); xml.LoadXml(xmlStr); XmlNode propsNode = xml.SelectSingleNode("CmdLineObject/Properties"); foreach (XmlNode propNode in propsNode.ChildNodes) { CmdLineProperty prop = Properties[propNode.Name]; if (prop == null) { continue; } if (prop.PropertyType.IsArray) { var vals = new List <object>(); Type elementType = prop.PropertyType.GetElementType(); foreach (XmlNode elementNode in propNode.ChildNodes) { vals.Add(ConvertEx.ChangeType(elementNode.InnerXml, elementType)); } Array arr = Array.CreateInstance(elementType, vals.Count); for (int i = 0; i < vals.Count; i++) { arr.SetValue(vals[i], i); } prop.Value = arr; } else { prop.Value = ConvertEx.ChangeType(propNode.InnerXml, prop.PropertyType); } } return(true); }
private void EndInit() { if (!Options.WaitArgName.IsEmpty()) { CmdLineProperty wait = Properties[Options.WaitArgName]; if (wait == null) { throw new CmdLineException("The Wait property '{0}' was not found.".Fmt(Options.WaitArgName)); } if (wait.PropertyType != typeof(bool)) { throw new CmdLineException("The Wait property must be a boolean."); } Options.Wait = (bool)wait.Value; } IsInitialized = true; Initialized(); }
public void InitializeFromCmdLine(params string[] args) { args = SplitArgsByDelimiter(args); InitializeBy(() => { Initialize_Internal(); if (Properties.Count == 0) { return; } // There aren't any args so just return. if (args.Length == 0) { return; } int startIndex = 0; if (DefaultProperties != null && DefaultProperties.Length > 0 && !args[0].StartsWith(Options.ArgumentPrefix)) { string[] argValues = GetArgValues(args); if (DefaultProperties.Length == 1) { DefaultProperties[0].Value = argValues; } else { for (int i = 0; i < argValues.Length && i < DefaultProperties.Length; i++) { DefaultProperties[i].Value = argValues[i]; } } startIndex = argValues.Length; // We already took care of the first set of arguments so start with the next. } for (int i = startIndex; i < args.Length; i++) { if (!args[i].StartsWith(Options.ArgumentPrefix)) { continue; } string argName = args[i].Substring(Options.ArgumentPrefix.Length); if (argName == "") { continue; } CmdLineProperty prop = Properties[argName.TrimEnd('-')]; if (prop == null) { continue; } string[] argValues; argValues = GetArgValues(args.Shrink(i + 1)); i = i + argValues.Length; // Move to the next valid argument. if (prop.PropertyType == typeof(bool)) { // Boolean arguments don't require an argument value (/A and /A- can be used for true/false). if (argName.EndsWith("-")) { prop.Value = false; } else { bool bVal = true; if (argValues.Length > 0) { bVal = ConvertEx.ToBoolean(argValues[0]); try { bVal = ConvertEx.ToBoolean(argValues[0]); } catch (Exception) { // The command-line argument value couldn't be converted to a // bool so assume it wasn't intended to be. } } prop.Value = bVal; } } else if (prop.PropertyType.IsArray) { try { var splitValues = argValues.Join(Options.ArraySeparator).Split(new[] { Options.ArraySeparator }, StringSplitOptions.RemoveEmptyEntries); prop.Value = splitValues.Convert(prop.PropertyType.GetElementType()); } catch (Exception) { prop.Value = argValues; } } else if (argValues.Length > 0) { prop.Value = argValues; } } }); }
/// <summary> /// Gets the help text for a single property. /// </summary> /// <param name="prop"></param> /// <returns></returns> public virtual string GetPropertyHelp(CmdLineProperty prop) { var sb = new StringBuilder(); sb.AppendFormat("{0}{1}", ParseResults.Options.ArgumentPrefix, prop.Name); if (prop.Aliases.Length > 0) { var aliases = string.Join(" | " + ParseResults.Options.ArgumentPrefix, prop.Aliases); sb.AppendFormat(" ({0}{1})", ParseResults.Options.ArgumentPrefix, aliases); } sb.AppendFormat(" <{0}>", GetPropertyTypeDisplay(prop)); if (prop.Required) { sb.Append(" REQUIRED"); } if (prop.Description.HasValue()) { sb.AppendFormat("\n\t{0}", prop.Description); } if (prop.ShowDefaultValue) { object dflt = prop.DefaultValue; if (!ConvertEx.IsEmpty(dflt) || prop.PropertyType.IsEnum) { if (dflt is Array arr) { var strs = arr.Convert <string>(); if (dflt.GetType().GetElementType() == typeof(string)) { dflt = "[\"{0}\"]".Fmt(strs.Join("\", \"")); } else { dflt = "[{0}]".Fmt(strs.Join(", ")); } } sb.AppendFormat("\n\tDefault: {0}", dflt); } } foreach (var att in prop.Property.Attributes) { var validator = att as ValidationAttribute; if (validator == null) { continue; } // The RequiredAttribute is handled differently. if (validator.GetType() == typeof(RequiredAttribute)) { continue; } string message = validator.FormatErrorMessage(prop.Name); sb.AppendFormat("\n\t{0}", message); } return(sb.ToString()); }