diff --git a/src/NuGetLicense/CommandLineOptionsParser.cs b/src/NuGetLicense/CommandLineOptionsParser.cs index 2ddc31f4..75887bcb 100644 --- a/src/NuGetLicense/CommandLineOptionsParser.cs +++ b/src/NuGetLicense/CommandLineOptionsParser.cs @@ -39,9 +39,14 @@ public string[] GetInputFiles(string? inputFile, string? inputJsonFile) return [inputFile]; } - return inputJsonFile != null - ? JsonSerializer.Deserialize(_fileSystem.File.ReadAllText(inputJsonFile))! - : throw new ArgumentException("Please provide an input file using --input or --json-input"); + if (inputJsonFile != null) + { + return JsonSerializer.Deserialize(_fileSystem.File.ReadAllText(inputJsonFile))!; + } + + // Defensive check: validation should already be done at command line parsing level, + // but throw exception if called directly or if validation is bypassed + throw new ArgumentException("Please provide an input file using --input or --json-input"); } public string[] GetAllowedLicenses(string? allowedLicenses) diff --git a/src/NuGetLicense/Program.cs b/src/NuGetLicense/Program.cs index 3395f23b..14b1953a 100644 --- a/src/NuGetLicense/Program.cs +++ b/src/NuGetLicense/Program.cs @@ -119,10 +119,24 @@ public static RootCommand CreateRootCommand() rootCommand.SetAction(async (parseResult, cancellationToken) => { + string? inputFile = parseResult.GetValue(inputFileOption); + string? inputJsonFile = parseResult.GetValue(inputJsonFileOption); + + // Check if mandatory parameters are provided + if (inputFile == null && inputJsonFile == null) + { + Console.Error.WriteLine("Error: Please provide an input file using --input or --json-input"); + Console.Error.WriteLine(); + // Show help by parsing and invoking with --help argument + ParseResult helpResult = rootCommand.Parse(new[] { "--help" }); + await helpResult.InvokeAsync(); + return 1; + } + var options = new CommandLineOptions { - InputFile = parseResult.GetValue(inputFileOption), - InputJsonFile = parseResult.GetValue(inputJsonFileOption), + InputFile = inputFile, + InputJsonFile = inputJsonFile, IncludeTransitive = parseResult.GetValue(includeTransitiveOption), AllowedLicenses = parseResult.GetValue(allowedLicensesOption), IgnoredPackages = parseResult.GetValue(ignoredPackagesOption),