From 2a4cf727e7e52b13049d2e3608bd386466b67354 Mon Sep 17 00:00:00 2001 From: devklick Date: Tue, 7 May 2024 19:21:01 +0100 Subject: [PATCH] fix: fixes #11 - sequence contains no elements when no args provided --- src/GDMan.Cli/Parsing/Parser.cs | 8 ++++++-- src/GDMan.Cli/Program.cs | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/GDMan.Cli/Parsing/Parser.cs b/src/GDMan.Cli/Parsing/Parser.cs index 0864c7b..6aaa4c1 100644 --- a/src/GDMan.Cli/Parsing/Parser.cs +++ b/src/GDMan.Cli/Parsing/Parser.cs @@ -21,10 +21,14 @@ public ParseResult Parse(params string[] args) var result = new ParseResult(); - var arg1 = args.First(); + var arg1 = args.FirstOrDefault(); if (!TryInitCommandOptions(arg1, out var options, out var helpInfo)) { - result.Errors.Add($"{arg1} is not a known command"); + if (!string.IsNullOrEmpty(arg1)) + { + result.Errors.Add($"{arg1} is not a known command"); + } + result.HelpInfo = helpInfo; result.RequiresHelp = true; return result; diff --git a/src/GDMan.Cli/Program.cs b/src/GDMan.Cli/Program.cs index b9fbda8..5159f2f 100644 --- a/src/GDMan.Cli/Program.cs +++ b/src/GDMan.Cli/Program.cs @@ -47,24 +47,24 @@ static async Task Main(string[] args) } } - private static async Task HandleParseResult(ParseResult cliArgs) + private static async Task HandleParseResult(ParseResult result) { - if (cliArgs.RequiresHelp) + if (result.HasError) { - HandleHelp(cliArgs); + HandleError(result); } - if (cliArgs.HasError) + if (result.RequiresHelp) { - HandleError(cliArgs); + HandleHelp(result); } - if (cliArgs.Options == null) + if (result.Options == null) { throw new NullReferenceException("Expected Options to have a value but found null"); } - await RunAsync(cliArgs.Options); + await RunAsync(result.Options); } private static Task RunAsync(BaseOptions command) => command switch @@ -138,10 +138,10 @@ private static void HandleHelp(ParseResult cliArgs) } [DoesNotReturn] - private static void HandleError(ParseResult cliArgs) + private static void HandleError(ParseResult result) { - _logger.LogError(string.Join(Environment.NewLine, new List(cliArgs.Errors.Append(Environment.NewLine)))); - _logger.LogInformation(cliArgs.HelpInfoString); + _logger.LogError(string.Join(Environment.NewLine, new List(result.Errors.Append("")))); + _logger.LogInformation(result.HelpInfoString); Environment.Exit(1); }