diff --git a/src/client/sparkcmd/Cmd/ConfigCheckCommand.cs b/src/client/sparkcmd/Cmd/ConfigCheckCommand.cs index 432fce36b..7284e48db 100644 --- a/src/client/sparkcmd/Cmd/ConfigCheckCommand.cs +++ b/src/client/sparkcmd/Cmd/ConfigCheckCommand.cs @@ -1,14 +1,19 @@ -using ManyConsole; using System.Linq; +using System; +using System.Collections.Generic; +using CommandLine; namespace bts { - public class Configcheck : ConsoleCommand { - public Configcheck() { - IsCommand("configcheck", "Ye olde Configcheck"); - HasAdditionalArguments(null, " [Keys to check]"); - } - public override int Run(string[] args) { - mbts.config.Configuration.configcheck(args.Length, args.ToArray()); + + [Verb("configcheck", HelpText="Ye olde Configcheck")] + public class ConfigcheckOptions { + [Value(0, MetaName="keys", HelpText="Keys to print")] + public IEnumerable keys { get; set; } + } + public class Configcheck { + public static int Run(ConfigcheckOptions options) { + var keys = options.keys.ToArray(); + mbts.config.Configuration.configcheck(keys.Length, keys); return 0; } } diff --git a/src/client/sparkcmd/Cmd/ConsoleCommandEx.cs b/src/client/sparkcmd/Cmd/ConsoleCommandEx.cs deleted file mode 100644 index 18c73075d..000000000 --- a/src/client/sparkcmd/Cmd/ConsoleCommandEx.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ManyConsole { - public static class ConsoleCommandEx { - public static string get_argument(this ConsoleCommand _, string arg_name) { - var arg = Environment.GetCommandLineArgs().FirstOrDefault(str => str.StartsWith("--"+arg_name)); - if (arg != null) { - var parts = arg.Split(new[] { '=' }, 2); - if (parts.Length > 1) { - return parts[1]; - } - } - return ""; - } - } -} diff --git a/src/client/sparkcmd/Cmd/ScriptClientCommand.cs b/src/client/sparkcmd/Cmd/ScriptClientCommand.cs index 18f0c18c9..5f00cf392 100644 --- a/src/client/sparkcmd/Cmd/ScriptClientCommand.cs +++ b/src/client/sparkcmd/Cmd/ScriptClientCommand.cs @@ -6,32 +6,37 @@ using System.Reflection; using System.Threading.Tasks; using System.Reactive.Linq; using System.Reactive; -using ManyConsole; using System.Linq; +using CommandLine; namespace sparkcmd { - public class ScriptClientCommand : ConsoleCommand { + [Verb("script", HelpText="Remote control for shell diagnostic host")] + public class ScriptClientOptions { + [Option('s', "server", HelpText="Run a script server for testing.")] + public bool server { get; set; } + + [Option('h', "host", HelpText="Connect to {host}")] + public string host { get; set; } + + [Option('f', "fromfile", HelpText="Read commands from file instead of stdin")] + public string file { get; set; } + + } + public class ScriptClientCommand { bool _server = false; string _host; string _logfile; - public ScriptClientCommand() { - IsCommand("script", "Remote control for shell diagnostic host"); - HasOption("s|server", "Run a script server for testing.", x => _server = true); - HasOption("h|host", "Connect to {host}", h => _host = h); - HasOption("f|fromfile", "Read commands from file instead of stdin", f => _logfile = f); - } - public override int Run(string[] args) { - if (_server) { + public static int Run(ScriptClientOptions o) { + if (o.server) { ScriptClient.runserver(); } else { - var host = string.IsNullOrEmpty(_host) ? args.First() : _host; - if (string.IsNullOrEmpty(_logfile) && File.Exists(_logfile)) { - ScriptClient.start(host, File.OpenRead(_logfile)); + if (string.IsNullOrEmpty(o.file) && File.Exists(o.file)) { + ScriptClient.start(o.host, File.OpenRead(o.file)); } else { - ScriptClient.start(host, Console.OpenStandardInput()); + ScriptClient.start(o.host, Console.OpenStandardInput()); } } return 0; diff --git a/src/client/sparkcmd/Cmd/ServiceTail.cs b/src/client/sparkcmd/Cmd/ServiceTail.cs index 7ebbb0c92..926bc5a6c 100644 --- a/src/client/sparkcmd/Cmd/ServiceTail.cs +++ b/src/client/sparkcmd/Cmd/ServiceTail.cs @@ -1,6 +1,6 @@ using bts.api.core; using bts.test; -using ManyConsole; +using CommandLine; using System; using System.Collections.Generic; using System.Linq; @@ -8,24 +8,26 @@ using System.Text; using System.Threading.Tasks; namespace bts { - class TailService : ConsoleCommand { + class TailServiceOptions { + [Option('m', "method", Required=true, HelpText = "'[ServiceName].[Method]' to call")] + public string method { get;set; } + + [Option('t', "time", HelpText="Include timestamp")] + public bool time { get; set; } + + [Option('p', "path", HelpText="Path inside the row to use for display. Ex: 'Id' or 'Name' ")] + public string path { get; set; } + + [Option('a', "all", HelpText="Include 'all' rows on every print")] + public bool all { get; set; } + } + class TailService { static bool _all = false; static bool _timestamp = false; - public TailService() { - void ignore(string _) { } - IsCommand("tailservice", "Tail updates to named service api. Ex: tail --method=SyntheticSecurityService.metadata"); - HasRequiredOption("method", "Service.method to tail", ignore); - HasOption("time", "Include timestamp", _ => _timestamp = true); - // The following are not implemented. If you need them get to work. - HasOption("path", "Path inside the row to use for display. Ex: 'Id' or 'Name' ", ignore); - HasOption("all", "Invlude 'all' rows on every print", _ => _all = true); - HasAdditionalArguments(null, " [nothing]"); - } - public override int Run(string[] remainingArguments) { - var arg = this.get_argument("method"); - var split = arg.Split('.'); + public static int Run(TailServiceOptions o) { + var split = o.method.Split('.'); if (split.Length != 2) { - Console.WriteLine("Try --method=[ServiceName].[Method]"); + Console.WriteLine("Try --method [ServiceName].[Method]"); return -1; } tail(split[0], split[1]); diff --git a/src/client/sparkcmd/Cmd/ServiceTestCommand.cs b/src/client/sparkcmd/Cmd/ServiceTestCommand.cs index 2b2d1acd0..331469b30 100644 --- a/src/client/sparkcmd/Cmd/ServiceTestCommand.cs +++ b/src/client/sparkcmd/Cmd/ServiceTestCommand.cs @@ -2,30 +2,29 @@ using bts.api; using bts.api.core; using bts.api.synthetic; using bts.test; -using ManyConsole; +using CommandLine; using System; using System.Threading; namespace bts { - public class ServiceTest : ConsoleCommand { - bool _synthetic; - bool _user; - bool _admin; - public ServiceTest() { - IsCommand("service", "Put code here for testing during service development"); - HasOption("synthetic", "Run synthetic test", _ => _synthetic = true); - HasOption("user", "user test", _ => _user = true); - HasOption("admin", "admin test", _ => _admin = true); - } - public override int Run(string[] remainingArguments) { - if (_synthetic) { + public class ServiceTestOptions { + [Option(nameof(synthetic))] + public bool synthetic { get; set; } + [Option(nameof(user))] + public bool user { get; set; } + [Option(nameof(admin))] + public bool admin { get; set; } + } + public class ServiceTest { + public static int Run(ServiceTestOptions o) { + if (o.synthetic) { synthetic_test(); } - if (_user) { + if (o.user) { user_test(); } - if (_admin) { + if (o.admin) { admin_test(); } return 0; diff --git a/src/client/sparkcmd/Cmd/SettingsManagerCommand.cs b/src/client/sparkcmd/Cmd/SettingsManagerCommand.cs index 3c74b9c87..7d01ad08d 100644 --- a/src/client/sparkcmd/Cmd/SettingsManagerCommand.cs +++ b/src/client/sparkcmd/Cmd/SettingsManagerCommand.cs @@ -1,14 +1,13 @@ -using ManyConsole; using System.Reflection; +using CommandLine; namespace bts { - public class SettinsManagerCommand : ConsoleCommand { - public SettinsManagerCommand() { - IsCommand("SettingsManager", "Copy workspaces around"); - } + [Verb("settingsmanager", HelpText="Copy workspaces around")] + public class SettingsManagerOptions { } - public override int Run(string[] remainingArguments) { + public class SettingsManagerCommand { + public static int Run(SettingsManagerOptions _) { var asm = Assembly.Load("spark"); var type = asm.GetType("bts.settings.SettingsManagerTool"); var run = type.GetMethod("run", BindingFlags.Static | BindingFlags.Public); diff --git a/src/client/sparkcmd/Cmd/TestCommand.cs b/src/client/sparkcmd/Cmd/TestCommand.cs index b8a0f790c..89dbf7498 100644 --- a/src/client/sparkcmd/Cmd/TestCommand.cs +++ b/src/client/sparkcmd/Cmd/TestCommand.cs @@ -1,6 +1,5 @@ using bts.test; -using ManyConsole; using System; using mbts; using System.Collections.Generic; @@ -8,47 +7,59 @@ using bts.api.test; using System.Threading.Tasks; using mbts.config; using System.Linq; +using CommandLine; namespace sparkcmd { - public class TestCommand : ConsoleCommand { - uint _timeout = 0; - uint _parallel = 1; - bool _list; - TestType test_type = TestType.Unit; + [Verb("test", HelpText="Run Tests")] + public class TestCommandOptions { - bool self_contained = false; + [Option('t', "timeout", HelpText="Timeout unit tets", Default=0)] + public uint timeout { get; set; } + [Option('l', "list", HelpText="List tests")] + public bool list { get; set; } + [Option('i', "insular", HelpText="Tests run sparkd and use their own config (if specified)")] + public bool self_contained { get; set; } - public TestCommand() { - IsCommand("test", "Run managed unit tests"); - // i comes thorugh as "t" which seems daft. - HasOption("t|timeout", "Timeout unit tests", i => _timeout = 10); - HasOption("n|parallel", "How many tests to run in parallel", i => _parallel = i); - HasOption("l|list", "List all tests", _ => _list = true); - HasOption("unit","Run unit tests", _ => test_type = TestType.Unit); - HasOption("system","Run system tests", _ => test_type = TestType.System); - HasOption("shell","Run shell tests", _ => test_type = TestType.Shell); - HasOption("i|insular|selfcontained", "Tests run sparkd and use their own config (if specified)", _ => self_contained = true); - HasAdditionalArguments(null, " [Test List]"); + // [Option("unit", HelpText="Run unit tests")] + // public bool unit { get; set; } + // [Option("system", HelpText="Run system tests")] + // public bool system { get; set; } + // [Option("shell", HelpText="Run shell tests")] + // public bool shell { get; set; } + + [Value(0, MetaName="TestType")] + public string type { get; set; } + + [Value(1, MetaName="Tests", HelpText="Test list")] + public IEnumerable tests { get; set; } + + } + public static class TestCommand { + static TestType test_type(string t) { + switch(t.ToLower()) { + case "unit": return TestType.Unit; + case "system": return TestType.System; + case "shell": return TestType.Shell; + } + return TestType.Unit; } - public override int Run(string[] tests) { - if (_list) { - list_tests(); + public static int Run(TestCommandOptions o) { + if (o.list) { + list_tests(test_type(o.type)); return 0; } - //Configuration.initialize_for_test(); - - if (_timeout == 0 && tests.Length == 1 && !self_contained) { + if (o.timeout == 0 && o.tests.Count() == 1 && !o.self_contained) { //System.Threading.Thread.Sleep(5000); using (var trr = new TestResultReceiver(true)) { - InProcessTestRunner.run_test_synchronously(tests[0], Enumerator, trr.get_reporter()); + InProcessTestRunner.run_test_synchronously(o.tests.First(), Enumerator, trr.get_reporter()); trr.print_summary(); return trr.ErrorCount; } } - else if (tests.Length > 0) { + else if (o.tests.Count() > 0) { return run_tests(Enumerator.match(tests)); } else { @@ -56,16 +67,16 @@ namespace sparkcmd { } } - Lazy _enumerator = new Lazy(() => new TestEnumerator("spark_test")); - TestEnumerator Enumerator => _enumerator.Value; + static Lazy _enumerator = new Lazy(() => new TestEnumerator("spark_test")); + static TestEnumerator Enumerator => _enumerator.Value; - int run_tests(IEnumerable tests) { + static int run_tests(IEnumerable tests) { var runner = new TestRunner(); // waits for all child processes to exit or time out return runner.run_tests(tests, _timeout, self_contained); } - void list_tests() { + static void list_tests(TestType test_type) { Console.WriteLine("All tests: "); foreach (var t in Enumerator.get_groups_of_type(test_type)) { ConsoleEx.WriteLine(t.name, ConsoleColor.Green); diff --git a/src/client/sparkcmd/Program.cs b/src/client/sparkcmd/Program.cs index 790308e27..14b3df9bb 100644 --- a/src/client/sparkcmd/Program.cs +++ b/src/client/sparkcmd/Program.cs @@ -2,16 +2,37 @@ using bts.api.core; using bts.api.synthetic; using bts.api.test; -using ManyConsole; using System; using System.Linq; +using CommandLine; +using bts; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; namespace sparkcmd { class Program { + + // static object option(Func run) {return run;} + // static Type argType(object arg) { + // return arg.GetType().GetGenericArguments().First(); + // } + // static int Run(Func,int> on_error, params object[] args) { + // var types = args.Select(argType).ToArray(); + // var parse = CommandLine.Parser.Default.GetType().GetMethod("ParseArguments", BindingFlags.Public).MakeGenericMethod(types); + // return 3; + // } static int Main(string[] args) { - mbts.config.Configuration.initialize(); - var commands = ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(Program)); - return ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out); + // note: this can be made to infer the options type list from the list of delegates + return CommandLine.Parser.Default + .ParseArguments(args) + .MapResult( + Configcheck.Run, + ScriptClientCommand.Run, + TailService.Run, + ServiceTest.Run, + SettingsManagerCommand.Run, + errors => 1); } } } \ No newline at end of file diff --git a/src/client/sparkcmd/sparkcmd.csproj b/src/client/sparkcmd/sparkcmd.csproj index 9046c4610..1a8c7f607 100644 --- a/src/client/sparkcmd/sparkcmd.csproj +++ b/src/client/sparkcmd/sparkcmd.csproj @@ -21,7 +21,8 @@ - - - + + + +