Skip to content

Commit 0f79be1

Browse files
authored
Suppress plugins for global options and sub-commands.. Closes #469 (#471)
* Suppress plugins for global options and sub-commands. Closes #469 * Revert devproxyrc.json
1 parent df28158 commit 0f79be1

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

dev-proxy/Program.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,37 @@
1414
}
1515
IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration);
1616
ProxyHost proxyHost = new();
17+
18+
// this is where the root command is created which contains all commands and subcommands
1719
RootCommand rootCommand = proxyHost.GetRootCommand(logger);
1820

21+
// store the global options that are created automatically for us
22+
// rootCommand doesn't return the global options, so we have to store them manually
23+
string[] globalOptions = { "--version", "--help", "-h", "/h", "-?", "/?" };
24+
25+
// check if any of the global options are present
26+
var hasGlobalOption = args.Any(arg => globalOptions.Contains(arg));
27+
28+
// get the list of available subcommands
29+
var subCommands = rootCommand.Children.OfType<Command>().Select(c => c.Name).ToArray();
30+
31+
// check if any of the subcommands are present
32+
var hasSubCommand = args.Any(arg => subCommands.Contains(arg));
33+
34+
if (hasGlobalOption || hasSubCommand)
35+
{
36+
// we don't need to load plugins if the user is using a global option or using a subcommand, so we can exit early
37+
await rootCommand.InvokeAsync(args);
38+
return;
39+
}
40+
1941
PluginLoaderResult loaderResults = new PluginLoader(logger).LoadPlugins(pluginEvents, context);
2042

2143
// have all the plugins init and provide any command line options
2244
pluginEvents.RaiseInit(new InitArgs(rootCommand));
2345

2446
rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, loaderResults.UrlsToWatch, logger);
2547

26-
// store the global options that are created automatically for us
27-
// rootCommand doesn't return the global options, so we have to store them manually
28-
string[] globalOptions = { "--version", "--help", "-h", "/h", "-?", "/?" };
29-
3048
// filter args to retrieve options
3149
var incomingOptions = args.Where(arg => arg.StartsWith("-")).ToArray();
3250

0 commit comments

Comments
 (0)