forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationParser.cs
130 lines (114 loc) · 5.4 KB
/
ApplicationParser.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using McMaster.Extensions.CommandLineUtils;
namespace QuantConnect.Configuration
{
/// <summary>
/// Command Line application parser
/// </summary>
public static class ApplicationParser
{
/// <summary>
/// This function will parse args based on options and will show application name, version, help
/// </summary>
/// <param name="applicationName">The application name to be shown</param>
/// <param name="applicationDescription">The application description to be shown</param>
/// <param name="applicationHelpText">The application help text</param>
/// <param name="args">The command line arguments</param>
/// <param name="options">The applications command line available options</param>
/// <param name="noArgsShowHelp">To show help when no command line arguments were provided</param>
/// <returns>The user provided options. Key is option name</returns>
public static Dictionary<string, object> Parse(string applicationName, string applicationDescription, string applicationHelpText,
string[] args, List<CommandLineOption> options, bool noArgsShowHelp = false)
{
var application = new CommandLineApplication
{
Name = applicationName,
Description = applicationDescription,
ExtendedHelpText = applicationHelpText
};
application.HelpOption("-?|-h|--help");
// This is a helper/shortcut method to display version info - it is creating a regular Option, with some defaults.
// The default help text is "Show version Information"
application.VersionOption("-v|-V|--version",
() =>
$"Version {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
var optionsObject = new Dictionary<string, object>();
var listOfOptions = new List<CommandOption>();
foreach (var option in options)
{
listOfOptions.Add(application.Option($"--{option.Name}", option.Description, option.Type));
}
application.OnExecute(() =>
{
foreach (var commandOption in listOfOptions.Where(option => option.HasValue()))
{
var optionKey = commandOption.Template.Replace("--", "");
var matchingOption = options.Find(o => o.Name == optionKey);
switch (matchingOption.Type)
{
// Booleans
case CommandOptionType.NoValue:
optionsObject[optionKey] = true;
break;
// Strings and numbers
case CommandOptionType.SingleValue:
optionsObject[optionKey] = commandOption.Value();
break;
// Parsing nested objects
case CommandOptionType.MultipleValue:
var keyValuePairs = commandOption.Value().Split(',');
var subDictionary = new Dictionary<string, string>();
foreach (var keyValuePair in keyValuePairs)
{
var subKeys = keyValuePair.Split(':');
subDictionary[subKeys[0]] = subKeys.Length > 1 ? subKeys[1] : "";
}
optionsObject[optionKey] = subDictionary;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return 0;
});
application.Execute(args);
if (noArgsShowHelp && args.Length == 0)
{
application.ShowHelp();
}
return optionsObject;
}
public static void PrintMessageAndExit(int exitCode = 0, string message = "")
{
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine("\n" + message);
}
Console.WriteLine("\nUse the '--help' parameter for more information");
Console.WriteLine("Press any key to quit");
Console.ReadLine();
Environment.Exit(exitCode);
}
public static string GetParameterOrExit(IReadOnlyDictionary<string, object> optionsObject, string parameter)
{
if (!optionsObject.ContainsKey(parameter))
{
PrintMessageAndExit(1, "ERROR: REQUIRED parameter --" + parameter + "= is missing");
}
return optionsObject[parameter].ToString();
}
public static string GetParameterOrDefault(IReadOnlyDictionary<string, object> optionsObject, string parameter, string defaultValue)
{
object value;
if (!optionsObject.TryGetValue(parameter, out value))
{
Console.WriteLine($"'{parameter}' was not specified. Using default value: '{defaultValue}'");
return defaultValue;
}
return value.ToString();
}
}
}