/// <summary> /// Writes the help for a action to a textwriter /// </summary> /// <param name="action">The action to provide help</param> /// <param name="width">The width of the console</param> /// <param name="indent">The indent to use for splitted lines</param> /// <param name="tw">The textwriter to output to (defaults to <see cref="Console.Out" /></param> private static void ActionHelp([NotNull] CmdActionAttribute action, int width, int indent = 3, TextWriter tw = null) { tw = tw ?? Console.Out; action.LoadParametersAndAlias(); tw.Write(CommandlineMethods.PadCentered(action.Name, width)); if (!(action.LongDescription is null)) { foreach (string s in action.LongDescription) { CommandlineMethods.PrintWithPotentialIndent(s, width, 0, tw); } } if (action.Parameters.Count != 0) { tw.Write(CommandlineMethods.PadCentered("Parameters", width)); } foreach (CmdParameterAttribute parameter in action.Parameters) { if (parameter.Usage.HasFlag(CmdParameterUsage.SupportDeclaredRaw)) { if (parameter.Description is null) { tw.WriteLine((parameter.ShortForm is null ? "--" : "-" + parameter.ShortForm + " | --") + parameter.Name); } else { CommandlineMethods.PrintWithPotentialIndent( $"{(parameter.ShortForm is null ? "" : "-" + parameter.ShortForm + " | ")}--{parameter.Name}: {parameter.Description}", width, indent, tw); } } //TODO Differentiate directs if (parameter.Usage.HasFlag(CmdParameterUsage.SupportDirectAlias) || parameter.Usage.HasFlag(CmdParameterUsage.SupportDeclaredAlias)) { foreach (CmdParameterAliasAttribute alias in parameter.ParameterAliases) { if (alias.Description is null) { tw.WriteLine((alias.ShortForm is null ? "--" : "-" + alias.ShortForm + " | --") + alias.Name); } else { CommandlineMethods.PrintWithPotentialIndent( $"{(alias.ShortForm is null ? "" : "-" + alias.ShortForm + " | ")}--{alias.Name}: {alias.Description}", width, indent, tw); } } } } if (!(action.UsageExamples is null)) { tw.Write(CommandlineMethods.PadCentered("Examples", width)); foreach (string s in action.UsageExamples) { CommandlineMethods.PrintWithPotentialIndent(s, width, indent, tw); } } }
public ActionInterpreter(CmdActionAttribute myActionAttribute, BaseInterpreter parent, int offset = 0) : base(parent, myActionAttribute.Name, offset) => _underlyingAction = myActionAttribute;
/// <summary> /// Prints help for an action /// </summary> /// <param name="action">The action to print help for</param> /// <param name="interpreter">The interpreter to use</param> public static void PrintActionHelp(CmdActionAttribute action, BaseInterpreter interpreter) => ActionHelp(action, Console.WindowWidth, interpreter.TopInterpreter.Options.DefaultIndent);