|
14 | 14 | } |
15 | 15 | IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration); |
16 | 16 | ProxyHost proxyHost = new(); |
| 17 | + |
| 18 | +// this is where the root command is created which contains all commands and subcommands |
17 | 19 | RootCommand rootCommand = proxyHost.GetRootCommand(logger); |
18 | 20 |
|
| 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 | + |
19 | 41 | PluginLoaderResult loaderResults = new PluginLoader(logger).LoadPlugins(pluginEvents, context); |
20 | 42 |
|
21 | 43 | // have all the plugins init and provide any command line options |
22 | 44 | pluginEvents.RaiseInit(new InitArgs(rootCommand)); |
23 | 45 |
|
24 | 46 | rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, loaderResults.UrlsToWatch, logger); |
25 | 47 |
|
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 | | - |
30 | 48 | // filter args to retrieve options |
31 | 49 | var incomingOptions = args.Where(arg => arg.StartsWith("-")).ToArray(); |
32 | 50 |
|
|
0 commit comments