Skip to content

Commit 3e3cbc4

Browse files
Fix docs for making options required
1 parent 0708f22 commit 3e3cbc4

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

Spectre.Docs.Cli.Examples/DemoApps/MakingOptionsRequired/Main.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ internal class DeployCommand : Command<DeployCommand.Settings>
2222
{
2323
public class Settings : CommandSettings
2424
{
25-
// Use C#'s required keyword - simplest approach
26-
[CommandOption("-e|--environment")]
25+
// Use `isRequired` parameter on `CommandOption`
26+
[CommandOption("-e|--environment <TARGET>", isRequired: true)]
2727
[Description("Target environment")]
2828
public required string Environment { get; init; }
2929

30-
[CommandOption("-v|--version")]
30+
[CommandOption("-v|--version <VERSION>", isRequired: true)]
3131
[Description("Version to deploy")]
3232
public required string Version { get; init; }
3333

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,20 @@ order: 2060
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.
99

10-
## Use the `required` Keyword
10+
## Use the `isRequired` Parameter
1111

12-
The simplest approach is to use C#'s `required` keyword on the property. Spectre.Console.Cli detects this and enforces the option, displaying "Required" in help output and showing a clear error when omitted.
12+
The simplest approach is to use the `isRequired` parameter on the `CommandOption` attribute.
1313

1414
```csharp:xmldocid
1515
T:Spectre.Docs.Cli.Examples.DemoApps.MakingOptionsRequired.DeployCommand.Settings
1616
```
1717

18-
When users omit a required option:
19-
20-
```
21-
Error: Option '--environment' is required.
22-
```
23-
2418
Help output marks these options clearly:
2519

2620
```
2721
OPTIONS:
28-
-e, --environment Target environment (Required)
29-
-v, --version Version to deploy (Required)
22+
-e, --environment Target environment. Required
23+
-v, --version Version to deploy. Required
3024
--dry-run Preview changes without applying them
3125
```
3226

0 commit comments

Comments
 (0)