-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathHelpSubsystem.cs
54 lines (45 loc) · 1.83 KB
/
HelpSubsystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Subsystems.Annotations;
using System.CommandLine.Subsystems;
namespace System.CommandLine;
// stub Help subsystem demonstrating annotation model.
//
// usage:
//
//
// var help = new HelpSubsystem();
// var command = new CliCommand("greet")
// .With(help.Description, "Greet the user");
//
public class HelpSubsystem(IAnnotationProvider? annotationProvider = null)
: CliSubsystem(HelpAnnotations.Prefix, SubsystemKind.Help, annotationProvider)
{
public CliOption<bool> HelpOption { get; } = new CliOption<bool>("--help", ["-h"])
{
// TODO: Why don't we accept bool like any other bool option?
Arity = ArgumentArity.Zero
};
protected internal override void Initialize(InitializationContext context)
{
AddOptionRecursively(context.Configuration.RootCommand, HelpOption);
static void AddOptionRecursively(CliCommand command, CliOption option)
{
command.Add(option);
foreach (var subcommand in command.Subcommands)
{
AddOptionRecursively(subcommand, option);
}
}
}
protected internal override bool GetIsActivated(ParseResult? parseResult)
=> parseResult is not null && parseResult.GetValue(HelpOption);
protected internal override void Execute(PipelineResult pipelineResult)
{
// TODO: Match testable output pattern
pipelineResult.ConsoleHack.WriteLine("Help me!");
pipelineResult.SetSuccess();
}
public bool TryGetDescription (CliSymbol symbol, out string? description)
=> TryGetAnnotation (symbol, HelpAnnotations.Description, out description);
}