forked from dotnet/command-line-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpAction.cs
More file actions
69 lines (60 loc) · 1.97 KB
/
HelpAction.cs
File metadata and controls
69 lines (60 loc) · 1.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.CommandLine.Invocation;
namespace System.CommandLine.Help
{
/// <summary>
/// Provides command line help.
/// </summary>
public sealed class HelpAction : SynchronousCommandLineAction
{
private HelpBuilder? _builder;
private int _maxWidth = -1;
/// <summary>
/// The maximum width in characters after which help output is wrapped.
/// </summary>
/// <remarks>It defaults to <see cref="Console.WindowWidth"/>.</remarks>
public int MaxWidth
{
get
{
if (_maxWidth < 0)
{
try
{
_maxWidth = Console.IsOutputRedirected ? int.MaxValue : Console.WindowWidth;
}
catch (Exception)
{
_maxWidth = int.MaxValue;
}
}
return _maxWidth;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
_maxWidth = value;
}
}
/// <summary>
/// Specifies an <see cref="Builder"/> to be used to format help output when help is requested.
/// </summary>
internal HelpBuilder Builder
{
get => _builder ??= new HelpBuilder(MaxWidth);
set => _builder = value ?? throw new ArgumentNullException(nameof(value));
}
/// <inheritdoc />
public override int Invoke(ParseResult parseResult)
{
var output = parseResult.InvocationConfiguration.Output;
var helpContext = new HelpContext(Builder,
parseResult.CommandResult.Command,
output);
Builder.Write(helpContext);
return 0;
}
}
}