public static ConsoleString GetStyledUsage(CommandLineArgumentsDefinition definition, string exeName = null, ArgUsageOptions options = null) { if (ArgHook.HookContext.Current != null && ArgHook.HookContext.Current.Definition == definition) { ArgHook.HookContext.Current.RunBeforePrepareUsage(); } options = options ?? new ArgUsageOptions(); if (exeName == null) { var assembly = Assembly.GetEntryAssembly(); if (assembly == null) { throw new InvalidOperationException("PowerArgs could not determine the name of your executable automatically. This may happen if you run GetUsage<T>() from within unit tests. Use GetUsageT>(string exeName) in unit tests to avoid this exception."); } exeName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); } ConsoleString ret = new ConsoleString(); ret += new ConsoleString("Usage: " + exeName, ConsoleColor.Cyan); if (definition.Actions.Count > 0) { string actionText = options.SpecifiedActionOverride == null ? "<action>" : options.SpecifiedActionOverride.DefaultAlias; ret = ret.AppendUsingCurrentFormat(" " + actionText + " options\n"); foreach (var example in definition.Examples) { ret += new ConsoleString("\nEXAMPLE: " + example.Example + "\n" + example.Description + "\n\n", ConsoleColor.DarkGreen); } if (definition.Arguments.Count > 0) { var global = GetOptionsUsage(definition.Arguments, true, options); if (string.IsNullOrEmpty(global.ToString()) == false) { ret += new ConsoleString("\nGlobal options:\n\n", ConsoleColor.Cyan) + global + "\n"; } } var specifiedAction = definition.SpecifiedAction; if (options.SpecifiedActionOverride != null) { specifiedAction = options.SpecifiedActionOverride; if (definition.Actions.Contains(specifiedAction) == false) { throw new InvalidArgDefinitionException("There is no action that matches '" + options.SpecifiedActionOverride + "'"); } } if (specifiedAction == null) { ret += new ConsoleString("Actions:", ConsoleColor.Cyan); } foreach (var action in definition.Actions) { if (specifiedAction != null && action.Equals(specifiedAction) == false) { // The user specified an action so only show the usage for that action continue; } if (string.IsNullOrWhiteSpace(action.Description) == false) { ret += "\n" + action.DefaultAlias + " - " + action.Description + "\n\n"; } else { ret += "\n" + action.DefaultAlias + "\n\n"; } foreach (var example in action.Examples) { ret += new ConsoleString() + " EXAMPLE: " + new ConsoleString(example.Example + "\n", ConsoleColor.Green) + new ConsoleString(" " + example.Description + "\n\n", ConsoleColor.DarkGreen); } ret += GetOptionsUsage(action.Arguments, false, options); } } else { ret = ret.AppendUsingCurrentFormat(" options\n\n"); ret += GetOptionsUsage(definition.Arguments, false, options); ret += "\n"; foreach (var example in definition.Examples) { ret += new ConsoleString() + " EXAMPLE: " + new ConsoleString(example.Example + "\n", ConsoleColor.Green) + new ConsoleString(" " + example.Description + "\n\n", ConsoleColor.DarkGreen); } } return(ret); }
/// <summary> /// Generates color styled usage documentation for the given argument scaffold type. /// </summary> /// <typeparam name="T">Your custom argument scaffold type</typeparam> /// <param name="exeName">The name of your program or null if you want PowerArgs to automatically detect it.</param> /// <param name="options">Specify custom usage options</param> /// <returns></returns> public static ConsoleString GetStyledUsage <T>(string exeName = null, ArgUsageOptions options = null) { options = options ?? new ArgUsageOptions(); if (exeName == null) { var assembly = Assembly.GetEntryAssembly(); if (assembly == null) { throw new ArgException("PowerArgs could not determine the name of your executable automatically. This may happen if you run GetUsage<T>() from within unit tests. Use GetUsageT>(string exeName) in unit tests to avoid this exception."); } exeName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); } ConsoleString ret = new ConsoleString(); ret += new ConsoleString("Usage: " + exeName, ConsoleColor.Cyan); var actionProperty = ArgAction.GetActionProperty <T>(); if (actionProperty != null) { ret.AppendUsingCurrentFormat(" <action> options\n\n"); foreach (var example in typeof(T).Attrs <ArgExample>()) { ret += new ConsoleString("EXAMPLE: " + example.Example + "\n" + example.Description + "\n\n", ConsoleColor.DarkGreen); } var global = GetOptionsUsage(typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public), true, options); if (string.IsNullOrEmpty(global.ToString()) == false) { ret += new ConsoleString("Global options:\n\n", ConsoleColor.Cyan) + global + "\n"; } ret += "Actions:"; foreach (PropertyInfo prop in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (prop.IsActionArgProperty() == false) { continue; } var actionDescription = prop.HasAttr <ArgDescription>() ? " - " + prop.Attr <ArgDescription>().Description : ""; ret += "\n\n" + prop.GetArgumentName().Substring(0, prop.GetArgumentName().Length - Constants.ActionArgConventionSuffix.Length) + actionDescription + "\n\n"; foreach (var example in prop.Attrs <ArgExample>()) { ret += new ConsoleString() + " EXAMPLE: " + new ConsoleString(example.Example + "\n", ConsoleColor.Green) + new ConsoleString(" " + example.Description + "\n\n", ConsoleColor.DarkGreen); } ret += GetOptionsUsage(prop.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public), false, options); } } else { ret.AppendUsingCurrentFormat(" options\n\n"); ret += GetOptionsUsage(typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public), false, options); ret += "\n"; foreach (var example in typeof(T).Attrs <ArgExample>()) { ret += new ConsoleString() + " EXAMPLE: " + new ConsoleString(example.Example + "\n", ConsoleColor.Green) + new ConsoleString(" " + example.Description + "\n\n", ConsoleColor.DarkGreen); } } return(ret); }