/
CommandAttribute.cs
195 lines (170 loc) · 6.66 KB
/
CommandAttribute.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace McMaster.Extensions.CommandLineUtils
{
/// <summary>
/// Represents a command line application using attributes to define options and arguments.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class CommandAttribute : Attribute
{
private string[] _names = Array.Empty<string>();
/// <summary>
/// Initializes a new <see cref="CommandAttribute"/>.
/// </summary>
public CommandAttribute()
{ }
/// <summary>
/// Initializes a new <see cref="CommandAttribute"/>.
/// </summary>
/// <param name="name">The name of the command.</param>
public CommandAttribute(string name)
{
Name = name;
}
/// <summary>
/// Initializes a new <see cref="CommandAttribute"/>.
/// </summary>
/// <param name="names">The names of the command. The first name given is the primary name</param>
public CommandAttribute(params string[] names)
{
_names = names;
}
/// <summary>
/// The name of the command line application. When this is a subcommand, it is the name of the word used to invoke the subcommand.
/// </summary>
/// <seealso cref="CommandLineApplication.Name" />
public string? Name
{
get => _names.Length > 0 ? _names[0] : null;
set
{
if (value != null)
{
_names = new[] { value };
}
else
{
_names = Array.Empty<string>();
}
}
}
/// <summary>
/// THe names of the command. The first is the primary name. All other names are aliases.
/// </summary>
public IEnumerable<string> Names => _names;
/// <summary>
/// The full name of the command line application to show in help text.
/// </summary>
/// <seealso cref="CommandLineApplication.FullName" />
public string? FullName { get; set; }
/// <summary>
/// A description of the command.
/// </summary>
/// <seealso cref="CommandLineApplication.Description"/>
public string? Description { get; set; }
/// <summary>
/// Determines if this command appears in generated help text.
/// </summary>
/// <seealso cref="CommandLineApplication.ShowInHelpText"/>
public bool ShowInHelpText { get; set; } = true;
/// <summary>
/// Additional text that appears at the bottom of generated help text.
/// </summary>
/// <seealso cref="CommandLineApplication.ExtendedHelpText"/>
public string? ExtendedHelpText { get; set; }
/// <summary>
/// Set the behavior for how to handle unrecognized arguments.
/// </summary>
public UnrecognizedArgumentHandling UnrecognizedArgumentHandling
{
get => _unrecognizedArgumentHandling ?? UnrecognizedArgumentHandling.Throw;
set => _unrecognizedArgumentHandling = value;
}
private UnrecognizedArgumentHandling? _unrecognizedArgumentHandling;
/// <summary>
/// Allow '--' to be used to stop parsing arguments.
/// </summary>
/// <seealso cref="CommandLineApplication.AllowArgumentSeparator"/>
public bool AllowArgumentSeparator { get; set; }
/// <summary>
/// Treat arguments beginning as '@' as a response file.
/// </summary>
/// <seealso cref="CommandLineApplication.ResponseFileHandling"/>
public ResponseFileHandling ResponseFileHandling { get; set; } = ResponseFileHandling.Disabled;
/// <summary>
/// The way arguments and options are matched.
/// </summary>
public StringComparison OptionsComparison { get; set; } = StringComparison.Ordinal;
/// <summary>
/// Specifies the culture used to convert values into types.
/// </summary>
public CultureInfo ParseCulture { get; set; } = CultureInfo.CurrentCulture;
/// <summary>
/// Whether a Pager should be used to display help text.
/// </summary>
public bool UsePagerForHelpText { get; set; } = false;
/// <summary>
/// <para>
/// One or more options of <see cref="CommandOptionType.NoValue"/>, followed by at most one option that takes values, should be accepted when grouped behind one '-' delimiter.
/// </para>
/// <para>
/// When true, the following are equivalent.
///
/// <code>
/// -abcXyellow
/// -abcX=yellow
/// -abcX:yellow
/// -abc -X=yellow
/// -ab -cX=yellow
/// -a -b -c -Xyellow
/// -a -b -c -X yellow
/// -a -b -c -X=yellow
/// -a -b -c -X:yellow
/// </code>
/// </para>
/// <para>
/// This defaults to true unless an option with a short name of two or more characters is added.
/// </para>
/// </summary>
/// <remarks>
/// <seealso href="https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html"/>
/// </remarks>
public bool ClusterOptions
{
get => _clusterOptions ?? true;
set => _clusterOptions = value;
}
private bool? _clusterOptions;
internal void Configure(CommandLineApplication app)
{
// this might have been set from SubcommandAttribute
app.Name = Name ?? app.Name;
foreach (var name in Names.Skip(1))
{
app.AddName(name);
}
app.AllowArgumentSeparator = AllowArgumentSeparator;
app.Description = Description;
app.ExtendedHelpText = ExtendedHelpText;
app.FullName = FullName;
app.ResponseFileHandling = ResponseFileHandling;
app.ShowInHelpText = ShowInHelpText;
app.OptionsComparison = OptionsComparison;
app.ValueParsers.ParseCulture = ParseCulture;
app.UsePagerForHelpText = UsePagerForHelpText;
if (_clusterOptions.HasValue)
{
app.ClusterOptions = _clusterOptions.Value;
}
if (_unrecognizedArgumentHandling.HasValue)
{
app.UnrecognizedArgumentHandling = _unrecognizedArgumentHandling.Value;
}
}
}
}