Skip to content

Commit

Permalink
(chocolateyGH-586) Ignore invalid options/switches by default
Browse files Browse the repository at this point in the history
Much like the way PowerShell ignores switches it doesn't understand,
allow doing that with swtiches and options that choco doesn't
understand. This allows passing things that would be created in future
versions and not having older versions break when attempting to use
those switches. Also allow for the feature to be turned off.
  • Loading branch information
ferventcoder committed Apr 10, 2016
1 parent bf97195 commit cd0169f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static class Features
public static readonly string LogEnvironmentValues = "logEnvironmentValues";
public static readonly string VirusCheck = "virusCheck";
public static readonly string FailOnInvalidOrMissingLicense = "failOnInvalidOrMissingLicense";
public static readonly string IgnoreInvalidOptionsSwitches = "ignoreInvalidOptionsSwitches";
}

public static class Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile
config.Features.LogEnvironmentValues = set_feature_flag(ApplicationParameters.Features.LogEnvironmentValues, configFileSettings, defaultEnabled: false, description: "Log Environment Values - will log values of environment before and after install (could disclose sensitive data).");
config.Features.VirusCheck = set_feature_flag(ApplicationParameters.Features.VirusCheck, configFileSettings, defaultEnabled: false, description: "Virus Check [licensed versions only] - perform virus checking on downloaded files.");
config.Features.FailOnInvalidOrMissingLicense = set_feature_flag(ApplicationParameters.Features.FailOnInvalidOrMissingLicense, configFileSettings, defaultEnabled: false, description: "Fail On Invalid Or Missing License - allows knowing when a license is expired or not applied to a machine.");
config.Features.IgnoreInvalidOptionsSwitches = set_feature_flag(ApplicationParameters.Features.IgnoreInvalidOptionsSwitches, configFileSettings, defaultEnabled: true, description: "Ignore Invalid Options/Switches - If a switch or option is passed that is not recognized, should choco fail?");
config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public sealed class FeaturesConfiguration
public bool LogEnvironmentValues { get; set; }
public bool VirusCheck { get; set; }
public bool FailOnInvalidOrMissingLicense { get; set; }
public bool IgnoreInvalidOptionsSwitches { get; set; }
}

//todo: retrofit other command configs this way
Expand Down
13 changes: 8 additions & 5 deletions src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ public void run(string[] args, ChocolateyConfiguration config, Container contain

command.handle_additional_argument_parsing(unparsedArgs, config);

// all options / switches should be parsed,
// so show help menu if there are any left
foreach (var unparsedArg in unparsedArgs.or_empty_list_if_null())
if (!config.Features.IgnoreInvalidOptionsSwitches)
{
if (unparsedArg.StartsWith("-") || unparsedArg.StartsWith("/"))
// all options / switches should be parsed,
// so show help menu if there are any left
foreach (var unparsedArg in unparsedArgs.or_empty_list_if_null())
{
config.HelpRequested = true;
if (unparsedArg.StartsWith("-") || unparsedArg.StartsWith("/"))
{
config.HelpRequested = true;
}
}
}
},
Expand Down

0 comments on commit cd0169f

Please sign in to comment.