mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 10:35:27 +08:00 
			
		
		
		
	Add Spectre.Cli to Spectre.Console
* Renames Spectre.Cli to Spectre.Console.Cli. * Now uses Verify with Spectre.Console.Cli tests. * Removes some duplicate definitions. Closes #168
This commit is contained in:
		
				
					committed by
					
						
						Patrik Svensson
					
				
			
			
				
	
			
			
			
						parent
						
							0bbf9b81a9
						
					
				
				
					commit
					0ae419326d
				
			
							
								
								
									
										49
									
								
								src/Spectre.Console.Tests/Data/Commands/AnimalCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/Spectre.Console.Tests/Data/Commands/AnimalCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,49 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
using SystemConsole = System.Console;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public abstract class AnimalCommand<TSettings> : Command<TSettings>
 | 
			
		||||
        where TSettings : CommandSettings
 | 
			
		||||
    {
 | 
			
		||||
        protected void DumpSettings(CommandContext context, TSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            if (context == null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new ArgumentNullException(nameof(context));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (settings == null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new ArgumentNullException(nameof(settings));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var properties = settings.GetType().GetProperties();
 | 
			
		||||
            foreach (var group in properties.GroupBy(x => x.DeclaringType).Reverse())
 | 
			
		||||
            {
 | 
			
		||||
                SystemConsole.WriteLine();
 | 
			
		||||
                SystemConsole.ForegroundColor = ConsoleColor.Yellow;
 | 
			
		||||
                SystemConsole.WriteLine(group.Key.FullName);
 | 
			
		||||
                SystemConsole.ResetColor();
 | 
			
		||||
 | 
			
		||||
                foreach (var property in group)
 | 
			
		||||
                {
 | 
			
		||||
                    SystemConsole.WriteLine($"  {property.Name} = {property.GetValue(settings)}");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (context.Remaining.Raw.Count > 0)
 | 
			
		||||
            {
 | 
			
		||||
                SystemConsole.WriteLine();
 | 
			
		||||
                SystemConsole.ForegroundColor = ConsoleColor.Yellow;
 | 
			
		||||
                SystemConsole.WriteLine("Remaining:");
 | 
			
		||||
                SystemConsole.ResetColor();
 | 
			
		||||
                SystemConsole.WriteLine(string.Join(", ", context.Remaining));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            SystemConsole.WriteLine();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								src/Spectre.Console.Tests/Data/Commands/CatCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Spectre.Console.Tests/Data/Commands/CatCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public class CatCommand : AnimalCommand<CatSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, CatSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            DumpSettings(context, settings);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								src/Spectre.Console.Tests/Data/Commands/DogCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/Spectre.Console.Tests/Data/Commands/DogCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
using System.ComponentModel;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    [Description("The dog command.")]
 | 
			
		||||
    public class DogCommand : AnimalCommand<DogSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override ValidationResult Validate(CommandContext context, DogSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            if (context is null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.ArgumentNullException(nameof(context));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (settings is null)
 | 
			
		||||
            {
 | 
			
		||||
                throw new System.ArgumentNullException(nameof(settings));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (settings.Age > 100 && !context.Remaining.Raw.Contains("zombie"))
 | 
			
		||||
            {
 | 
			
		||||
                return ValidationResult.Error("Dog is too old...");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return base.Validate(context, settings);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override int Execute(CommandContext context, DogSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            DumpSettings(context, settings);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								src/Spectre.Console.Tests/Data/Commands/EmptyCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/Spectre.Console.Tests/Data/Commands/EmptyCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public sealed class EmptyCommand : Command<EmptyCommandSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, EmptyCommandSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								src/Spectre.Console.Tests/Data/Commands/GenericCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Spectre.Console.Tests/Data/Commands/GenericCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public sealed class GenericCommand<TSettings> : Command<TSettings>
 | 
			
		||||
        where TSettings : CommandSettings
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, TSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								src/Spectre.Console.Tests/Data/Commands/GiraffeCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/Spectre.Console.Tests/Data/Commands/GiraffeCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
using System.ComponentModel;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    [Description("The giraffe command.")]
 | 
			
		||||
    public sealed class GiraffeCommand : Command<GiraffeSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, GiraffeSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								src/Spectre.Console.Tests/Data/Commands/HorseCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								src/Spectre.Console.Tests/Data/Commands/HorseCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
using System.ComponentModel;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    [Description("The horse command.")]
 | 
			
		||||
    public class HorseCommand : AnimalCommand<MammalSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, MammalSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            DumpSettings(context, settings);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,22 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public sealed class InterceptingCommand<TSettings> : Command<TSettings>
 | 
			
		||||
    where TSettings : CommandSettings
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Action<CommandContext, TSettings> _action;
 | 
			
		||||
 | 
			
		||||
        public InterceptingCommand(Action<CommandContext, TSettings> action)
 | 
			
		||||
        {
 | 
			
		||||
            _action = action ?? throw new ArgumentNullException(nameof(action));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override int Execute(CommandContext context, TSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            _action(context, settings);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								src/Spectre.Console.Tests/Data/Commands/InvalidCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/Spectre.Console.Tests/Data/Commands/InvalidCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public sealed class InvalidCommand : Command<InvalidSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, InvalidSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								src/Spectre.Console.Tests/Data/Commands/LionCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/Spectre.Console.Tests/Data/Commands/LionCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
using System.ComponentModel;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    [Description("The lion command.")]
 | 
			
		||||
    public class LionCommand : AnimalCommand<LionSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, LionSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public class OptionVectorCommand : Command<OptionVectorSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, OptionVectorSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								src/Spectre.Console.Tests/Data/Commands/ThrowingCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/Spectre.Console.Tests/Data/Commands/ThrowingCommand.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Spectre.Console.Cli;
 | 
			
		||||
 | 
			
		||||
namespace Spectre.Console.Tests.Data
 | 
			
		||||
{
 | 
			
		||||
    public sealed class ThrowingCommand : Command<ThrowingCommandSettings>
 | 
			
		||||
    {
 | 
			
		||||
        public override int Execute(CommandContext context, ThrowingCommandSettings settings)
 | 
			
		||||
        {
 | 
			
		||||
            throw new InvalidOperationException("W00t?");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public sealed class ThrowingCommandSettings : CommandSettings
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user