[WIP] Testing new CommandLineParser version

This commit is contained in:
temacdonald 2020-04-16 22:49:11 +12:00
parent 6bfb3edfba
commit 34337a0255
2 changed files with 25 additions and 29 deletions

View File

@ -33,7 +33,5 @@ namespace HeliosDisplayManagement
[Option("arguments", Required = false, HelpText = "(optional) Extra arguments to pass to the application/game when we're temporarily switching profile and running the application/game. Also can be used when creating a shortcut.")] [Option("arguments", Required = false, HelpText = "(optional) Extra arguments to pass to the application/game when we're temporarily switching profile and running the application/game. Also can be used when creating a shortcut.")]
public string ExecuteArguments { get; set; } public string ExecuteArguments { get; set; }
} }
} }

View File

@ -112,46 +112,37 @@ namespace HeliosDisplayManagement
private static int Main(string[] args) private static int Main(string[] args)
{ {
/*return Parser.Default.ParseArguments<CommandLineOptions>(args).MapResult( var parser = new Parser(config => config.HelpWriter = null);
options => RunOptions(options), var parserResult = parser.ParseArguments<CommandLineOptions>(args);
_ => 1);*/ parserResult.WithParsed<CommandLineOptions>(options => RunOptions(parserResult, options));
//var parser = new CommandLine.Parser(with => with.HelpWriter = null); parserResult.WithNotParsed<CommandLineOptions>(errs => DisplayHelp(parserResult, errs));
//var parserResult = parser.ParseArguments<CommandLineOptions, object>(args)
var result = Parser.Default.ParseArguments<CommandLineOptions>(args) return 0;
.MapResult(
options => RunOptions(options),
_ => 1);
return result;
} }
static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs) static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{ {
var helpText = HelpText.AutoBuild(result, h => var helpText = errs.ToList();
{
h.AdditionalNewLineAfterOption = false;
h.Heading = "Myapp 2.0.0-beta"; //change header
h.Copyright = "Copyright (c) 2019 Global.com"; //change copyright text
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
Console.WriteLine(helpText); Console.WriteLine(helpText);
} }
static int RunOptions(CommandLineOptions options) static void RunOptions(ParserResult<CommandLineOptions> parserResult, CommandLineOptions options)
{ {
// Validate combinations of Command line options // Validate combinations of Command line options
// If a profileId is supplied then we need an action other than None // If a profileId is supplied then we need an action other than None
if (options.Action == HeliosStartupAction.None && !String.IsNullOrEmpty(options.Profile)) if (options.Action == HeliosStartupAction.None && !String.IsNullOrEmpty(options.Profile))
{ {
Console.WriteLine("Error - If you supply a Profile ID or Name then you must also provide an Action."); Console.WriteLine("Error - If you supply a Profile ID or Name then you must also provide an Action.");
return 1; Environment.Exit(1);
} }
Console.WriteLine(CommandLine.Parser.Default.FormatCommandLine(options));
if (options.Action != HeliosStartupAction.None && String.IsNullOrEmpty(options.Profile)) if (options.Action != HeliosStartupAction.None && String.IsNullOrEmpty(options.Profile))
{ {
Console.WriteLine("Error - If you want to perform an Action then you must also provide a Profile ID or Name."); Console.WriteLine("Error - If you want to perform an Action then you must also provide a Profile ID or Name.");
return 1; Environment.Exit(2);
} }
Application.EnableVisualStyles(); Application.EnableVisualStyles();
@ -170,7 +161,7 @@ namespace HeliosDisplayManagement
// Show the user the profiles if they want to look // Show the user the profiles if they want to look
foreach (Profile aprofile in profiles) foreach (Profile aprofile in profiles)
{ {
Console.WriteLine($"Found Profile: {aprofile.Name} (ID:{aprofile.Id})"); Console.WriteLine($"Found Profile: '{aprofile.Name}' (ID:{aprofile.Id})");
} }
// Try and lookup the profile in the profiles' ID fields // Try and lookup the profile in the profiles' ID fields
var profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Id.Equals(options.Profile, StringComparison.InvariantCultureIgnoreCase)) : -1; var profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Id.Equals(options.Profile, StringComparison.InvariantCultureIgnoreCase)) : -1;
@ -178,15 +169,22 @@ namespace HeliosDisplayManagement
if (profileIndex == -1) if (profileIndex == -1)
{ {
// Try and lookup the profile in the profiles' Name fields // Try and lookup the profile in the profiles' Name fields
profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Name.Equals(options.Profile, StringComparison.InvariantCultureIgnoreCase)) : -1; profileIndex = profiles.Length > 0 ? Array.FindIndex(profiles, p => p.Name.StartsWith(options.Profile, StringComparison.InvariantCultureIgnoreCase)) : -1;
} }
// If the profileID still isn't there, then raise the alarm // If the profileID still isn't there, then raise the alarm
if (profileIndex == -1) if (profileIndex == -1)
{ {
Console.WriteLine($"Error - Couldn't find Profile Name or ID supplied via command line: \"{options.Profile}\". Please check the Profile Name or ID you supplied is correct."); if (!string.IsNullOrEmpty(options.Profile))
return 1; {
Console.WriteLine($"ERROR - Couldn't find Profile Name or ID supplied via command line: '{options.Profile}'. Please check the Profile Name or ID you supplied on the command line is correct.");
Environment.Exit(3);
}
} }
Console.WriteLine($"Using Profile: {profiles[profileIndex].Name} (ID:{profiles[profileIndex].Id})"); else
{
Console.WriteLine($"Using Profile: '{profiles[profileIndex].Name}' (ID:{profiles[profileIndex].Id})");
}
switch (options.Action) switch (options.Action)
@ -220,7 +218,7 @@ namespace HeliosDisplayManagement
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Error); MessageBoxIcon.Error);
} }
return 0;
} }
static void HandleParseError(IEnumerable<Error> errs) static void HandleParseError(IEnumerable<Error> errs)
{ {