Add a command line parser to sparkcmd

This commit is contained in:
Eric Harding committed 2018-08-16 13:28:29 -04:00
1 parent 659e083f6f
commit 7cbdbcc790
6 files changed
+162 -125

No files matched your search

@@ -0,0 +1,15 @@
using ManyConsole;
using System.Linq;
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());
return 0;
}
}
}
@@ -6,8 +6,37 @@ using System.Reflection;
using System.Threading.Tasks;
using System.Reactive.Linq;
using System.Reactive;
using ManyConsole;
using System.Linq;
namespace sparkcmd {
public class ScriptClientCommand : ConsoleCommand {
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) {
ScriptClient.runserver();
}
else {
var host = string.IsNullOrEmpty(_host) ? args.First() : _host;
if (string.IsNullOrEmpty(_logfile) && File.Exists(_logfile)) {
ScriptClient.start(host, File.OpenRead(_logfile));
}
else {
ScriptClient.start(host, Console.OpenStandardInput());
}
}
return 0;
}
}
internal static class ScriptClient {
internal static void start(string qualifier, Stream stream) {
using (var ctx = new ServiceContext()) {
@@ -82,10 +111,9 @@ namespace sparkcmd {
public static void runserver() {
// for debugging
using (var ctx = new ServiceContext()) {
var assembly = Assembly.GetAssembly(typeof(mbts.ui.shell.App));
var assembly = Assembly.Load("spark.exe");
var stype = assembly.GetType("bts.api.diagnostic.ShellDiagnosticsServiceServer");
var server = Activator.CreateInstance(stype, ctx);
//ShellDiagnosticsServiceServer server = new bts.api.diagnostic.ShellDiagnosticsServiceServer(ctx);
while (true) System.Threading.Thread.Sleep(1000);
}
}
@@ -0,0 +1,86 @@
using bts.api;
using bts.api.core;
using bts.api.synthetic;
using ManyConsole;
using System;
namespace bts {
public class ServiceTest : ConsoleCommand {
bool _synthetic;
bool _user;
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);
}
public override int Run(string[] remainingArguments) {
if (_synthetic) {
synthetic_test();
}
if (_user) {
user_test();
}
return 0;
}
private static void synthetic_test() {
using (var ctx = new ServiceContext()) {
var security = new Security {
Symbol = "ES",
Exchange = Venue.Bts,
Currency = Currency.Usd,
Synthetic = new SyntheticContract {
Name = "Synthetic1",
PriceCalc = SyntheticContract.Types.PriceCalc.Ratio,
Legs = {
new ContractLeg {
Security = new Security {
Id = 2,
Symbol = "ES",
// ESH8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 3,
ExpirationDay = 16
}
}
},
new ContractLeg {
Security = new Security {
Id = 5,
Symbol = "ES",
// ESU8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 9,
ExpirationDay = 21
}
}
}
}
}
};
var service = ctx.locate<SyntheticSecurityService>();
try {
var result = service.create_synthetic(security);
if (result.Error == CreateResult.Types.Error.Ok) {
Console.WriteLine(result.Key);
}
}
catch (ApiException e) {
Console.WriteLine(e.Message);
}
}
}
private static void user_test() {
var ctx = new ServiceContext();
var s = ctx.locate<api.user.UserService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
}
}
Whitespace-only changes.
+6 -102
View File
@@ -2,115 +2,19 @@
using bts.api.core;
using bts.api.synthetic;
using bts.api.test;
using ManyConsole;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace sparkcmd
{
class Program
{
namespace sparkcmd {
class Program {
static void Main(string[] args) {
if (args.FirstOrDefault() == "configcheck") {
mbts.config.Configuration.configcheck(args.Length - 1, args.Skip(1).ToArray());
return;
}
mbts.config.Configuration.initialize();
if (args.FirstOrDefault() == "usertest") {
user_test();
}
else if (args.FirstOrDefault() == "synthetic") {
synthetic_test();
}
else if (args.FirstOrDefault() == "hello") {
hello_test();
}
else if (args.FirstOrDefault() == "scriptserver") {
ScriptClient.runserver();
}
else if (args.FirstOrDefault() == "script") {
var qualifier = args.Skip(1).First();
var filename = args.Skip(2).FirstOrDefault();
if (!string.IsNullOrEmpty(filename)) {
Console.WriteLine("filename: " + filename);
// todo: run forever
ScriptClient.start(qualifier, System.IO.File.OpenRead(filename));
}
else {
ScriptClient.start(qualifier, Console.OpenStandardInput());
}
}
var commands = ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(Program));
ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out);
return;
}
private static void hello_test() {
using (var ctx = new ServiceContext()) {
var hello = ctx.locate<HelloService>();
var a = hello.say_hello(new HelloRequest { Name = "Bob" });
Console.WriteLine(a.Message);
}
}
private static void synthetic_test() {
using (var ctx = new ServiceContext()) {
var security = new Security {
Symbol = "ES",
Exchange = Venue.Bts,
Currency = Currency.Usd,
Synthetic = new SyntheticContract {
Name = "Synthetic1",
PriceCalc = SyntheticContract.Types.PriceCalc.Ratio,
Legs = {
new ContractLeg {
Security = new Security {
Id = 2,
Symbol = "ES",
// ESH8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 3,
ExpirationDay = 16
}
}
},
new ContractLeg {
Security = new Security {
Id = 5,
Symbol = "ES",
// ESU8
Future = new FutureContract {
ExpirationYear = 2018,
ExpirationMonth = 9,
ExpirationDay = 21
}
}
}
}
}
};
var service = ctx.locate<SyntheticSecurityService>();
try {
var result = service.create_synthetic(security);
if (result.Error == CreateResult.Types.Error.Ok) {
Console.WriteLine(result.Key);
}
}
catch (ApiException e) {
Console.WriteLine(e.Message);
}
}
}
private static void user_test() {
var ctx = new bts.api.core.ServiceContext();
var s = ctx.locate<bts.api.user.UserService>();
var r = new bts.api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
}
}
+25 -21
View File
@@ -1,22 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net471</TargetFrameworks>
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
<Authors>Blue Trading Systems</Authors>
<Company>Blue Trading Systems</Company>
<Product>Spark</Product>
<Platforms>x64</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<Version>1.0.0</Version>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net471</TargetFrameworks>
<!--<TargetFrameworks>net471;netcoreapp2.1</TargetFrameworks>-->
<RuntimeIdentifiers>win7-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
<Authors>Blue Trading Systems</Authors>
<Company>Blue Trading Systems</Company>
<Product>Spark</Product>
<Platforms>x64</Platforms>
<Version>1.0.0</Version>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../spark_api/spark_api.csproj" />
<!--<ProjectReference Include="..\spark\Spark.csproj" />-->
<ProjectReference Include="..\spark_core\spark_core.csproj" />
<!--<Proto3 Include="**/*.proto" />-->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../spark_api/spark_api.csproj" />
<ProjectReference Include="..\spark\Spark.csproj" />
<ProjectReference Include="..\spark_core\spark_core.csproj" />
<Proto3 Include="**/*.proto" />
</ItemGroup>
</Project>
<PackageReference Include="ManyConsole" Version="1.0.0.4" />
</ItemGroup>
</Project>