|
46 | 46 |
|
47 | 47 | // store the global options that are created automatically for us
|
48 | 48 | // 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", "-?", "/?"]; |
50 | 51 |
|
51 |
| -// check if any of the global options are present |
| 52 | +// check if any of the global- or help options are present |
52 | 53 | var hasGlobalOption = args.Any(arg => globalOptions.Contains(arg));
|
| 54 | +var hasHelpOption = args.Any(arg => helpOptions.Contains(arg)); |
53 | 55 |
|
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 |
67 | 57 | var pluginLoader = new PluginLoader(logger, loggerFactory);
|
68 | 58 | PluginLoaderResult loaderResults = await pluginLoader.LoadPluginsAsync(pluginEvents, context);
|
69 |
| -// have all the plugins init |
70 |
| -pluginEvents.RaiseInit(new InitArgs()); |
71 |
| - |
72 | 59 | var options = loaderResults.ProxyPlugins
|
73 | 60 | .SelectMany(p => p.GetOptions())
|
74 | 61 | // remove duplicates by comparing the option names
|
|
82 | 69 | .ToList()
|
83 | 70 | .ForEach(rootCommand.AddCommand);
|
84 | 71 |
|
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 | +} |
86 | 85 |
|
87 | 86 | // filter args to retrieve options
|
88 | 87 | var incomingOptions = args.Where(arg => arg.StartsWith('-')).ToArray();
|
89 | 88 |
|
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])]; |
92 | 91 |
|
93 | 92 | // compare the incoming options against the root command options
|
94 | 93 | foreach (var option in rootCommand.Options)
|
|
111 | 110 | // list the remaining incoming options as unknown in the output
|
112 | 111 | if (incomingOptions.Length > 0)
|
113 | 112 | {
|
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"); |
117 | 116 | }
|
118 | 117 | else
|
119 | 118 | {
|
| 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); |
120 | 126 | await rootCommand.InvokeAsync(args);
|
121 | 127 | }
|
0 commit comments