Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from devklick/dev
Browse files Browse the repository at this point in the history
Release 1.2.1
  • Loading branch information
devklick authored May 7, 2024
2 parents 38c3a20 + c063bdc commit 36f708a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/GDMan.Cli/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/GDMan.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string>(cliArgs.Errors.Append(Environment.NewLine))));
_logger.LogInformation(cliArgs.HelpInfoString);
_logger.LogError(string.Join(Environment.NewLine, new List<string>(result.Errors.Append(""))));
_logger.LogInformation(result.HelpInfoString);
Environment.Exit(1);
}

Expand Down

0 comments on commit 36f708a

Please sign in to comment.