From b47ce15c949ad72653307cc53512214c1527ea13 Mon Sep 17 00:00:00 2001 From: Jakub Cislo Date: Sat, 14 Jan 2017 18:30:22 +0100 Subject: [PATCH] (GH-473) Correct exit code when help is requested There are two cases when choco shows help message: when it is explicitly requested or when parsing fails. In first scenario choco should return 0 exit code, in second - non-zero value. This commit introduces new property UnsuccessfulParsing in ChocolateyConfiguration and using this property it fixes bug with incorrect exit code. --- src/chocolatey.console/Program.cs | 4 ++-- src/chocolatey.tests.integration/Scenario.cs | 1 + .../configuration/ConfigurationOptionsSpec.cs | 20 +++++++++++++++++++ .../builders/ConfigurationBuilder.cs | 1 + .../configuration/ChocolateyConfiguration.cs | 4 ++++ .../configuration/ConfigurationOptions.cs | 2 ++ .../runners/ConsoleApplication.cs | 3 ++- .../runners/GenericRunner.cs | 4 ++-- 8 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/chocolatey.console/Program.cs b/src/chocolatey.console/Program.cs index 775328a960..537ddf9b31 100644 --- a/src/chocolatey.console/Program.cs +++ b/src/chocolatey.console/Program.cs @@ -105,10 +105,10 @@ private static void Main(string[] args) } } - if (config.HelpRequested) + if (config.HelpRequested || config.UnsuccessfulParsing) { pause_execution_if_debug(); - Environment.Exit(-1); + Environment.Exit(config.UnsuccessfulParsing? 1 : 0); } Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); diff --git a/src/chocolatey.tests.integration/Scenario.cs b/src/chocolatey.tests.integration/Scenario.cs index 29ab41eeed..81a0d7481c 100644 --- a/src/chocolatey.tests.integration/Scenario.cs +++ b/src/chocolatey.tests.integration/Scenario.cs @@ -142,6 +142,7 @@ private static ChocolateyConfiguration baseline_configuration() config.ForceDependencies = false; config.ForceX86 = false; config.HelpRequested = false; + config.UnsuccessfulParsing = false; config.IgnoreDependencies = false; config.InstallArguments = string.Empty; config.Noop = false; diff --git a/src/chocolatey.tests/infrastructure.app/configuration/ConfigurationOptionsSpec.cs b/src/chocolatey.tests/infrastructure.app/configuration/ConfigurationOptionsSpec.cs index db1aa1e9ae..336ba562a7 100644 --- a/src/chocolatey.tests/infrastructure.app/configuration/ConfigurationOptionsSpec.cs +++ b/src/chocolatey.tests/infrastructure.app/configuration/ConfigurationOptionsSpec.cs @@ -312,6 +312,26 @@ public void should_show_help_menu_when_passing_bundled_options_that_do_not_exist config.Debug.ShouldBeFalse(); helpMessageContents.ToString().ShouldNotBeEmpty(); } + + [Fact] + public void should_successfully_parse_help_option() + { + args.Add("-h"); + + because(); + + config.UnsuccessfulParsing.ShouldBeFalse(); + } + + [Fact] + public void should_not_parse_unknown_option() + { + args.Add("-unknown"); + + because(); + + config.UnsuccessfulParsing.ShouldBeTrue(); + } } } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 7ec66f7f26..8fc5dce53b 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -391,6 +391,7 @@ private static void set_global_options(IList args, ChocolateyConfigurati { // save help for next menu config.HelpRequested = false; + config.UnsuccessfulParsing = false; } }, () => { }, diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index b558854dd5..0cbf5c0ff7 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -157,6 +157,10 @@ private void append_output(StringBuilder propertyValues, string append) public bool Force { get; set; } public bool Noop { get; set; } public bool HelpRequested { get; set; } + /// + /// Gets or sets a value indicating whether parsing was successful (everything parsed) or not. + /// + public bool UnsuccessfulParsing { get; set; } // TODO: Should look into using mutually exclusive output levels - Debug, Info (Regular), Error (Quiet) // Verbose and Important are not part of the levels at all diff --git a/src/chocolatey/infrastructure.app/configuration/ConfigurationOptions.cs b/src/chocolatey/infrastructure.app/configuration/ConfigurationOptions.cs index e55cd48073..c7dca0b888 100644 --- a/src/chocolatey/infrastructure.app/configuration/ConfigurationOptions.cs +++ b/src/chocolatey/infrastructure.app/configuration/ConfigurationOptions.cs @@ -85,6 +85,7 @@ private static IConsole Console catch (OptionException) { show_help(_optionSet, helpMessage); + configuration.UnsuccessfulParsing = true; } // the command argument @@ -102,6 +103,7 @@ private static IConsole Console else { configuration.HelpRequested = true; + configuration.UnsuccessfulParsing = true; } } diff --git a/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs b/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs index 5f4b7dd47a..70277c8cd2 100644 --- a/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs +++ b/src/chocolatey/infrastructure.app/runners/ConsoleApplication.cs @@ -64,7 +64,7 @@ public void run(string[] args, ChocolateyConfiguration config, Container contain commandArgs.Add(arg); } - + var runner = new GenericRunner(); runner.run(config, container, isConsole: true, parseArgs: command => { @@ -89,6 +89,7 @@ public void run(string[] args, ChocolateyConfiguration config, Container contain if (unparsedArg.StartsWith("-") || unparsedArg.StartsWith("/")) { config.HelpRequested = true; + config.UnsuccessfulParsing = true; } } } diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index d14e042b98..37b9a617db 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -75,13 +75,13 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe this.Log().Debug(() => "Configuration: {0}".format_with(config.ToString())); - if (isConsole && config.HelpRequested) + if (isConsole && (config.HelpRequested || config.UnsuccessfulParsing)) { #if DEBUG Console.WriteLine("Press enter to continue..."); Console.ReadKey(); #endif - Environment.Exit(1); + Environment.Exit(config.UnsuccessfulParsing? 1 : 0); } var token = Assembly.GetExecutingAssembly().get_public_key_token();