Skip to content

Commit

Permalink
(chocolatey#2867) Add --online option to all commands
Browse files Browse the repository at this point in the history
This commit adds the --online option to all Chocolatey commands, to
allow opening of the online help documentation for the command that is
being executed.

For example, running:

choco install -h

will output the help documentation to the console, but running:

choco install -h --online

will not output anything to the command line, and instead open the
default browser to the help documentation for the command.
  • Loading branch information
gep13 committed May 3, 2023
1 parent 97792a7 commit f966315
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function script:chocoCmdOperations($commands, $command, $filter, $currentArgumen
$script:chocoCommands = @('-?','search','list','info','install','outdated','upgrade','uninstall','new','pack','push','-h','--help','pin','source','config','feature','apikey','export','help','template','--version')

# ensure these all have a space to start, or they will cause issues
$allcommands = " --debug --verbose --trace --noop --help -? --accept-license --confirm --limit-output --no-progress --log-file='' --execution-timeout='' --cache-location='' --proxy='' --proxy-user='' --proxy-password='' --proxy-bypass-list='' --proxy-bypass-on-local --force --no-color --skip-compatibility-checks"
$allcommands = " --debug --verbose --trace --noop --help -? --online --accept-license --confirm --limit-output --no-progress --log-file='' --execution-timeout='' --cache-location='' --proxy='' --proxy-user='' --proxy-password='' --proxy-bypass-list='' --proxy-bypass-on-local --force --no-color --skip-compatibility-checks"

$commandOptions = @{
list = "--id-only --pre --exact --by-id-only --id-starts-with --detailed --prerelease --include-programs --page='' --page-size=''"
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ private static ChocolateyConfiguration baseline_configuration()
config.ForceDependencies = false;
config.ForceX86 = false;
config.HelpRequested = false;
config.ShowOnlineHelp = false;
config.UnsuccessfulParsing = false;
config.UnsuccessfulParsing = false;
config.IgnoreDependencies = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public void Should_show_help_menu_when_help_is_requested()
because();

config.HelpRequested.ShouldBeTrue();
config.ShowOnlineHelp.ShouldBeFalse();
}

[Fact]
public void Should_show_online_help_menu_when_help_is_requested()
{
args.Add("-h");
args.Add("--online");

because();

config.HelpRequested.ShouldBeTrue();
config.ShowOnlineHelp.ShouldBeTrue();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,11 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
{
if (!string.IsNullOrWhiteSpace(config.CommandName))
{
// save help for next menu
// The setOptions lambda above is called twice, once to set the command name, and then to execute the command with all options.
// To ensure correct option of the help options, we need to reset them to false in the first execution, to then have them parsed correctly in the second
// iteration.
config.HelpRequested = false;
config.ShowOnlineHelp = false;
config.UnsuccessfulParsing = false;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ private void AppendOutput(StringBuilder propertyValues, string append)

// top level commands

public bool ShowOnlineHelp { get; set; }
public bool Debug { get; set; }
public bool Verbose { get; set; }
public bool Trace { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.app.configuration
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using adapters;
Expand Down Expand Up @@ -75,8 +76,11 @@ public static void ParseArgumentsAndUpdateConfiguration(ICollection<string> args
{
_optionSet
.Add("?|help|h",
"Prints out the help menu.",
option => configuration.HelpRequested = option != null);
"Prints out the help menu.",
option => configuration.HelpRequested = option != null)
.Add("online",
"Online - Open help for specified command in default browser application. This option only works when used in combintation with the -?/--help/-h option.",
option => configuration.ShowOnlineHelp = option != null);
}

if (setOptions != null)
Expand Down Expand Up @@ -120,10 +124,38 @@ public static void ParseArgumentsAndUpdateConfiguration(ICollection<string> args

if (configuration.HelpRequested)
{
if (configuration.ShowOnlineHelp)
{
if (string.IsNullOrWhiteSpace(configuration.CommandName))
{
"chocolatey".Log().Warn("Unable to open command help as no command name has been provided.");
return;
}

var targetAddress = "https://ch0.co/c/{0}".FormatWith(configuration.CommandName.ToLowerSafe());

try
{
System.Diagnostics.Process.Start(new ProcessStartInfo(targetAddress) { UseShellExecute = true });
}
catch (Exception)
{
"chocolatey".Log().Warn("There was an error while attempting to open the following URL: {0} in the default browser.".FormatWith(targetAddress));
}

return;
}

ShowHelp(_optionSet, helpMessage);
}
else
{
// Only show this warning once
if (configuration.ShowOnlineHelp)
{
"chocolatey".Log().Warn("The --online option has been used, without the corresponding -?/--help/-h option. Command execution will be completed without invoking help.");
}

if (validateConfiguration != null)
{
validateConfiguration();
Expand Down

0 comments on commit f966315

Please # to comment.