/// <summary> /// Constructor that forms the info from the argument's metadata. /// </summary> /// <param name="setAttribute">Argument set attribute.</param> /// <param name="arg">Argument metadata.</param> public ArgumentUsageInfo(ArgumentSetAttribute setAttribute, Argument arg) { Syntax = arg.GetSyntaxHelp(setAttribute); Description = arg.Attribute.HelpText; Required = arg.IsRequired; ShortName = arg.ShortName; DefaultValue = TryGetDefaultValueString(setAttribute, arg); }
/// <summary> /// Tries to construct a string describing the argument's default value. /// </summary> /// <param name="setAttribute">Metadata regarding the argument set /// containing this argument.</param> /// <param name="arg">The argument to retrieve a default value string /// from.</param> /// <returns>If one should be advertised, returns the string version of /// the default value for this argument; otherwise, returns null. /// </returns> private static string TryGetDefaultValueString(ArgumentSetAttribute setAttribute, Argument arg) { // Firstly, if the argument is required, then there's no need to // indicate any default value. if (arg.IsRequired) { return null; } // Next, go check for the actual *effective* default value for the // argument; we may still receive back a value here even if one // wasn't explicitly declared, as we will have consulted with the // argument's type to determine its default value. var defaultValue = arg.EffectiveDefaultValue; // If the default value is null, then that's not useful to show the // user. if (defaultValue == null) { return null; } // Special case: if the argument type is bool, then the argument // will be like a switch, and that's typically assumed to be false // if not present. So if the default value is indeed 'false', then // don't bother displaying it; but if it's 'true', then it's // important to indicate that. if ((defaultValue is bool) && !((bool)defaultValue)) { return null; } // Special case: if the argument type is string, then it's safe // to assume that its default value is an empty string. var stringDefaultValue = defaultValue as string; if ((stringDefaultValue != null) && string.IsNullOrEmpty(stringDefaultValue)) { return null; } // At this point, it's probably important to display the default // value. var formattedArg = arg.Format(setAttribute, defaultValue, suppressArgNames: true).ToList(); if (formattedArg.Count == 0) { return null; } return string.Join(" ", formattedArg); }
/// <summary> /// Primary constructor. /// </summary> /// <param name="loopOptions">Loop options.</param> /// <param name="argSetAttrib">Argument set attribute.</param> public HelpCommand(LoopOptions loopOptions, ArgumentSetAttribute argSetAttrib) { _loopOptions = loopOptions?.DeepClone() ?? new LoopOptions(); _parserOptions = _loopOptions.ParserOptions ?? new CommandLineParserOptions(); _argSetAttrib = argSetAttrib; }
public void InvalidNamedArgumentPrefixes() { var arg = GetArgument(typeof(StringArguments)); var attrib = new ArgumentSetAttribute { NamedArgumentPrefixes = new string[] { }, }; Action getSyntaxHelpWithBogusSeparators = () => arg.GetSyntaxHelp(attrib); getSyntaxHelpWithBogusSeparators.ShouldThrow<ArgumentOutOfRangeException>(); }
public void InvalidArgumentValueSeparators() { var arg = GetArgument(typeof(StringArguments)); var attrib = new ArgumentSetAttribute { ArgumentValueSeparators = new char[] { } }; Action getSyntaxHelpWithBogusSeparators = () => arg.GetSyntaxHelp(attrib); getSyntaxHelpWithBogusSeparators.ShouldThrow<ArgumentOutOfRangeException>(); }
public void InvalidSeparators() { var attribs = new ArgumentSetAttribute(); Action setSeparators = () => attribs.ArgumentValueSeparators = null; setSeparators.ShouldThrow<ArgumentNullException>(); }
public void InvalidPrefixes() { var attribs = new ArgumentSetAttribute(); Action setPrefixes = () => attribs.NamedArgumentPrefixes = null; setPrefixes.ShouldThrow<ArgumentNullException>(); }