Skip to content

Commit 40a95ed

Browse files
committed
Move all classes to a separate file
1 parent 501b053 commit 40a95ed

26 files changed

+506
-426
lines changed

Src/CmdLineColor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace RT.CommandLine;
2+
3+
internal static class CmdLineColor
4+
{
5+
public const ConsoleColor Option = ConsoleColor.Yellow;
6+
public const ConsoleColor FieldBrackets = ConsoleColor.DarkCyan;
7+
public const ConsoleColor Field = ConsoleColor.Cyan;
8+
public const ConsoleColor Command = ConsoleColor.Green;
9+
public const ConsoleColor EnumValue = ConsoleColor.Green;
10+
public const ConsoleColor UsageLinePrefix = ConsoleColor.Green;
11+
public const ConsoleColor OptionalityDelimiters = ConsoleColor.DarkGray; // e.g. [foo|bar] has [, ] and | in this color
12+
public const ConsoleColor SubcommandsPresentAsterisk = ConsoleColor.DarkYellow;
13+
public const ConsoleColor UnexpectedArgument = ConsoleColor.Magenta;
14+
public const ConsoleColor Error = ConsoleColor.Red;
15+
public const ConsoleColor HelpHeading = ConsoleColor.White;
16+
public const ConsoleColor Highlight = ConsoleColor.White;
17+
}

Src/CmdLineExtensions.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Reflection;
2+
using RT.Internal;
3+
using RT.Util;
4+
using RT.Util.Consoles;
5+
using RT.Util.ExtensionMethods;
6+
7+
namespace RT.CommandLine;
8+
9+
static class CmdLineExtensions
10+
{
11+
public static string[] GetOrderedOptionAttributeNames(this MemberInfo member)
12+
{
13+
var attr = member.GetCustomAttributes<OptionAttribute>().FirstOrDefault();
14+
return attr == null ? null : attr.Names.OrderBy(compareOptionNames).ToArray();
15+
}
16+
17+
private static int compareOptionNames(string opt1, string opt2)
18+
{
19+
bool long1 = opt1.StartsWith("--");
20+
bool long2 = opt2.StartsWith("--");
21+
if (long1 == long2)
22+
return StringComparer.OrdinalIgnoreCase.Compare(opt1, opt2);
23+
else if (long1)
24+
return 1; // --blah comes after -blah
25+
else
26+
return -1;
27+
}
28+
29+
public static ConsoleColoredString FormatParameterUsage(this FieldInfo field, bool isMandatory)
30+
{
31+
// Positionals
32+
if (field.IsDefined<IsPositionalAttribute>())
33+
return (isMandatory ? "{0}" : "[{0}]").Color(CmdLineColor.OptionalityDelimiters).Fmt(
34+
"<".Color(CmdLineColor.FieldBrackets) + field.Name.Color(CmdLineColor.Field) + ">".Color(CmdLineColor.FieldBrackets));
35+
36+
// -t name [-t name [...]] — arrays, multi-value enums with CommandNames
37+
if (field.FieldType.IsArray ||
38+
(field.FieldType.IsEnum &&
39+
field.IsDefined<OptionAttribute>() &&
40+
field.IsDefined<EnumOptionsAttribute>() &&
41+
field.GetCustomAttributes<EnumOptionsAttribute>().First().Behavior == EnumBehavior.MultipleValues))
42+
{
43+
return (isMandatory ? "{0} {1} [{0} {1} [...]]" : "[{0} {1} [{0} {1} [...]]]").Color(CmdLineColor.OptionalityDelimiters).Fmt(
44+
field.GetOrderedOptionAttributeNames().First().Color(CmdLineColor.Option),
45+
"<".Color(CmdLineColor.FieldBrackets) + field.Name.Color(CmdLineColor.Field) + ">".Color(CmdLineColor.FieldBrackets));
46+
}
47+
48+
// Enums with Option names
49+
if (field.FieldType.IsEnum && !field.IsDefined<OptionAttribute>())
50+
{
51+
var options = field.FieldType.GetFields(BindingFlags.Public | BindingFlags.Static)
52+
.Where(fld => fld.IsDefined<OptionAttribute>() && !fld.IsDefined<UndocumentedAttribute>())
53+
.Select(fi => fi.GetOrderedOptionAttributeNames().First().Color(CmdLineColor.Option))
54+
.ToArray();
55+
56+
if (field.IsDefined<EnumOptionsAttribute>() && field.GetCustomAttributes<EnumOptionsAttribute>().First().Behavior == EnumBehavior.MultipleValues)
57+
// [-t] [-u] [-v] — multi-value enums with Option names
58+
return options.Select(opt => "[{0}]".Color(CmdLineColor.OptionalityDelimiters).Fmt(opt)).JoinColoredString(" ");
59+
60+
// {-t|-u} — single-value enums with Options
61+
return (isMandatory ? (options.Length > 1 ? "{{{0}{1}" : "{0}") : "[{0}]").Color(CmdLineColor.OptionalityDelimiters).Fmt(options.JoinColoredString("|".Color(CmdLineColor.OptionalityDelimiters)), "}");
62+
}
63+
64+
// -t — bools
65+
if (field.FieldType == typeof(bool))
66+
return "[{0}]".Color(CmdLineColor.OptionalityDelimiters).Fmt(field.GetOrderedOptionAttributeNames().First().Color(CmdLineColor.Option));
67+
68+
// -t name
69+
return (isMandatory ? "{0} {1}" : "[{0} {1}]").Color(CmdLineColor.OptionalityDelimiters).Fmt(
70+
field.GetOrderedOptionAttributeNames().First().Color(CmdLineColor.Option),
71+
"<".Color(CmdLineColor.FieldBrackets) + field.Name.Color(CmdLineColor.Field) + ">".Color(CmdLineColor.FieldBrackets));
72+
}
73+
}

Src/CommandGroupAttribute.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RT.CommandLine;
2+
3+
/// <summary>Use this on an abstract class to specify that its subclasses represent various commands.</summary>
4+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
5+
public sealed class CommandGroupAttribute : Attribute
6+
{
7+
/// <summary>Constructor.</summary>
8+
public CommandGroupAttribute() { }
9+
}

0 commit comments

Comments
 (0)