Skip to content

Commit ae2040b

Browse files
committed
- Merge attributes and exceptions into single files
- Move hint suppressions into editorconfig
1 parent 5d8a023 commit ae2040b

25 files changed

+339
-401
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ dotnet_diagnostic.ide0044.severity = none
289289
# IDE0042: Deconstruct variable declaration
290290
dotnet_diagnostic.ide0042.severity = none
291291

292+
# IDE0305: Simplify collection initialization
293+
dotnet_diagnostic.ide0305.severity = none
294+
295+
# IDE0057: Substring can be simplified
296+
dotnet_diagnostic.ide0057.severity = none
297+
292298
# CS1998: Async method lacks 'await' operators and will run synchronously
293299
dotnet_diagnostic.cs1998.severity = none
294300

Src/Attributes.cs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using RT.Util;
2+
using RT.Util.Consoles;
3+
4+
namespace RT.CommandLine;
5+
6+
/// <summary>
7+
/// Use this to specify that a field in a class can be specified on the command line using an option, for example
8+
/// <c>-a</c> or <c>--option-name</c>. The option name(s) MUST begin with a dash (<c>-</c>).</summary>
9+
/// <param name="names">
10+
/// The name of the option. Specify several names as synonyms if required.</param>
11+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
12+
public sealed class OptionAttribute(params string[] names) : Attribute
13+
{
14+
/// <summary>All of the names of the option.</summary>
15+
public string[] Names { get; private set; } = names;
16+
}
17+
18+
/// <summary>
19+
/// Use this to specify that a command-line parameter is positional, i.e. is not invoked by an option that starts with
20+
/// "-".</summary>
21+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
22+
public sealed class IsPositionalAttribute : Attribute
23+
{
24+
/// <summary>Constructor.</summary>
25+
public IsPositionalAttribute() { }
26+
}
27+
28+
/// <summary>Use this to specify that a command-line parameter is mandatory.</summary>
29+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
30+
public sealed class IsMandatoryAttribute() : Attribute
31+
{
32+
}
33+
34+
/// <summary>Specifies that the command-line parser should ignore a field.</summary>
35+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
36+
public sealed class IgnoreAttribute : Attribute
37+
{
38+
/// <summary>Constructor.</summary>
39+
public IgnoreAttribute() { }
40+
}
41+
42+
/// <summary>
43+
/// Specifies that a field of an enum type should be interpreted as multiple possible options, each specified by an <see
44+
/// cref="OptionAttribute"/> on the enum values in the enum type.</summary>
45+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
46+
public sealed class EnumOptionsAttribute(EnumBehavior behavior) : Attribute
47+
{
48+
/// <summary>
49+
/// Specifies whether the enum is considered to represent a single value or a bitfield containing multiple values.</summary>
50+
public EnumBehavior Behavior { get; private set; } = behavior;
51+
}
52+
53+
/// <summary>
54+
/// Use this on a sub-class of an abstract class or on an enum value to specify the command the user must use to invoke
55+
/// that class or enum value.</summary>
56+
/// <param name="names">
57+
/// The command(s) the user can specify to invoke this class or enum value.</param>
58+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
59+
public sealed class CommandNameAttribute(params string[] names) : Attribute
60+
{
61+
/// <summary>The command the user can specify to invoke this class.</summary>
62+
public string[] Names { get; private set; } = names;
63+
}
64+
65+
/// <summary>Use this on an abstract class to specify that its subclasses represent various commands.</summary>
66+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
67+
public sealed class CommandGroupAttribute : Attribute
68+
{
69+
/// <summary>Constructor.</summary>
70+
public CommandGroupAttribute() { }
71+
}
72+
73+
/// <summary>
74+
/// Use this attribute to link a command-line option or command with the help text that describes (documents) it. Suitable
75+
/// for single-language applications only. See Remarks.</summary>
76+
/// <remarks>
77+
/// This attribute specifies the documentation in plain text. All characters are printed exactly as specified. You may
78+
/// wish to use <see cref="DocumentationRhoMLAttribute"/> to specify documentation with special markup for
79+
/// command-line-related concepts, as well as <see cref="DocumentationEggsMLAttribute"/> for an alternative markup
80+
/// language without command-line specific concepts.</remarks>
81+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
82+
public class DocumentationAttribute(string documentation) : Attribute
83+
{
84+
/// <summary>
85+
/// Gets the console-colored documentation string. Note that this property may throw if the text couldn't be parsed
86+
/// where applicable.</summary>
87+
public virtual ConsoleColoredString Text => OriginalText;
88+
/// <summary>Gets a string describing the documentation format to the programmer (not seen by the users).</summary>
89+
public virtual string OriginalFormat => "Plain text";
90+
/// <summary>Gets the original documentation string exactly as specified in the attribute.</summary>
91+
public string OriginalText { get; private set; } = documentation;
92+
}
93+
94+
/// <summary>
95+
/// This is a legacy attribute. Do not use in new programs. This attribute is equivalent to <see
96+
/// cref="DocumentationEggsMLAttribute"/>.</summary>
97+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
98+
public class DocumentationLiteralAttribute(string documentation) : DocumentationEggsMLAttribute(documentation)
99+
{
100+
}
101+
102+
/// <summary>
103+
/// Use this attribute to link a command-line option or command with the help text that describes (documents) it. Suitable
104+
/// for single-language applications only. The documentation is to be specified in <see cref="EggsML"/>, which is
105+
/// interpreted as described in <see cref="CommandLineParser.Colorize(EggsNode)"/>. See also <see
106+
/// cref="DocumentationRhoMLAttribute"/> and <see cref="DocumentationAttribute"/>.</summary>
107+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
108+
public class DocumentationEggsMLAttribute(string documentation) : DocumentationAttribute(documentation)
109+
{
110+
/// <summary>Gets a string describing the documentation format to the programmer (not seen by the users).</summary>
111+
public override string OriginalFormat { get { return "EggsML"; } }
112+
/// <summary>
113+
/// Gets the console-colored documentation string. Note that this property may throw if the text couldn't be parsed
114+
/// where applicable.</summary>
115+
public override ConsoleColoredString Text => _parsed ??= CommandLineParser.Colorize(EggsML.Parse(OriginalText));
116+
private ConsoleColoredString _parsed;
117+
}
118+
119+
/// <summary>
120+
/// Use this attribute to link a command-line option or command with the help text that describes (documents) it. Suitable
121+
/// for single-language applications only. The documentation is to be specified in <see cref="RhoML"/>, which is
122+
/// interpreted as described in <see cref="CommandLineParser.Colorize(RhoElement)"/>. See also <see
123+
/// cref="DocumentationAttribute"/>.</summary>
124+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false), RummageKeepUsersReflectionSafe]
125+
public class DocumentationRhoMLAttribute(string documentation) : DocumentationAttribute(documentation)
126+
{
127+
/// <summary>Gets a string describing the documentation format to the programmer (not seen by the users).</summary>
128+
public override string OriginalFormat { get { return "RhoML"; } }
129+
/// <summary>
130+
/// Gets the console-colored documentation string. Note that this property may throw if the text couldn't be parsed
131+
/// where applicable.</summary>
132+
public override ConsoleColoredString Text => _parsed ??= CommandLineParser.Colorize(RhoML.Parse(OriginalText));
133+
private ConsoleColoredString _parsed;
134+
}
135+
136+
/// <summary>
137+
/// Specifies that a specific command-line option should not be printed in help pages, i.e. the option should explicitly
138+
/// be undocumented.</summary>
139+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
140+
public sealed class UndocumentedAttribute : Attribute
141+
{
142+
/// <summary>Constructor.</summary>
143+
public UndocumentedAttribute() { }
144+
}

Src/CommandGroupAttribute.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Src/CommandLineHelpRequestedException.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Src/CommandLineParseException.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

Src/CommandLineValidationException.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Src/CommandNameAttribute.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Src/DocumentationAttribute.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

Src/DocumentationEggsMLAttribute.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

Src/DocumentationLiteralAttribute.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)