public void ConstructorCanTakeString()
        {
            var s = new ColoredMultistring("xyz");

            s.Content.Should().HaveCount(1);
            s.Content[0].Content.Should().Be("xyz");
        }
        public void ConstructorCanTakeString()
        {
            var s = new ColoredMultistring("xyz");

            s.Content.Should().ContainSingle();
            s.Content[0].Content.Should().Be("xyz");
        }
        public void TestAppendingMultistringAddsItsPieces()
        {
            var anyColoredStrings = anyArrayOfMultipleColoredStrings;
            var anyMultistring    = new ColoredMultistring(anyColoredStrings);

            var builder = new ColoredMultistringBuilder();

            builder.Append(anyMultistring);

            builder.ShouldProduce(anyMultistring.Content);
        }
        public void TestThatExplicitConversionOperatorsPreserveNull()
        {
            string nullStr = null;

            ColoredMultistring cms = (ColoredMultistring)nullStr;

            cms.Should().BeNull();

            string unwrapped = (string)cms;

            unwrapped.Should().BeNull();
        }
        /// <summary>
        /// Parses command-line arguments into a reference-type object.
        /// Displays usage message if invalid arguments are encountered.
        /// </summary>
        /// <typeparam name="T">Type of the parsed arguments object.</typeparam>
        /// <param name="arguments">The actual arguments.</param>
        /// <param name="destination">The resulting parsed arguments.</param>
        /// <param name="options">Optionally provides additional options
        /// controlling how parsing proceeds.</param>
        /// <param name="usageInfoOptions">Options for how to display usage
        /// information, in case it's presented.</param>
        /// <returns>True if no errors were detected.</returns>
        public static bool ParseWithUsage <T>(IEnumerable <string> arguments, T destination, CommandLineParserOptions options, UsageInfoOptions usageInfoOptions) where T : class
        {
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (options == null)
            {
                options = new CommandLineParserOptions {
                    Reporter = DefaultReporter
                };
            }

            Debug.Assert(arguments.All(a => a != null));

            // Check if the object inherits from HelpArgumentsBase.
            var helpDestination = destination as HelpArgumentsBase;

            // Parse!
            var engine = new CommandLineParserEngine(destination.GetType(), destination, options);

            if (!engine.Parse(arguments, destination))
            {
                // An error was encountered in arguments. Display the usage
                // message.
                options.Reporter?.Invoke(ColoredMultistring.FromString(Environment.NewLine));
                options.Reporter?.Invoke(GetUsageInfo(engine, null, null, usageInfoOptions, destination));

                return(false);
            }

            // We parsed the arguments, but check if we were requested to
            // display the usage help message anyway.
            if ((helpDestination != null) && helpDestination.Help)
            {
                options.Reporter?.Invoke(GetUsageInfo(engine, null, null, usageInfoOptions, destination));
                return(false);
            }

            return(true);
        }
        public void TestAppendingMultipleMultistrings()
        {
            var anyColoredStrings = anyArrayOfMultipleColoredStrings;
            var anyMultistring    = new ColoredMultistring(anyColoredStrings);

            var anyOtherArrayOfMultipleColoredStrings = new[]
            {
                new ColoredString("Another", ConsoleColor.Cyan),
                new ColoredString("string", ConsoleColor.Gray)
            };

            var anyOtherMultistring = new ColoredMultistring(anyOtherArrayOfMultipleColoredStrings);

            var builder = new ColoredMultistringBuilder();

            builder.Append(new[] { anyMultistring, anyOtherMultistring });

            builder.ShouldProduce(anyColoredStrings.Concat(anyOtherMultistring.Content));
        }
        public void FromStringThrowsOnNull()
        {
            Action action = () => ColoredMultistring.FromString(null);

            action.ShouldThrow <ArgumentNullException>();
        }
        private IEnumerable <ColoredMultistring> FormatParameterInfo(
            ArgumentUsageInfo info,
            ArgumentSetUsageInfo setInfo,
            List <IEnumArgumentType> inlineDocumentedEnums)
        {
            var builder = new ColoredMultistringBuilder();

            var syntax = SimplifyParameterSyntax(info.DetailedSyntax);

            builder.Append(new ColoredString(syntax, ParameterNameForegroundColor));

            if (!string.IsNullOrEmpty(info.Description))
            {
                builder.Append(" - ");
                builder.Append(info.Description);
            }

            var metadataItems = new List <List <ColoredString> >();

            if (Options.HasFlag(UsageInfoOptions.IncludeParameterDefaultValues) &&
                !string.IsNullOrEmpty(info.DefaultValue))
            {
                metadataItems.Add(new List <ColoredString>
                {
                    new ColoredString(Strings.UsageInfoDefaultValue, ParameterMetadataForegroundColor),
                    " ",
                    info.DefaultValue
                });
            }

            // Append parameter's short name (if it has one).
            if (Options.HasFlag(UsageInfoOptions.IncludeParameterShortNameAliases) &&
                !string.IsNullOrEmpty(info.ShortName) &&
                !string.IsNullOrEmpty(setInfo.DefaultShortNamePrefix))
            {
                metadataItems.Add(new List <ColoredString>
                {
                    new ColoredString(Strings.UsageInfoShortForm, ParameterMetadataForegroundColor),
                    " ",
                    new ColoredString(setInfo.DefaultShortNamePrefix + info.ShortName, NameForegroundColor)
                });
            }

            if (metadataItems.Count > 0)
            {
                builder.Append(" [");
                builder.Append(metadataItems.InsertBetween(new List <ColoredString> {
                    ", "
                }).Flatten());
                builder.Append("]");
            }

            var formattedInfo = new List <ColoredMultistring> {
                builder.ToMultistring()
            };

            if (inlineDocumentedEnums?.Count > 0)
            {
                foreach (var entry in inlineDocumentedEnums.SelectMany(GetEnumValueEntries))
                {
                    var indentedEntry = new ColoredMultistring(new ColoredString[]
                    {
                        new string(' ', Section.DefaultIndent)
                    }.Concat(entry.Content));
                    formattedInfo.Add(indentedEntry);
                }
            }

            return(formattedInfo);
        }
Exemple #9
0
 public Section(ArgumentSetHelpSectionType type, ArgumentSetHelpOptions options, ArgumentMetadataHelpOptions itemOptions, ColoredMultistring entry)
     : this(type, options, itemOptions, new[] { entry })
 {
 }
Exemple #10
0
 public Section(string name, ColoredMultistring entry) : this(name, new[] { entry })
 {
 }
Exemple #11
0
        /// <summary>
        /// Tries to parse the given string arguments into the provided instance of <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">Type of the destination object.</typeparam>
        /// <param name="argSet">Definition of the argument set to be parsing.</param>
        /// <param name="arguments">The string arguments to parse.</param>
        /// <param name="options">Options describing how to parse.</param>
        /// <param name="destination">The object to parse into.</param>
        /// <returns>True on success; false otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="arguments"/> or
        /// <paramref name="destination" /> is null.</exception>
        internal static bool TryParse <T>(ArgumentSetDefinition argSet, IEnumerable <string> arguments, CommandLineParserOptions options, T destination)
        {
            if (options == null)
            {
                options = new CommandLineParserOptions();
            }

            //
            // Buffer output to the reporter; suppress it if we find afterwards
            // that the user just wanted to see help information.
            //

            var reportedLines  = new List <ColoredMultistring>();
            var actualReporter = options.Reporter;

            options          = options.DeepClone();
            options.Reporter = s => reportedLines.Add(s);

            //
            // Parse!
            //

            var parser = new ArgumentSetParser(argSet, options);

            var parseResult = parser.ParseArgumentList(arguments, destination).IsReady;

            var parserArgSet = parser.ArgumentSet;

            //
            // See if the user requested help output; if so, then suppress any errors.
            //

            if ((destination is IArgumentSetWithHelp helpArgs) && helpArgs.Help && actualReporter != null)
            {
                actualReporter(GetUsageInfo(parserArgSet, options.HelpOptions, destination));
                return(false);
            }

            //
            // Okay, now flush any reported output.
            //

            if (actualReporter != null)
            {
                foreach (var line in reportedLines)
                {
                    actualReporter.Invoke(line);
                }
            }

            //
            // If we failed to parse and if the caller requested it, then display usage information.
            //

            if (!parseResult && options.DisplayUsageInfoOnError && actualReporter != null)
            {
                actualReporter(ColoredMultistring.FromString(Environment.NewLine));
                actualReporter(GetUsageInfo(parserArgSet, options.HelpOptions, destination));
            }

            return(parseResult);
        }