예제 #1
0
        public void CommandGroupHelp(CommandGroup commandGroup)
        {
            if (commandGroup == null)
            {
                throw new ArgumentNullException(nameof(commandGroup));
            }
            var appName = Assembly.GetEntryAssembly().GetName().Name;

            Console.WriteLine($"Command group: {commandGroup.Name}");

            // Print all the commands except the help command itself(that's the one we're currently in).
            // Use the MethodHandle to compare.
            var currentMethodHandle = MethodBase.GetCurrentMethod().MethodHandle;
            var commands            = commandGroup.Commands
                                      .Where(q => !q.CommandMethodInfo.MethodHandle.Equals(currentMethodHandle));

            Console.WriteLine($"Syntax: {appName} {commandGroup.Name} [command] [option:value] [[--]argument:value]");

            Console.WriteLine();
            Console.WriteLine($"Available commands:");
            foreach (var command in commands)
            {
                CommandTreeBuilder.FillOptionsAndArguments(command);
                Console.WriteLine($"{command.CommandAttribute.Name} - {command.CommandAttribute.Description}");

                if (command.Options.Any())
                {
                    Console.WriteLine($"  Options:");
                    ConsoleUtilities.WriteDictionary(
                        command.Options.ToDictionary(key => key.CommandOptionAttribute.Name, value => value.CommandOptionAttribute.Description),
                        5, 4);
                }

                if (command.Arguments.Any())
                {
                    Console.WriteLine($"  Arguments:");
                    ConsoleUtilities.WriteDictionary(
                        command.Arguments.ToDictionary(key => key.CommandArgumentAttribute.Name, value => value.CommandArgumentAttribute.Description),
                        5, 4);
                }

                if (!string.IsNullOrWhiteSpace(command.CommandAttribute.ExampleUsage))
                {
                    Console.WriteLine($"  Example usage:");
                    Console.WriteLine($"    {command.CommandAttribute.ExampleUsage}");
                }
                Console.WriteLine();
            }

            Console.WriteLine();
        }
            public void FillsArguments(Type commandGroupType, string commandMethodName)
            {
                var methodInfo = commandGroupType.GetMethod(commandMethodName);

                _expectedArgumentsPerCommand.TryGetValue(methodInfo, out var expectedArguments);
                Assert.NotNull(expectedArguments, $"Test setup not correct. Missing expectedArguments for command of method {commandGroupType.Name}.{commandMethodName}");

                var command = new Command()
                {
                    CommandMethodInfo = methodInfo
                };

                CommandTreeBuilder.FillOptionsAndArguments(command);

                Assert.Multiple(() =>
                {
                    foreach (var expected in expectedArguments)
                    {
                        var found = command.Arguments.Any(
                            q => q.CommandArgumentAttribute.Name == expected.Name && q.ParameterInfo.ParameterType == expected.ParameterType);
                        Assert.True(found, $"Argument {expected.Name} with parameter type {expected.ParameterType.Name} for command of method {commandGroupType.Name}.{commandMethodName} was not found.");
                    }
                });
            }