Skip to content

Commit

Permalink
(GH-473) Correct exit code when help is requested
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Cubix651 authored and ferventcoder committed Mar 19, 2017
1 parent 5d25060 commit b47ce15
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/chocolatey.console/Program.cs
Expand Up @@ -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()));
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Expand Up @@ -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;
Expand Down
Expand Up @@ -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();
}
}
}
}
Expand Up @@ -391,6 +391,7 @@ private static void set_global_options(IList<string> args, ChocolateyConfigurati
{
// save help for next menu
config.HelpRequested = false;
config.UnsuccessfulParsing = false;
}
},
() => { },
Expand Down
Expand Up @@ -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; }
/// <summary>
/// Gets or sets a value indicating whether parsing was successful (everything parsed) or not.
/// </summary>
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
Expand Down
Expand Up @@ -85,6 +85,7 @@ private static IConsole Console
catch (OptionException)
{
show_help(_optionSet, helpMessage);
configuration.UnsuccessfulParsing = true;
}

// the command argument
Expand All @@ -102,6 +103,7 @@ private static IConsole Console
else
{
configuration.HelpRequested = true;
configuration.UnsuccessfulParsing = true;
}
}

Expand Down
Expand Up @@ -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 =>
{
Expand All @@ -89,6 +89,7 @@ public void run(string[] args, ChocolateyConfiguration config, Container contain
if (unparsedArg.StartsWith("-") || unparsedArg.StartsWith("/"))
{
config.HelpRequested = true;
config.UnsuccessfulParsing = true;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Expand Up @@ -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();
Expand Down

0 comments on commit b47ce15

Please sign in to comment.