Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from EternalQuasar0206/refactor/hard-refactor-u…
Browse files Browse the repository at this point in the history
…pdates

v2.0.0: hard refactor updates and remade features
  • Loading branch information
scarletquasar authored Mar 6, 2022
2 parents 8a38acb + 7cb4ed2 commit 61d00db
Show file tree
Hide file tree
Showing 29 changed files with 706 additions and 181 deletions.
7 changes: 7 additions & 0 deletions Cli.NET/Cli.NET.Abstractions/Actions/ICommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Cli.NET.Abstractions.Actions
{
public interface ICommand
{
public void Execute(string[] arguments);
}
}
9 changes: 9 additions & 0 deletions Cli.NET/Cli.NET.Abstractions/Cli.NET.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions Cli.NET/Cli.NET.Console/Cli.NET.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Cli.NET.Abstractions\Cli.NET.Abstractions.csproj" />
<ProjectReference Include="..\Cli.NET\Cli.NET.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Cli.NET/Cli.NET.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Cli.NET.Actions;
using Cli.NET.Tools;

var container = new CommandContainer();

container.Register(new()
{
{ "exit", new ExitCommand() },
{ "echo", new EchoCommand() },
{ "sum", new SumCommand() },
{ "clear", new ClearCommand() },
{ "cls", new ClearCommand() }
});

container.WaitForNextCommand();
42 changes: 42 additions & 0 deletions Cli.NET/Cli.NET.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cli.NET", "Cli.NET\Cli.NET.csproj", "{071AAB32-CFAB-4A02-9203-9E0A1D831DB4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cli.NET.Abstractions", "Cli.NET.Abstractions\Cli.NET.Abstractions.csproj", "{4887C5D7-2C3B-4E33-B7A5-DF48571FF08F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cli.NET.Console", "Cli.NET.Console\Cli.NET.Console.csproj", "{B57D8554-6089-4093-9251-9CE6A30B7D6C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RepositoryItems", "RepositoryItems", "{FE91112C-5E7A-4C53-8708-5EEF11074BE7}"
ProjectSection(SolutionItems) = preProject
..\README.MD = ..\README.MD
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{071AAB32-CFAB-4A02-9203-9E0A1D831DB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{071AAB32-CFAB-4A02-9203-9E0A1D831DB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{071AAB32-CFAB-4A02-9203-9E0A1D831DB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{071AAB32-CFAB-4A02-9203-9E0A1D831DB4}.Release|Any CPU.Build.0 = Release|Any CPU
{4887C5D7-2C3B-4E33-B7A5-DF48571FF08F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4887C5D7-2C3B-4E33-B7A5-DF48571FF08F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4887C5D7-2C3B-4E33-B7A5-DF48571FF08F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4887C5D7-2C3B-4E33-B7A5-DF48571FF08F}.Release|Any CPU.Build.0 = Release|Any CPU
{B57D8554-6089-4093-9251-9CE6A30B7D6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B57D8554-6089-4093-9251-9CE6A30B7D6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B57D8554-6089-4093-9251-9CE6A30B7D6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B57D8554-6089-4093-9251-9CE6A30B7D6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {03167DE2-D179-434A-AE02-A17D4058BBE4}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions Cli.NET/Cli.NET/Actions/ClearCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Cli.NET.Abstractions.Actions;

namespace Cli.NET.Actions
{
/// <summary>
/// Default example "clear" command to erase the information in the screen
/// and writes new information if given.
/// </summary>
public class ClearCommand : ICommand
{
public void Execute(string[] arguments)
{
Console.Clear();

if(arguments.Any())
Console.WriteLine(string.Join("", arguments));
}
}
}
12 changes: 12 additions & 0 deletions Cli.NET/Cli.NET/Actions/EchoCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Cli.NET.Abstractions.Actions;

namespace Cli.NET.Actions
{
/// <summary>
/// Default example "echo" command to display data in the console.
/// </summary>
public class EchoCommand : ICommand
{
public void Execute(string[] arguments) => Console.WriteLine(string.Join(" ", arguments));
}
}
17 changes: 17 additions & 0 deletions Cli.NET/Cli.NET/Actions/ExitCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Cli.NET.Abstractions.Actions;

namespace Cli.NET.Actions
{
public class ExitCommand : ICommand
{
/// <summary>
/// Default example "exit" command with a given exit code.
/// </summary>
/// <param name="args"></param>
public void Execute(string[] args)
{
try { Environment.Exit(int.Parse(args[0])); }
catch { Environment.Exit(0); }
}
}
}
13 changes: 13 additions & 0 deletions Cli.NET/Cli.NET/Actions/SumCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Cli.NET.Abstractions.Actions;

namespace Cli.NET.Actions
{
public class SumCommand : ICommand
{
public void Execute(string[] arguments)
{
try { Console.WriteLine(long.Parse(arguments[0]) + long.Parse(arguments[1])); }
catch { Console.WriteLine("NaN"); }
}
}
}
13 changes: 13 additions & 0 deletions Cli.NET/Cli.NET/Cli.NET.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Cli.NET.Abstractions\Cli.NET.Abstractions.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions Cli.NET/Cli.NET/Extensions/ParameterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Cli.NET.Extensions
{
public static class ParameterExtensions
{
public static IDictionary<string, string> GetArguments(this string[] parameters)
{
Dictionary<string, string> arguments = new();

var targets = parameters.Skip(1);
var argumentHeader = string.Empty;
var argumentInformation = string.Empty;

int currentHeaderIndex = -1;
foreach (var target in targets)
{
if (target.StartsWith("-"))
{
currentHeaderIndex++;
argumentHeader = target;
}
else
{
argumentInformation += target;
arguments.Add(argumentHeader, argumentInformation);
}
}

return arguments;
}
}
}
4 changes: 4 additions & 0 deletions Cli.NET/Cli.NET/Models/CommandList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Cli.NET.Abstractions.Actions;

namespace Cli.NET.Models;
public class CommandList : Dictionary<string, ICommand> {}
19 changes: 19 additions & 0 deletions Cli.NET/Cli.NET/Models/Output/Output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Cli.NET.Models
{
public class Output
{
/// <summary>
/// Output default model to manage console responses.
/// </summary>
/// <param name="message"></param>
/// <param name="type"></param>
public Output(string message, OutputType type)
{
Message = message;
Type = type;
}

public OutputType Type { get; set; }
public string Message { get; set; }
}
}
9 changes: 9 additions & 0 deletions Cli.NET/Cli.NET/Models/Output/OutputType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Cli.NET.Models
{
public enum OutputType
{
Message,
Warning,
Error
}
}
2 changes: 2 additions & 0 deletions Cli.NET/Cli.NET/Models/OutputList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
namespace Cli.NET.Models;
public class OutputList : Dictionary<DateTime, Output> { }
55 changes: 55 additions & 0 deletions Cli.NET/Cli.NET/Tools/CLNConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Cli.NET.Tools
{
public static class CLNConsole
{
public static void WriteLine(string message) => Console.WriteLine(message);
public static void WriteLine(string message, string color) => WriteLine(message, Enum.Parse<ConsoleColor>(color));
public static void WriteLine(string message, int color) => WriteLine(message, (ConsoleColor)color);
public static void WriteLine(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
}

public static void Write(string message) => Console.Write(message);
public static void Write(string message, string color) => Write(message, Enum.Parse<ConsoleColor>(color));
public static void Write(string message, int color) => Write(message, (ConsoleColor)color);
public static void Write(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.Write(message);
Console.ResetColor();
}

public static string ReadText() => Console.ReadLine() ?? string.Empty;
public static string? ReadText(uint minLength = 0, uint maxLength = int.MaxValue)
{
string input = ReadText();

if (input.Length > maxLength || input.Length < minLength)
return null;

return input;
}

public static string? DataQuestion(string message)
{
WriteLine(message);
return ReadText();
}
public static string? DataQuestion(string message, string color, uint minLength = 0, uint maxLength = int.MaxValue)
{
return DataQuestion(message, Enum.Parse<ConsoleColor>(color), minLength, maxLength);
}
public static string? DataQuestion(string message, int color, uint minLength = 0, uint maxLength = int.MaxValue)
{
return DataQuestion(message, (ConsoleColor)color, minLength, maxLength);
}
public static string? DataQuestion(string message, ConsoleColor color, uint minLength = 0, uint maxLength = int.MaxValue)
{
WriteLine(message, color);
return ReadText(minLength, maxLength);
}
}
}
Loading

0 comments on commit 61d00db

Please sign in to comment.