Skip to content

Commit 360c956

Browse files
Improve error handling when using options from disabled plugins. Closes #289 (#317)
* Improve error handling when using options from disabled plugins. Closes #289 * Address review comments * Fix help and version * Address review comments --------- Co-authored-by: Waldek Mastykarz <[email protected]>
1 parent 0759b8a commit 360c956

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

m365-developer-proxy/Program.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,50 @@
1010
IProxyContext context = new ProxyContext(logger, ProxyCommandHandler.Configuration);
1111
ProxyHost proxyHost = new();
1212
RootCommand rootCommand = proxyHost.GetRootCommand();
13+
1314
PluginLoaderResult loaderResults = new PluginLoader(logger).LoadPlugins(pluginEvents, context);
1415

1516
// have all the plugins init and provide any command line options
1617
pluginEvents.RaiseInit(new InitArgs(rootCommand));
1718

1819
rootCommand.Handler = proxyHost.GetCommandHandler(pluginEvents, loaderResults.UrlsToWatch, logger);
1920

20-
return await rootCommand.InvokeAsync(args);
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+
// filter args to retrieve options
26+
var incomingOptions = args.Where(arg => arg.StartsWith("-") || arg.StartsWith("/")).ToArray();
27+
28+
// remove the global options from the incoming options
29+
incomingOptions = incomingOptions.Except(globalOptions).ToArray();
30+
31+
// compare the incoming options against the root command options
32+
foreach (var option in rootCommand.Options)
33+
{
34+
// get the option aliases
35+
var aliases = option.Aliases.ToArray();
36+
37+
// iterate over aliases
38+
foreach (string alias in aliases)
39+
{
40+
// if the alias is present
41+
if (incomingOptions.Contains(alias))
42+
{
43+
// remove the option from the incoming options
44+
incomingOptions = incomingOptions.Where(val => val != alias).ToArray();
45+
}
46+
}
47+
}
48+
49+
// list the remaining incoming options as unknown in the output
50+
if (incomingOptions.Length > 0)
51+
{
52+
logger.LogError($"Unknown option(s): {string.Join(" ", incomingOptions)}");
53+
logger.LogInfo($"TIP: Use --help view available options");
54+
logger.LogInfo($"TIP: Are you missing a plugin? See: https://aka.ms/m365/proxy/plugins");
55+
}
56+
else
57+
{
58+
await rootCommand.InvokeAsync(args);
59+
}

m365-developer-proxy/Properties/launchSettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
"Default": {
3434
"commandName": "Project",
3535
"hotReloadEnabled": true
36+
},
37+
"Missing arg": {
38+
"commandName": "Project",
39+
"commandLineArgs": "--port 8080 --summary-file-path report.md"
3640
}
3741
}
3842
}

0 commit comments

Comments
 (0)