Skip to content

Commit 5edcd3a

Browse files
Displaying which options are actually not null
1 parent 7ee5d9b commit 5edcd3a

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace ACE_Tests.Reworked;
2+
3+
public class ConfigurationTests
4+
{
5+
[Test]
6+
[Parallelizable(ParallelScope.Self)]
7+
public void WhenConfigurationIsPassedViaConfigurationFileAndInline_ItShouldReturnError()
8+
{
9+
var exitCode = Program.Main([
10+
"templates/bicep/availability-set.bicep",
11+
"cf70b558-b930-45e4-9048-ebcefb926adf",
12+
"arm-estimator-tests-rg",
13+
"--configuration-file",
14+
"templates/configuration/configuration.json",
15+
"--generate-json-output"
16+
]);
17+
18+
Assert.That(exitCode, Is.EqualTo(1));
19+
}
20+
}

ace/EstimateOptionsBinder.cs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.CommandLine;
44
using System.CommandLine.Binding;
55
using System.Text.Json;
6+
using System.Text.Json.Serialization;
67

78
namespace ACE;
89

@@ -148,34 +149,42 @@ protected override EstimateOptions GetBoundValue(BindingContext bindingContext)
148149

149150
private void ValidateProperUse(BindingContext bindingContext)
150151
{
151-
if(bindingContext.ParseResult.GetValueForOption(configurationFile) != null &&
152-
(bindingContext.ParseResult.GetValueForOption(mode) != null ||
153-
bindingContext.ParseResult.GetValueForOption(threshold) != null ||
154-
bindingContext.ParseResult.GetValueForOption(parameters) != null ||
155-
bindingContext.ParseResult.GetValueForOption(currency) != null ||
156-
bindingContext.ParseResult.GetValueForOption(generateJsonOutput) != null ||
157-
bindingContext.ParseResult.GetValueForOption(shouldBeSilent) != null ||
158-
bindingContext.ParseResult.GetValueForOption(stdout) != null ||
159-
bindingContext.ParseResult.GetValueForOption(disableDetails) != null ||
160-
bindingContext.ParseResult.GetValueForOption(jsonOutputFilename) != null ||
161-
bindingContext.ParseResult.GetValueForOption(generateHtmlOutput) != null ||
162-
bindingContext.ParseResult.GetValueForOption(dryRunOnly) != null ||
163-
bindingContext.ParseResult.GetValueForOption(htmlOutputFilename) != null ||
164-
bindingContext.ParseResult.GetValueForOption(outputFormat) != null ||
165-
bindingContext.ParseResult.GetValueForOption(disableCache) != null ||
166-
bindingContext.ParseResult.GetValueForOption(terraformExecutable) != null ||
167-
bindingContext.ParseResult.GetValueForOption(conversionRate) != null ||
168-
bindingContext.ParseResult.GetValueForOption(cacheHandler) != null ||
169-
bindingContext.ParseResult.GetValueForOption(cacheStorageAccountName) != null ||
170-
bindingContext.ParseResult.GetValueForOption(webhookUrl) != null ||
171-
bindingContext.ParseResult.GetValueForOption(logFile) != null ||
172-
bindingContext.ParseResult.GetValueForOption(optOutCheckingNewVersion) != null ||
173-
bindingContext.ParseResult.GetValueForOption(mockedRetailAPIResponsePaths) != null ||
174-
bindingContext.ParseResult.GetValueForOption(userGeneratedWhatIf) != null ||
175-
bindingContext.ParseResult.GetValueForOption(generateMarkdownOutput) != null ||
176-
bindingContext.ParseResult.GetValueForOption(markdownOutputFilename) != null))
152+
var options = new Dictionary<string, object?>
177153
{
178-
throw new Exception("Cannot use both --configuration-file and other options besides --webhook-authorization and --inline.");
179-
}
154+
{ mode.Name, bindingContext.ParseResult.GetValueForOption(mode) },
155+
{ threshold.Name, bindingContext.ParseResult.GetValueForOption(threshold) },
156+
{ parameters.Name, bindingContext.ParseResult.GetValueForOption(parameters) },
157+
{ currency.Name, bindingContext.ParseResult.GetValueForOption(currency) },
158+
{ generateJsonOutput.Name, bindingContext.ParseResult.GetValueForOption(generateJsonOutput) },
159+
{ shouldBeSilent.Name, bindingContext.ParseResult.GetValueForOption(shouldBeSilent) },
160+
{ stdout.Name, bindingContext.ParseResult.GetValueForOption(stdout) },
161+
{ disableDetails.Name, bindingContext.ParseResult.GetValueForOption(disableDetails) },
162+
{ jsonOutputFilename.Name, bindingContext.ParseResult.GetValueForOption(jsonOutputFilename) },
163+
{ generateHtmlOutput.Name, bindingContext.ParseResult.GetValueForOption(generateHtmlOutput) },
164+
{ outputFormat.Name, bindingContext.ParseResult.GetValueForOption(outputFormat) },
165+
{ disableCache.Name, bindingContext.ParseResult.GetValueForOption(disableCache) },
166+
{ terraformExecutable.Name, bindingContext.ParseResult.GetValueForOption(terraformExecutable) },
167+
{ conversionRate.Name, bindingContext.ParseResult.GetValueForOption(conversionRate) },
168+
{ cacheHandler.Name, bindingContext.ParseResult.GetValueForOption(cacheHandler) },
169+
{ cacheStorageAccountName.Name, bindingContext.ParseResult.GetValueForOption(cacheStorageAccountName) },
170+
{ webhookUrl.Name, bindingContext.ParseResult.GetValueForOption(webhookUrl) },
171+
{ logFile.Name, bindingContext.ParseResult.GetValueForOption(logFile) },
172+
{ optOutCheckingNewVersion.Name, bindingContext.ParseResult.GetValueForOption(optOutCheckingNewVersion) },
173+
{ mockedRetailAPIResponsePaths.Name, bindingContext.ParseResult.GetValueForOption(mockedRetailAPIResponsePaths) },
174+
{ userGeneratedWhatIf.Name, bindingContext.ParseResult.GetValueForOption(userGeneratedWhatIf) },
175+
{ generateMarkdownOutput.Name, bindingContext.ParseResult.GetValueForOption(generateMarkdownOutput) },
176+
{ markdownOutputFilename.Name, bindingContext.ParseResult.GetValueForOption(markdownOutputFilename) },
177+
};
178+
179+
if (bindingContext.ParseResult.GetValueForOption(configurationFile) == null) return;
180+
181+
var optionsWithNonNullValue = options.Where(o => o.Value != null).ToArray();
182+
if (optionsWithNonNullValue.Length == 0) return;
183+
184+
var parsedNonNullableOptions = string.Join(", ", optionsWithNonNullValue.Select(o => $"[{o.Key}]:[{o.Value}]"));
185+
186+
throw new Exception(
187+
$"Cannot use both --configuration-file and other options besides --webhook-authorization and --inline. Those options are: {parsedNonNullableOptions}");
188+
180189
}
181190
}

0 commit comments

Comments
 (0)