mirror of
https://github.com/yaakov-h/Shamir.git
synced 2025-01-18 08:46:33 +00:00
line endings
This commit is contained in:
parent
42fba23dd6
commit
f45b840705
6 changed files with 473 additions and 472 deletions
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
* text=auto
|
112
shamir.sln
112
shamir.sln
|
@ -1,56 +1,56 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5384980-CE26-4FC0-9894-0A7AE7915FA0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shamir", "src\console\shamir.csproj", "{5264C707-99B4-43B7-AD5C-5EDD8609E697}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{A1008129-BAE8-49BD-8807-D0A2E0082307}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console.tests", "tests\console.tests\console.tests.csproj", "{9133E2DC-9E86-4DF9-A080-4122B0468FDE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x64.Build.0 = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x86.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x64.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697} = {A5384980-CE26-4FC0-9894-0A7AE7915FA0}
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE} = {A1008129-BAE8-49BD-8807-D0A2E0082307}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A5384980-CE26-4FC0-9894-0A7AE7915FA0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shamir", "src\console\shamir.csproj", "{5264C707-99B4-43B7-AD5C-5EDD8609E697}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{A1008129-BAE8-49BD-8807-D0A2E0082307}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console.tests", "tests\console.tests\console.tests.csproj", "{9133E2DC-9E86-4DF9-A080-4122B0468FDE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|Any CPU = Release|Any CPU
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x64.Build.0 = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697}.Release|x86.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x64.Build.0 = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{5264C707-99B4-43B7-AD5C-5EDD8609E697} = {A5384980-CE26-4FC0-9894-0A7AE7915FA0}
|
||||
{9133E2DC-9E86-4DF9-A080-4122B0468FDE} = {A1008129-BAE8-49BD-8807-D0A2E0082307}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
@ -1,155 +1,155 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using CommandLine.Text;
|
||||
using SysConsole = System.Console;
|
||||
|
||||
namespace Shamir.Console
|
||||
{
|
||||
public class CdnOperations : ICommandSet
|
||||
{
|
||||
[Verb("ls", HelpText = "List files on the CDN")]
|
||||
public class CdnLsOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("add", HelpText = "Upload a file to the CDN")]
|
||||
public class CdnAddOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("mv", HelpText = "Move a file to the CDN")]
|
||||
public class CdnMvOptions
|
||||
{
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> VerbOptionTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return typeof(CdnLsOptions);
|
||||
yield return typeof(CdnAddOptions);
|
||||
yield return typeof(CdnMvOptions);
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand? FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length == 0) return null;
|
||||
|
||||
var parserResult = Parser.Default.ParseArguments<CdnLsOptions, CdnAddOptions, CdnMvOptions>(args.ToArray());
|
||||
return parserResult.MapResult<CdnLsOptions, CdnAddOptions, CdnMvOptions, ICommand>(
|
||||
options => new CdnLsCommand(options),
|
||||
options => new CdnAddCommand(options),
|
||||
options => new CdnMvCommand(options),
|
||||
errors => new HelpTextCommand(parserResult)
|
||||
);
|
||||
}
|
||||
|
||||
public class CdnLsCommand : ICommand
|
||||
{
|
||||
public CdnLsCommand(CdnLsOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-ls");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CdnAddCommand : ICommand
|
||||
{
|
||||
public CdnAddCommand(CdnAddOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-add");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CdnMvCommand : ICommand
|
||||
{
|
||||
public CdnMvCommand(CdnMvOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-mv");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class HelpTextCommand : ICommand
|
||||
{
|
||||
public HelpTextCommand(ParserResult<object> result)
|
||||
{
|
||||
this.result = result ?? throw new ArgumentNullException(nameof(result));
|
||||
Debug.Assert(result.Tag == ParserResultType.NotParsed);
|
||||
}
|
||||
|
||||
readonly ParserResult<object> result;
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
var helpText = HelpText.AutoBuild(result);
|
||||
SysConsole.Error.WriteLine(helpText);
|
||||
return ValueTask.FromResult(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICommand
|
||||
{
|
||||
ValueTask<int> ExecuteAsync();
|
||||
}
|
||||
|
||||
public interface ICommandSet
|
||||
{
|
||||
ICommand? FindCommand(ReadOnlySpan<string> args);
|
||||
static IEnumerable<Type> VerbOptionTypes { get; }
|
||||
}
|
||||
|
||||
public class CommandTree : ICommandSet
|
||||
{
|
||||
IEnumerable<Type> VerbOptionTypes { get; }
|
||||
public ICommand? FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length == 0) return null;
|
||||
|
||||
return args[0] switch {
|
||||
"cdn" => new CdnOperations().FindCommand(args[1..]),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
var tree = new CommandTree();
|
||||
var command = tree.FindCommand(args);
|
||||
if (command is null)
|
||||
{
|
||||
PrintHelpText(tree);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return await command.ExecuteAsync();
|
||||
}
|
||||
|
||||
static void PrintHelpText(ICommandSet tree)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using CommandLine;
|
||||
using CommandLine.Text;
|
||||
using SysConsole = System.Console;
|
||||
|
||||
namespace Shamir.Console
|
||||
{
|
||||
public class CdnOperations : ICommandSet
|
||||
{
|
||||
[Verb("ls", HelpText = "List files on the CDN")]
|
||||
public class CdnLsOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("add", HelpText = "Upload a file to the CDN")]
|
||||
public class CdnAddOptions
|
||||
{
|
||||
}
|
||||
|
||||
[Verb("mv", HelpText = "Move a file to the CDN")]
|
||||
public class CdnMvOptions
|
||||
{
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> VerbOptionTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return typeof(CdnLsOptions);
|
||||
yield return typeof(CdnAddOptions);
|
||||
yield return typeof(CdnMvOptions);
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand? FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length == 0) return null;
|
||||
|
||||
var parserResult = Parser.Default.ParseArguments<CdnLsOptions, CdnAddOptions, CdnMvOptions>(args.ToArray());
|
||||
return parserResult.MapResult<CdnLsOptions, CdnAddOptions, CdnMvOptions, ICommand>(
|
||||
options => new CdnLsCommand(options),
|
||||
options => new CdnAddCommand(options),
|
||||
options => new CdnMvCommand(options),
|
||||
errors => new HelpTextCommand(parserResult)
|
||||
);
|
||||
}
|
||||
|
||||
public class CdnLsCommand : ICommand
|
||||
{
|
||||
public CdnLsCommand(CdnLsOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-ls");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CdnAddCommand : ICommand
|
||||
{
|
||||
public CdnAddCommand(CdnAddOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-add");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class CdnMvCommand : ICommand
|
||||
{
|
||||
public CdnMvCommand(CdnMvOptions options)
|
||||
{
|
||||
}
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
SysConsole.WriteLine($"Executing: cdn-mv");
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class HelpTextCommand : ICommand
|
||||
{
|
||||
public HelpTextCommand(ParserResult<object> result)
|
||||
{
|
||||
this.result = result ?? throw new ArgumentNullException(nameof(result));
|
||||
Debug.Assert(result.Tag == ParserResultType.NotParsed);
|
||||
}
|
||||
|
||||
readonly ParserResult<object> result;
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
var helpText = HelpText.AutoBuild(result);
|
||||
SysConsole.Error.WriteLine(helpText);
|
||||
return ValueTask.FromResult(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICommand
|
||||
{
|
||||
ValueTask<int> ExecuteAsync();
|
||||
}
|
||||
|
||||
public interface ICommandSet
|
||||
{
|
||||
ICommand? FindCommand(ReadOnlySpan<string> args);
|
||||
static IEnumerable<Type> VerbOptionTypes { get; }
|
||||
}
|
||||
|
||||
public class CommandTree : ICommandSet
|
||||
{
|
||||
IEnumerable<Type> VerbOptionTypes { get; }
|
||||
public ICommand? FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length == 0) return null;
|
||||
|
||||
return args[0] switch {
|
||||
"cdn" => new CdnOperations().FindCommand(args[1..]),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
var tree = new CommandTree();
|
||||
var command = tree.FindCommand(args);
|
||||
if (command is null)
|
||||
{
|
||||
PrintHelpText(tree);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return await command.ExecuteAsync();
|
||||
}
|
||||
|
||||
static void PrintHelpText(ICommandSet tree)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<RootNamespace>Shamir.Console</RootNamespace>
|
||||
<OutputPath>..\..\bin</OutputPath>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<RootNamespace>Shamir.Console</RootNamespace>
|
||||
<OutputPath>..\..\bin</OutputPath>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,228 +1,228 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Shamir.Console.Tests
|
||||
{
|
||||
public class CommandTreeTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
tree = new DefaultCommandTree(
|
||||
"root",
|
||||
"top level",
|
||||
ImmutableArray.Create<ICommandTree>(
|
||||
new DefaultCommandTree(
|
||||
"bar",
|
||||
"child command tree",
|
||||
ImmutableArray<ICommandTree>.Empty,
|
||||
ImmutableArray.Create<ICommand>(
|
||||
new SimpleTextCommand("one", "bar/1 command"),
|
||||
new SimpleTextCommand("two", "bar/2 command")
|
||||
))),
|
||||
ImmutableArray.Create<ICommand>(
|
||||
new SimpleTextCommand("foo", "foo command")
|
||||
));
|
||||
}
|
||||
|
||||
ICommandTree tree;
|
||||
|
||||
[Test]
|
||||
public void ConstructsHelpTextWithNoArgs()
|
||||
{
|
||||
var command = tree.FindCommand(Array.Empty<string>());
|
||||
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
|
||||
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
|
||||
Assert.That(helpText, Is.EqualTo(@"
|
||||
Group
|
||||
root : top level
|
||||
|
||||
Subgroups:
|
||||
bar : child command tree
|
||||
|
||||
Commands:
|
||||
foo : foo command
|
||||
".TrimStart()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructsHelpTextForSubTree()
|
||||
{
|
||||
var command = tree.FindCommand(new[] { "bar" });
|
||||
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
|
||||
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
|
||||
Assert.That(helpText, Is.EqualTo(@"
|
||||
Group
|
||||
root bar : child command tree
|
||||
|
||||
Commands:
|
||||
one : bar/1 command
|
||||
two : bar/1 command
|
||||
"));
|
||||
}
|
||||
|
||||
[TestCase((object)new string[] { "foo" }, ExpectedResult = "foo command")]
|
||||
[TestCase((object)new string[] { "bar", "one" }, ExpectedResult = "bar/1 command")]
|
||||
[TestCase((object)new string[] { "bar", "two" }, ExpectedResult = "bar/2 command")]
|
||||
public string FindsCommand(string[] args)
|
||||
{
|
||||
var command = tree.FindCommand(args);
|
||||
return command?.Description;
|
||||
}
|
||||
|
||||
|
||||
class SimpleTextCommand : ICommand
|
||||
{
|
||||
public SimpleTextCommand(string name, string description)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
Description = description ?? throw new ArgumentNullException(nameof(description));
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string Description { get; }
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface IOperationsNode
|
||||
{
|
||||
string Name { get; }
|
||||
string Description { get; }
|
||||
}
|
||||
|
||||
interface ICommand : IOperationsNode
|
||||
{
|
||||
ValueTask<int> ExecuteAsync();
|
||||
}
|
||||
|
||||
interface ICommandTree : IOperationsNode
|
||||
{
|
||||
ImmutableArray<ICommandTree> SubTrees { get; }
|
||||
ImmutableArray<ICommand> Commands { get; }
|
||||
ICommand FindCommand(ReadOnlySpan<string> args);
|
||||
}
|
||||
|
||||
class DefaultCommandTree : ICommandTree
|
||||
{
|
||||
public DefaultCommandTree(string name, string description, ImmutableArray<ICommandTree> subtrees, ImmutableArray<ICommand> commands)
|
||||
{
|
||||
this.Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
this.Description = description ?? throw new ArgumentNullException(nameof(description));
|
||||
this.SubTrees = subtrees;
|
||||
this.Commands = commands;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public string Description { get; }
|
||||
public ImmutableArray<ICommandTree> SubTrees { get; }
|
||||
public ImmutableArray<ICommand> Commands { get; }
|
||||
|
||||
public ICommand FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length > 0)
|
||||
{
|
||||
foreach (var command in Commands)
|
||||
{
|
||||
if (command.Name == args[0])
|
||||
{
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var tree in SubTrees)
|
||||
{
|
||||
if (tree.Name == args[0])
|
||||
{
|
||||
return tree.FindCommand(args[1..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new DefaultHelpTextCommand(this);
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultHelpTextCommand : ICommand
|
||||
{
|
||||
public DefaultHelpTextCommand(ICommandTree tree)
|
||||
{
|
||||
this.CommandTree = tree ?? throw new ArgumentNullException(nameof(tree));
|
||||
}
|
||||
|
||||
public string Name => "help";
|
||||
public string Description => "Print help text";
|
||||
|
||||
public ICommandTree CommandTree { get; }
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
System.Console.Error.WriteLine(GetHelpText());
|
||||
return ValueTask.FromResult(1); // TODO: const
|
||||
}
|
||||
|
||||
public string GetHelpText() => CommandTree.BuildHelpText();
|
||||
}
|
||||
|
||||
static class ICommandTreeExtensions
|
||||
{
|
||||
public static string BuildHelpText(this ICommandTree tree)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("Group");
|
||||
sb.Append(" ");
|
||||
sb.AppendLine(tree.Name);
|
||||
sb.AppendLine();
|
||||
|
||||
if (!tree.SubTrees.IsEmpty)
|
||||
{
|
||||
sb.AppendLine("Subgroups:");
|
||||
|
||||
var maxSpacing = tree.SubTrees.Max(c => c.Name.Length) + 1;
|
||||
|
||||
foreach (var child in tree.SubTrees)
|
||||
{
|
||||
sb.Append(" ");
|
||||
sb.Append(child.Name);
|
||||
for (var i = 0; i < maxSpacing; i++)
|
||||
{
|
||||
sb.Append(' ');
|
||||
}
|
||||
sb.Append(": ");
|
||||
sb.AppendLine(child.Description);
|
||||
}
|
||||
}
|
||||
|
||||
if (!tree.Commands.IsEmpty)
|
||||
{
|
||||
sb.AppendLine("Commands:");
|
||||
|
||||
var maxSpacing = tree.Commands.Max(c => c.Name.Length) + 1;
|
||||
|
||||
foreach (var command in tree.Commands)
|
||||
{
|
||||
sb.Append(" ");
|
||||
sb.Append(command.Name);
|
||||
for (var i = 0; i < maxSpacing; i++)
|
||||
{
|
||||
sb.Append(' ');
|
||||
}
|
||||
sb.Append(": ");
|
||||
sb.AppendLine(command.Description);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Shamir.Console.Tests
|
||||
{
|
||||
public class CommandTreeTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
tree = new DefaultCommandTree(
|
||||
"root",
|
||||
"top level",
|
||||
ImmutableArray.Create<ICommandTree>(
|
||||
new DefaultCommandTree(
|
||||
"bar",
|
||||
"child command tree",
|
||||
ImmutableArray<ICommandTree>.Empty,
|
||||
ImmutableArray.Create<ICommand>(
|
||||
new SimpleTextCommand("one", "bar/1 command"),
|
||||
new SimpleTextCommand("two", "bar/2 command")
|
||||
))),
|
||||
ImmutableArray.Create<ICommand>(
|
||||
new SimpleTextCommand("foo", "foo command")
|
||||
));
|
||||
}
|
||||
|
||||
ICommandTree tree;
|
||||
|
||||
[Test]
|
||||
public void ConstructsHelpTextWithNoArgs()
|
||||
{
|
||||
var command = tree.FindCommand(Array.Empty<string>());
|
||||
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
|
||||
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
|
||||
Assert.That(helpText, Is.EqualTo(@"
|
||||
Group
|
||||
root : top level
|
||||
|
||||
Subgroups:
|
||||
bar : child command tree
|
||||
|
||||
Commands:
|
||||
foo : foo command
|
||||
".TrimStart()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructsHelpTextForSubTree()
|
||||
{
|
||||
var command = tree.FindCommand(new[] { "bar" });
|
||||
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
|
||||
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
|
||||
Assert.That(helpText, Is.EqualTo(@"
|
||||
Group
|
||||
root bar : child command tree
|
||||
|
||||
Commands:
|
||||
one : bar/1 command
|
||||
two : bar/1 command
|
||||
"));
|
||||
}
|
||||
|
||||
[TestCase((object)new string[] { "foo" }, ExpectedResult = "foo command")]
|
||||
[TestCase((object)new string[] { "bar", "one" }, ExpectedResult = "bar/1 command")]
|
||||
[TestCase((object)new string[] { "bar", "two" }, ExpectedResult = "bar/2 command")]
|
||||
public string FindsCommand(string[] args)
|
||||
{
|
||||
var command = tree.FindCommand(args);
|
||||
return command?.Description;
|
||||
}
|
||||
|
||||
|
||||
class SimpleTextCommand : ICommand
|
||||
{
|
||||
public SimpleTextCommand(string name, string description)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
Description = description ?? throw new ArgumentNullException(nameof(description));
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string Description { get; }
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
return ValueTask.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface IOperationsNode
|
||||
{
|
||||
string Name { get; }
|
||||
string Description { get; }
|
||||
}
|
||||
|
||||
interface ICommand : IOperationsNode
|
||||
{
|
||||
ValueTask<int> ExecuteAsync();
|
||||
}
|
||||
|
||||
interface ICommandTree : IOperationsNode
|
||||
{
|
||||
ImmutableArray<ICommandTree> SubTrees { get; }
|
||||
ImmutableArray<ICommand> Commands { get; }
|
||||
ICommand FindCommand(ReadOnlySpan<string> args);
|
||||
}
|
||||
|
||||
class DefaultCommandTree : ICommandTree
|
||||
{
|
||||
public DefaultCommandTree(string name, string description, ImmutableArray<ICommandTree> subtrees, ImmutableArray<ICommand> commands)
|
||||
{
|
||||
this.Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
this.Description = description ?? throw new ArgumentNullException(nameof(description));
|
||||
this.SubTrees = subtrees;
|
||||
this.Commands = commands;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public string Description { get; }
|
||||
public ImmutableArray<ICommandTree> SubTrees { get; }
|
||||
public ImmutableArray<ICommand> Commands { get; }
|
||||
|
||||
public ICommand FindCommand(ReadOnlySpan<string> args)
|
||||
{
|
||||
if (args.Length > 0)
|
||||
{
|
||||
foreach (var command in Commands)
|
||||
{
|
||||
if (command.Name == args[0])
|
||||
{
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var tree in SubTrees)
|
||||
{
|
||||
if (tree.Name == args[0])
|
||||
{
|
||||
return tree.FindCommand(args[1..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new DefaultHelpTextCommand(this);
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultHelpTextCommand : ICommand
|
||||
{
|
||||
public DefaultHelpTextCommand(ICommandTree tree)
|
||||
{
|
||||
this.CommandTree = tree ?? throw new ArgumentNullException(nameof(tree));
|
||||
}
|
||||
|
||||
public string Name => "help";
|
||||
public string Description => "Print help text";
|
||||
|
||||
public ICommandTree CommandTree { get; }
|
||||
|
||||
public ValueTask<int> ExecuteAsync()
|
||||
{
|
||||
System.Console.Error.WriteLine(GetHelpText());
|
||||
return ValueTask.FromResult(1); // TODO: const
|
||||
}
|
||||
|
||||
public string GetHelpText() => CommandTree.BuildHelpText();
|
||||
}
|
||||
|
||||
static class ICommandTreeExtensions
|
||||
{
|
||||
public static string BuildHelpText(this ICommandTree tree)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("Group");
|
||||
sb.Append(" ");
|
||||
sb.AppendLine(tree.Name);
|
||||
sb.AppendLine();
|
||||
|
||||
if (!tree.SubTrees.IsEmpty)
|
||||
{
|
||||
sb.AppendLine("Subgroups:");
|
||||
|
||||
var maxSpacing = tree.SubTrees.Max(c => c.Name.Length) + 1;
|
||||
|
||||
foreach (var child in tree.SubTrees)
|
||||
{
|
||||
sb.Append(" ");
|
||||
sb.Append(child.Name);
|
||||
for (var i = 0; i < maxSpacing; i++)
|
||||
{
|
||||
sb.Append(' ');
|
||||
}
|
||||
sb.Append(": ");
|
||||
sb.AppendLine(child.Description);
|
||||
}
|
||||
}
|
||||
|
||||
if (!tree.Commands.IsEmpty)
|
||||
{
|
||||
sb.AppendLine("Commands:");
|
||||
|
||||
var maxSpacing = tree.Commands.Max(c => c.Name.Length) + 1;
|
||||
|
||||
foreach (var command in tree.Commands)
|
||||
{
|
||||
sb.Append(" ");
|
||||
sb.Append(command.Name);
|
||||
for (var i = 0; i < maxSpacing; i++)
|
||||
{
|
||||
sb.Append(' ');
|
||||
}
|
||||
sb.Append(": ");
|
||||
sb.AppendLine(command.Description);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>Shamir.Console.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\console\shamir.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>Shamir.Console.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\console\shamir.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue