Skip to content

Commit 025cd30

Browse files
Shows options from plugins in -h help. Closes #993 (#995)
Co-authored-by: Garry Trinder <garry@trinder365.co.uk>
1 parent 713af29 commit 025cd30

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

dev-proxy/Program.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,16 @@
4646

4747
// store the global options that are created automatically for us
4848
// rootCommand doesn't return the global options, so we have to store them manually
49-
string[] globalOptions = ["--version", "--help", "-h", "/h", "-?", "/?"];
49+
string[] globalOptions = ["--version"];
50+
string[] helpOptions = ["--help", "-h", "/h", "-?", "/?"];
5051

51-
// check if any of the global options are present
52+
// check if any of the global- or help options are present
5253
var hasGlobalOption = args.Any(arg => globalOptions.Contains(arg));
54+
var hasHelpOption = args.Any(arg => helpOptions.Contains(arg));
5355

54-
// get the list of available subcommands
55-
var subCommands = rootCommand.Children.OfType<Command>().Select(c => c.Name).ToArray();
56-
57-
// check if any of the subcommands are present
58-
var hasSubCommand = args.Any(arg => subCommands.Contains(arg));
59-
60-
if (hasGlobalOption || hasSubCommand)
61-
{
62-
// we don't need to load plugins if the user is using a global option or using a subcommand, so we can exit early
63-
await rootCommand.InvokeAsync(args);
64-
return;
65-
}
66-
56+
// load plugins to get their options and commands
6757
var pluginLoader = new PluginLoader(logger, loggerFactory);
6858
PluginLoaderResult loaderResults = await pluginLoader.LoadPluginsAsync(pluginEvents, context);
69-
// have all the plugins init
70-
pluginEvents.RaiseInit(new InitArgs());
71-
7259
var options = loaderResults.ProxyPlugins
7360
.SelectMany(p => p.GetOptions())
7461
// remove duplicates by comparing the option names
@@ -82,13 +69,25 @@
8269
.ToList()
8370
.ForEach(rootCommand.AddCommand);
8471

85-
rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, [.. options], loaderResults.UrlsToWatch, logger);
72+
// get the list of available subcommands
73+
var subCommands = rootCommand.Children.OfType<Command>().Select(c => c.Name).ToArray();
74+
75+
// check if any of the subcommands are present
76+
var hasSubCommand = args.Any(arg => subCommands.Contains(arg));
77+
78+
if (hasGlobalOption || hasSubCommand)
79+
{
80+
// we don't need to init plugins if the user is using a global option or
81+
// using a subcommand, so we can exit early
82+
await rootCommand.InvokeAsync(args);
83+
return;
84+
}
8685

8786
// filter args to retrieve options
8887
var incomingOptions = args.Where(arg => arg.StartsWith('-')).ToArray();
8988

90-
// remove the global options from the incoming options
91-
incomingOptions = incomingOptions.Except(globalOptions).ToArray();
89+
// remove the global- and help options from the incoming options
90+
incomingOptions = [.. incomingOptions.Except([.. globalOptions, .. helpOptions])];
9291

9392
// compare the incoming options against the root command options
9493
foreach (var option in rootCommand.Options)
@@ -111,11 +110,18 @@
111110
// list the remaining incoming options as unknown in the output
112111
if (incomingOptions.Length > 0)
113112
{
114-
logger.LogError("Unknown option(s): {unknownOptions}", string.Join(" ", incomingOptions));
115-
logger.LogInformation("TIP: Use --help view available options");
116-
logger.LogInformation("TIP: Are you missing a plugin? See: https://aka.ms/devproxy/plugins");
113+
Console.Error.WriteLine("Unknown option(s): {0}", string.Join(" ", incomingOptions));
114+
Console.Error.WriteLine("TIP: Use --help view available options");
115+
Console.Error.WriteLine("TIP: Are you missing a plugin? See: https://aka.ms/devproxy/plugins");
117116
}
118117
else
119118
{
119+
if (!hasHelpOption)
120+
{
121+
// have all the plugins init
122+
pluginEvents.RaiseInit(new InitArgs());
123+
}
124+
125+
rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, [.. options], loaderResults.UrlsToWatch, logger);
120126
await rootCommand.InvokeAsync(args);
121127
}

0 commit comments

Comments
 (0)