-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCmdLine.cs
More file actions
94 lines (74 loc) · 2.83 KB
/
Copy pathCmdLine.cs
File metadata and controls
94 lines (74 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;
#if NET48
using System.Windows.Forms;
#endif
namespace QuantBox.APIProvider
{
public class Options
{
[Option('f', "file", Required = false, HelpText = "策略项目文件")]
public string file { get; set; }
[Option('i', "id", Required = false, HelpText = "实例数字ID,用于收到剪贴板事件时过滤使用")]
public int id { get; set; }
[Option('r', "run", Required = false, Default = false, HelpText = "运行策略")]
public bool run { get; set; }
[Option('s', "stop", Required = false, Default = false, HelpText = "停止策略")]
public bool stop { get; set; }
[Option('e', "exit", Required = false, Default = false, HelpText = "退出程序")]
public bool exit { get; set; }
}
public class CmdLine
{
private int id;
public void ParseForStart(ProviderHost host)
{
// 通过命令行启动策略
//cd "C:\Program Files\SmartQuant Ltd\OpenQuant 2014"
//C:
//start OpenQuant.exe --file="D:\Users\Kan\Documents\OpenQuant 2014\Solutions\SMACrossover\SMACrossover.sln" --id=100 --run
var args = Environment.GetCommandLineArgs();
var text = Environment.CommandLine;
Console.WriteLine($"命令行: {text}");
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(opts => RunOptions(opts, host))
.WithNotParsed<Options>((errs) => HandleParseError(errs));
}
public void ParseForStop(ProviderHost host)
{
#if NET48
//echo --id=100 --stop --exit | clip
IDataObject ido = Clipboard.GetDataObject();
if (!ido.GetDataPresent(DataFormats.Text))
return;
var text = ido.GetData(DataFormats.Text) as string;
Console.WriteLine($"剪贴板: {text}");
CommandLine.Parser.Default.ParseArguments<Options>(text.Split(' '))
.WithParsed<Options>(opts => ExitOptions(opts, host))
.WithNotParsed<Options>((errs) => HandleParseError(errs));
#endif
}
void RunOptions(Options opts, ProviderHost host)
{
// 记下ID,退出时使用
id = opts.id;
if (string.IsNullOrEmpty(opts.file))
return;
if (!(new FileInfo(opts.file).Exists))
return;
host.Solution_Start_Thread(opts);
}
void ExitOptions(Options opts, ProviderHost host)
{
if (id != opts.id)
return;
host.Solution_Stop_Thread(opts);
}
void HandleParseError(IEnumerable<Error> errs)
{
// Application.Exit();
}
}
}