Files
quantconnect--lean/Configuration/ApplicationParser.cs
T
Colton Sellers 1c3d849ad5 Fix Warnings V2 (#5436)
* Reconcile duplicated code

* Add License header

* CS0219 Fixes: Value assigned, but never used

* CA1507: Use nameof in place of string literals

* CS0108 : Hides Inherited Member; Use new keyword to overwrite formally

* CS0114: Hides inherited member; use override keyword

* CS0168: Variable is declared but never used

* Tests CS1062; using obsolete implicit Symbol -> String; fix via .ToString()

* CS0472: Non Nullable Obj getting Null Checked

* CS0067 Member not used; ignore all cases for future use

* CS00162 : Unreachable code; either removed or ignored for debugging and test cases

* CS0169 Remove non-used fields; ignore those that may be used in future

* CS0414; Field is assigned but never used.

* CS0618; Obsolete properties and members; Only fixes simple ones, rest will have to broken up

* CS0649; Field never assigned too

* CS0659 & CS0661 ; Overwrite operators and equals but not hashcode; I don't really override it but just call base

* Small comment fix

* Cleanup pragma statement
2021-04-02 11:20:01 -07:00

96 lines
4.2 KiB
C#

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()))
{
#pragma warning disable CS0618 // Type or member is obsolete; Works fine, will use until replacement is required
var optionKey = commandOption.Template.Replace("--", "");
#pragma warning restore CS0618
var matchingOption = options.Find(o => o.Name == optionKey);
switch (matchingOption.Type)
{
// Booleans, string and numbers
case CommandOptionType.NoValue:
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;
}
}
}