Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/NuGetLicense/CommandLineOptionsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ public string[] GetInputFiles(string? inputFile, string? inputJsonFile)
return [inputFile];
}

return inputJsonFile != null
? JsonSerializer.Deserialize<string[]>(_fileSystem.File.ReadAllText(inputJsonFile))!
: throw new ArgumentException("Please provide an input file using --input or --json-input");
if (inputJsonFile != null)
{
return JsonSerializer.Deserialize<string[]>(_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)
Expand Down
18 changes: 16 additions & 2 deletions src/NuGetLicense/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading