Skip to content

Commit fe6b75c

Browse files
committed
Remove outdated Configuration API and Extensibility Points reference articles. Add new "How-To" guides on escaping markup and writing exceptions, along with accompanying code examples. Adjust navigation order for improved usability.
1 parent 0af5770 commit fe6b75c

43 files changed

Lines changed: 399 additions & 545 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Spectre.Console;
2+
3+
namespace Spectre.Docs.Examples.SpectreConsole.HowTo;
4+
5+
internal static class EscapingMarkupHowTo
6+
{
7+
/// <summary>
8+
/// Use Markup.Escape() to safely display user-provided strings.
9+
/// </summary>
10+
public static void EscapeUserInput()
11+
{
12+
// User input that contains brackets
13+
var userInput = "Use [brackets] for indexing";
14+
var escaped = Markup.Escape(userInput);
15+
16+
// Safe to use in markup string
17+
AnsiConsole.MarkupLine($"[blue]Input:[/] {escaped}");
18+
19+
// Common patterns that need escaping
20+
var configKey = "settings[debug]";
21+
var arrayRef = "items[0]";
22+
23+
AnsiConsole.MarkupLine($"[green]Config:[/] {Markup.Escape(configKey)}");
24+
AnsiConsole.MarkupLine($"[green]Array:[/] {Markup.Escape(arrayRef)}");
25+
}
26+
27+
/// <summary>
28+
/// Use MarkupLineInterpolated() to automatically escape interpolated values.
29+
/// </summary>
30+
public static void UseSafeInterpolation()
31+
{
32+
// Values that contain brackets are escaped automatically
33+
var fileName = "config[backup].json";
34+
var userName = "admin[test]";
35+
var version = "v2.0[beta]";
36+
37+
// No manual escaping needed
38+
AnsiConsole.MarkupLineInterpolated($"[blue]File:[/] {fileName}");
39+
AnsiConsole.MarkupLineInterpolated($"[green]User:[/] {userName}");
40+
AnsiConsole.MarkupLineInterpolated($"[yellow]Version:[/] {version}");
41+
42+
// Works with multiple interpolated values
43+
AnsiConsole.MarkupLineInterpolated(
44+
$"[dim]Loaded[/] {fileName} [dim]for user[/] {userName}");
45+
}
46+
47+
48+
/// <summary>
49+
/// Use Markup.Remove() to strip formatting for logging or file output.
50+
/// </summary>
51+
public static void StripMarkupForLogging()
52+
{
53+
var styled = "[bold red]Error:[/] Connection to [blue]database[/] failed";
54+
55+
// Remove all markup tags for plain text
56+
var plain = Markup.Remove(styled);
57+
58+
AnsiConsole.MarkupLine("[dim]Styled output:[/]");
59+
AnsiConsole.MarkupLine(styled);
60+
61+
AnsiConsole.MarkupLine("[dim]Plain text for logs:[/]");
62+
AnsiConsole.WriteLine(plain);
63+
}
64+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using Spectre.Console;
2+
3+
namespace Spectre.Docs.Examples.SpectreConsole.HowTo;
4+
5+
internal static class WritingExceptionsHowTo
6+
{
7+
/// <summary>
8+
/// Write an exception to the console with default formatting.
9+
/// </summary>
10+
public static void WriteBasicException()
11+
{
12+
try
13+
{
14+
ProcessUserData(null!);
15+
}
16+
catch (Exception ex)
17+
{
18+
AnsiConsole.WriteException(ex);
19+
}
20+
}
21+
22+
/// <summary>
23+
/// Shorten file paths for cleaner stack traces.
24+
/// </summary>
25+
public static void ShortenFilePaths()
26+
{
27+
try
28+
{
29+
ProcessUserData(null!);
30+
}
31+
catch (Exception ex)
32+
{
33+
AnsiConsole.WriteException(ex, ExceptionFormats.ShortenPaths);
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Shorten everything and add clickable links to source files.
39+
/// </summary>
40+
public static void ShortenEverythingWithLinks()
41+
{
42+
try
43+
{
44+
ProcessUserData(null!);
45+
}
46+
catch (Exception ex)
47+
{
48+
AnsiConsole.WriteException(ex,
49+
ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Customize exception colors using ExceptionSettings.
55+
/// </summary>
56+
public static void CustomizeColors()
57+
{
58+
try
59+
{
60+
ProcessUserData(null!);
61+
}
62+
catch (Exception ex)
63+
{
64+
AnsiConsole.WriteException(ex, new ExceptionSettings
65+
{
66+
Format = ExceptionFormats.ShortenEverything,
67+
Style = new ExceptionStyle
68+
{
69+
Exception = new Style(Color.Grey),
70+
Message = new Style(Color.White),
71+
Method = new Style(Color.Red),
72+
Path = new Style(Color.Yellow),
73+
LineNumber = new Style(Color.Blue),
74+
}
75+
});
76+
}
77+
}
78+
79+
// Helper to generate a realistic nested exception
80+
private static void ProcessUserData(string data)
81+
{
82+
try
83+
{
84+
ValidateInput(data);
85+
}
86+
catch (Exception ex)
87+
{
88+
throw new InvalidOperationException("Failed to process user data", ex);
89+
}
90+
}
91+
92+
private static void ValidateInput(string data)
93+
{
94+
if (data == null)
95+
{
96+
throw new ArgumentNullException(nameof(data), "Input cannot be null");
97+
}
98+
}
99+
}

Spectre.Docs/Content/cli/how--to/async-commands-and-cancellation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Async Commands and Cancellation"
33
description: "How to create asynchronous commands and handle cancellation in Spectre.Console.Cli"
44
uid: "cli-async-commands"
5-
order: 2025
5+
order: 2080
66
---
77

88
When your command performs I/O-bound operations like HTTP requests, database queries, or file operations, use `AsyncCommand<TSettings>` instead of `Command<TSettings>`. This lets you use `async/await` and enables graceful shutdown when users press Ctrl+C.
@@ -26,4 +26,4 @@ T:Spectre.Docs.Cli.Examples.DemoApps.AsyncCommandsAndCancellation.FetchCommand
2626
## See Also
2727

2828
- [Handling Errors and Exit Codes](/cli/how--to/handling-errors-and-exit-codes) - Custom exception handling
29-
- [Dependency Injection in CLI Commands](/cli/how--to/dependency-injection-in-cli-commands) - Inject services like HttpClient
29+
- [Dependency Injection in CLI Apps](/cli/tutorials/dependency-injection-in-cli-apps) - Inject services like HttpClient

Spectre.Docs/Content/cli/how--to/configuring-commandapp-and-commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Configuring CommandApp and Commands"
33
description: "How to register commands with the CommandApp and configure global settings"
44
uid: "cli-app-configuration"
5-
order: 2030
5+
order: 2090
66
---
77

88
When building a CLI with multiple commands, use `CommandApp.Configure` to register commands, set up aliases, and customize how your application appears in help output.

Spectre.Docs/Content/cli/how--to/customizing-help-text-and-usage.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Customizing Help Text and Usage"
33
description: "How to tailor the automatically generated help output of Spectre.Console.Cli"
44
uid: "cli-help-customization"
5-
order: 2040
5+
order: 2100
66
---
77

88
Spectre.Console.Cli generates help text automatically from your commands and settings. When the defaults don't match your needs—whether for branding, accessibility, or clarity—you can customize the application name, add usage examples, adjust styling, or disable styling entirely for plain text output.
@@ -60,5 +60,4 @@ The built-in `HelpProvider` class can also be extended if you only need to overr
6060

6161
## See Also
6262

63-
- [Hiding Commands and Options](/cli/how--to/hiding-commands-and-options) - Keep commands functional but hidden from help
64-
- [CommandApp Configuration](/cli/reference/commandapp-reference) - Full configuration options
63+
- [Hiding Commands and Options](/cli/how--to/hiding-commands-and-options) - Keep commands functional but hidden from help

Spectre.Docs/Content/cli/how--to/defining-commands-and-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Defining Commands and Arguments"
33
description: "How to declare command-line parameters (arguments and options) using Spectre.Console.Cli's attributes and settings classes"
44
uid: "cli-commands-arguments"
5-
order: 2010
5+
order: 2050
66
---
77

88
Every command in Spectre.Console.Cli receives its input through a `CommandSettings` class. Decorate properties with `[CommandArgument]` for positional parameters and `[CommandOption]` for named flags and options. The framework handles parsing, validation, and help generation automatically.

Spectre.Docs/Content/cli/how--to/handling-errors-and-exit-codes.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Handling Errors and Exit Codes"
33
description: "How Spectre.Console.Cli deals with exceptions and how to customize error handling"
44
uid: "cli-error-handling"
5-
order: 2050
5+
order: 2070
66
---
77

88
By default, Spectre.Console.Cli catches exceptions, displays a user-friendly message, and returns exit code `-1`. When you need more control—different exit codes for different error types, custom formatting, or integration with logging—you have two options: `SetExceptionHandler` for centralized handling within the framework, or `PropagateExceptions` for full manual control with try-catch blocks.
@@ -32,5 +32,4 @@ This approach requires more code but gives you complete flexibility. You can cat
3232
3333
## See Also
3434

35-
- [Async Commands and Cancellation](/cli/how--to/async-commands-and-cancellation) - Handle cancellation gracefully
36-
- [CommandApp Configuration](/cli/reference/commandapp-reference) - Full configuration options
35+
- [Async Commands and Cancellation](/cli/how--to/async-commands-and-cancellation) - Handle cancellation gracefully

Spectre.Docs/Content/cli/how--to/hiding-commands-and-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Hiding Commands and Options"
33
description: "How to hide commands and options from help output while keeping them functional"
44
uid: "cli-hidden-commands"
5-
order: 2045
5+
order: 2130
66
---
77

88
Sometimes you need commands or options that work but shouldn't appear in help output—internal debugging tools, deprecated features you're phasing out, or advanced options that would overwhelm typical users. Hidden items remain fully functional; users who know about them can still use them.

Spectre.Docs/Content/cli/how--to/intercepting-command-execution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Intercepting Command Execution"
33
description: "How to use command interceptors to run logic before or after any command executes"
44
uid: "cli-command-interception"
5-
order: 2080
5+
order: 2140
66
---
77

88
When you need cross-cutting concerns like logging, timing, or authentication across all commands without duplicating code, implement a command interceptor. The interceptor runs before and after every command, keeping individual commands focused on their core logic.
@@ -35,5 +35,5 @@ The `InterceptResult` method receives the exit code by reference (`ref int resul
3535

3636
## See Also
3737

38-
- [Dependency Injection in CLI Commands](/cli/how--to/dependency-injection-in-cli-commands) - Inject services into interceptors
38+
- [Dependency Injection in CLI Apps](/cli/tutorials/dependency-injection-in-cli-apps) - Inject services into interceptors
3939
- [Handling Errors and Exit Codes](/cli/how--to/handling-errors-and-exit-codes) - Custom error handling

Spectre.Docs/Content/cli/how--to/making-options-required.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Making Options Required"
33
description: "How to make command-line options required instead of optional in Spectre.Console.Cli"
44
uid: "cli-required-options"
5-
order: 2015
5+
order: 2060
66
---
77

88
By design, options (flags like `--name` or `-n`) are optional—that's why they're called "options." However, there are cases where you want a named option that users must provide, such as specifying a target environment or API key.

0 commit comments

Comments
 (0)