Skip to content

Commit 389af3c

Browse files
Copilotwaldekmastykarzgarrytrinder
authored
Add concrete examples to all --help screens (#1549)
* Initial plan * Add concrete examples to all --help screens Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> * Fix misaligned descriptions in help examples Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> * Apply suggestion from @waldekmastykarz * Apply suggestion from @waldekmastykarz --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> Co-authored-by: Waldek Mastykarz <waldek@mastykarz.nl> Co-authored-by: Garry Trinder <garry@trinder365.co.uk>
1 parent ac1a513 commit 389af3c

8 files changed

Lines changed: 91 additions & 0 deletions

File tree

DevProxy/Commands/CertCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ private void ConfigureCommand()
4141
certEnsureCommand,
4242
certRemoveCommand,
4343
}.OrderByName());
44+
45+
HelpExamples.Add(this, [
46+
"devproxy cert ensure Install and trust certificate",
47+
"devproxy cert remove --force Remove certificate (no prompt)",
48+
]);
4449
}
4550

4651
private async Task EnsureCertAsync()

DevProxy/Commands/ConfigCommand.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ private void ConfigureCommand()
108108
configNewCommand,
109109
configOpenCommand
110110
}.OrderByName());
111+
112+
HelpExamples.Add(this, [
113+
"devproxy config new Create default devproxyrc.json",
114+
"devproxy config new myconfig.json Create named config file",
115+
"devproxy config get <config-id> Download config from gallery",
116+
"devproxy config open Open config in default editor",
117+
]);
111118
}
112119

113120
private async Task DownloadConfigAsync(string configId, OutputFormat outputFormat)

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ private void ConfigureCommand()
532532
commands.AddRange(_plugins.SelectMany(p => p.GetCommands()));
533533
this.AddCommands(commands.OrderByName());
534534

535+
HelpExamples.Add(this, [
536+
"devproxy Start with default config",
537+
"devproxy -c myconfig.json Start with custom config",
538+
"devproxy -u \"https://api.example.com/*\" Watch specific URLs",
539+
"devproxy --port 9000 --record Custom port, record requests",
540+
]);
541+
HelpExamples.Install(this);
542+
535543
var helpOption = Options.OfType<HelpOption>().FirstOrDefault();
536544
if (helpOption?.Action is HelpAction helpAction)
537545
{

DevProxy/Commands/HelpExamples.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.CommandLine;
6+
using System.CommandLine.Help;
7+
using System.CommandLine.Invocation;
8+
using System.Runtime.CompilerServices;
9+
10+
namespace DevProxy.Commands;
11+
12+
static class HelpExamples
13+
{
14+
private static readonly ConditionalWeakTable<Command, string[]> _examples = new();
15+
16+
public static void Add(Command command, string[] examples)
17+
{
18+
_examples.AddOrUpdate(command, examples);
19+
}
20+
21+
public static void Install(RootCommand rootCommand)
22+
{
23+
var helpOption = rootCommand.Options.OfType<HelpOption>().First();
24+
helpOption.Action = new ExamplesHelpAction();
25+
}
26+
27+
private sealed class ExamplesHelpAction : SynchronousCommandLineAction
28+
{
29+
public override int Invoke(ParseResult parseResult)
30+
{
31+
new HelpAction().Invoke(parseResult);
32+
33+
var command = parseResult.CommandResult.Command;
34+
if (_examples.TryGetValue(command, out var examples))
35+
{
36+
var output = parseResult.Configuration.Output;
37+
output.WriteLine("Examples:");
38+
foreach (var example in examples)
39+
{
40+
output.Write(" ");
41+
output.WriteLine(example);
42+
}
43+
output.WriteLine();
44+
}
45+
return 0;
46+
}
47+
}
48+
}

DevProxy/Commands/JwtCommand.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ private void ConfigureCommand()
115115
jwtSigningKeyOption
116116
}.OrderByName());
117117

118+
HelpExamples.Add(jwtCreateCommand, [
119+
"devproxy jwt create Create token with defaults",
120+
"devproxy jwt create -n \"John Doe\" -r admin -r user Token with name and roles",
121+
"devproxy jwt create -s \"read\" -s \"write\" -a myapp Token with scopes and audience",
122+
"devproxy jwt create --claims \"dept:eng\" Custom claims",
123+
]);
124+
118125
jwtCreateCommand.SetAction(parseResult =>
119126
{
120127
var jwtOptions = new JwtOptions

DevProxy/Commands/MsGraphDbCommand.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public MsGraphDbCommand(MSGraphDb msGraphDb) :
2020

2121
private void ConfigureCommand()
2222
{
23+
HelpExamples.Add(this, [
24+
"devproxy msgraphdb Generate Microsoft Graph DB",
25+
]);
26+
2327
SetAction(GenerateMsGraphDbAsync);
2428
}
2529

DevProxy/Commands/OutdatedCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ private void ConfigureCommand()
3636
Description = "Return version only",
3737
};
3838
Add(outdatedShortOption);
39+
40+
HelpExamples.Add(this, [
41+
"devproxy outdated Check for updates (human output)",
42+
"devproxy outdated --short Version number only",
43+
]);
44+
3945
SetAction(async (parseResult) =>
4046
{
4147
var versionOnly = parseResult.GetValue(outdatedShortOption);

DevProxy/Commands/StdioCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ private void ConfigureCommand()
5555
.Select(g => g.First()));
5656
this.AddOptions(options.OrderByName());
5757

58+
HelpExamples.Add(this, [
59+
"devproxy stdio npx -y @devproxy/mcp Proxy MCP server stdio",
60+
"devproxy stdio node server.js Proxy Node.js app stdio",
61+
"devproxy stdio -c myconfig.json node app.js With custom config",
62+
]);
63+
5864
SetAction(RunAsync);
5965
}
6066

0 commit comments

Comments
 (0)