8000 Move over to Serde.CmdLine entirely by agocke · Pull Request #155 · dn-vm/dnvm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Move over to Serde.CmdLine entirely #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Serde.CmdLine/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@ public sealed class CommandParameterAttribute(int ordinal, string name) : Attrib
public sealed class CommandAttribute(string name) : Attribute
{
public string Name { get; } = name;

/// <summary>
/// Short summary of the command.
/// </summary>
public string? Summary { get; init; } = null;

/// <summary>
/// Detailed description of the command.
/// </summary>
public string? Description { get; init; } = null;
}
105 changes: 79 additions & 26 deletions src/Serde.CmdLine/CmdLine.cs
97A8
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ public static class CmdLine
public static T ParseRaw<T>(string[] args)
where T : IDeserialize<T>
{
var deserializer = new Deserializer(args);
var cmd = T.Deserialize(deserializer);
return cmd;
try
{
var deserializer = new Deserializer(args);
var cmd = T.Deserialize(deserializer);
return cmd;
}
catch (InvalidDeserializeValueException e)
{
throw new ArgumentSyntaxException(e.Message, e);
}
}

/// <summary>
Expand All @@ -41,34 +48,41 @@ public static bool TryParse<T>(string[] args, IAnsiConsole console, out T cmd)
}
}

public static string GetHelpText(ISerdeInfo serdeInfo)
public static string GetHelpText(ISerdeInfo serdeInfo, IEnumerable<ISerdeInfo>? parentCommandInfos = null)
{
var args = new List<(string Name, string? Description)>();
var options = new List<(string[] Patterns, string? Name)>();
var options = new List<(string[] Patterns, string? Name, string? Description)>();
string? commandsName = null;
var commands = new List<(string Name, string? Description)>();
var commands = new List<(string Name, string? Summary, string? Description)>();
for (int fieldIndex = 0; fieldIndex < serdeInfo.FieldCount; fieldIndex++)
{
var attrs = serdeInfo.GetFieldAttributes(fieldIndex);
foreach (var attr in attrs)
{
if (attr is { AttributeType: { Name: nameof(CommandOptionAttribute) },
ConstructorArguments: [ { Value: string flagNames } ] })
ConstructorArguments: [ { Value: string flagNames } ],
NamedArguments: var namedArgs })
{
// Consider nullable boolean fields as flag options.
#pragma warning disable SerdeExperimentalFieldInfo
var optionName = serdeInfo.GetFieldInfo(fieldIndex).Name == "bool?"
#pragma warning restore SerdeExperimentalFieldInfo
? null
: $"<{serdeInfo.GetFieldStringName(fieldIndex)}>";
options.Add((flagNames.Split('|'), optionName));
string? desc = null;
if (namedArgs is [ { MemberName: nameof(CommandParameterAttribute.Description),
TypedValue: { Value: string attrDesc } } ])
{
desc = attrDesc;
}
options.Add((flagNames.Split('|'), optionName, desc));
}
else if (attr is { AttributeType: { Name: nameof(CommandParameterAttribute) },
ConstructorArguments: [ { Value: int paramIndex }, { Value: string paramName } ],
NamedArguments: var namedArgs })
NamedArguments: var namedArgs2 })
{
string? desc = null;
if (namedArgs is [ { MemberName: nameof(CommandParameterAttribute.Description),
if (namedArgs2 is [ { MemberName: nameof(CommandParameterAttribute.Description),
TypedValue: { Value: string attrDesc } } ])
{
desc = attrDesc;
Expand All @@ -95,6 +109,7 @@ public static string GetHelpText(ISerdeInfo serdeInfo)
foreach (var unionField in unionInfo.CaseInfos)
{
var cmdName = unionField.Name;
string? summary = null;
string? desc = null;
foreach (var caseAttr in unionField.Attributes)
{
Expand All @@ -103,15 +118,25 @@ public static string GetHelpText(ISerdeInfo serdeInfo)
NamedArguments: var namedCaseArgs })
{
cmdName = caseCmdName;
if (namedCaseArgs is [ { MemberName: nameof(CommandAttribute.Description),
TypedValue: { Value: string caseDesc } } ])
foreach (var namedArg in namedCaseArgs)
{
desc = caseDesc;
if (namedArg is {
MemberName: nameof(CommandAttribute.Summary),
TypedValue: { Value: string caseSummary } })
{
summary = caseSummary;
}
if (namedArg is {
MemberName: nameof(CommandAttribute.Description),
TypedValue: { Value: string caseDesc } })
{
desc = caseDesc.ReplaceLineEndings();
}
}
break;
}
}
commands.Add((cmdName, desc));
commands.Add((cmdName, summary, desc));
}
}
}
Expand All @@ -122,30 +147,24 @@ public static string GetHelpText(ISerdeInfo serdeInfo)
var commandsString = commands.Count == 0
? ""
: $"""

Commands:
{Indent + string.Join(Environment.NewLine + Indent,
commands.Select(c => $"{c.Name}{c.Description?.Prepend(" ") ?? ""}"))}

commands.Select(c => $"{c.Name}{c.Summary?.Prepend(" ") ?? ""}"))}
""";

var argsString = args.Count > 0
? $"""

Arguments:
{Indent + string.Join(Environment.NewLine + Indent,
args.Select(a => $"{a.Name}{a.Description?.Prepend(" ") ?? ""}"))}

"""
: "";

var optionsString = options.Count > 0
? $"""

Options:
{Indent + string.Join(Environment.NewLine + Indent,
options.Select(o => $"{string.Join(", ", o.Patterns)}{o.Name?.Map(n => " " + n) ?? "" }"))}

options.Select(o => $"{string.Join(", ", o.Patterns)}{o.Name?.Map(n => " " + n) ?? "" }{o.Description?.Prepend(" ") ?? ""}"))}
"""
: "";

Expand All @@ -163,24 +182,58 @@ public static string GetHelpText(ISerdeInfo serdeInfo)
NamedArguments: var namedArgs })
{
topLevelName = name;
if (namedArgs is [ { MemberName: nameof(CommandAttribute.Description),
TypedValue: { Value: string desc } } ])
foreach (var named in namedArgs)
{
topLevelDesc = Environment.NewLine + desc + Environment.NewLine;
if (named is {
MemberName: nameof(CommandAttribute.Summary),
TypedValue: { Value: string summary } })
{
topLevelDesc = Environment.NewLine + summary + Environment.NewLine;
}
if (named is {
MemberName: nameof(CommandAttribute.Description),
TypedValue: { Value: string desc } })
{
topLevelDesc = Environment.NewLine + desc + Environment.NewLine;
}
}
break;
}
}

if (parentCommandInfos != null)
{
topLevelName = string.Join(" ", parentCommandInfos.Select(GetCommandName)) + " " + topLevelName;
}

var argsShortString = args.Count > 0
? " " + string.Join(" ", args.Select(a => a.Name))
: "";

var remainingString = string.Join(Environment.NewLine + Environment.NewLine,
((string[])[ argsString, optionsString, commandsString ]).Where(s => !string.IsNullOrWhiteSpace(s)));

return $"""
Usage: {topLevelName}{optionsUsageShortString}{commandsName?.Map(n => $" <{n}>") ?? ""}{argsShortString}
{topLevelDesc}{argsString}{optionsString}{commandsString}
{topLevelDesc}
{remainingString}

""";
}

public static string GetCommandName(ISerdeInfo serdeInfo)
{
var name = serdeInfo.Name;
foreach (var attr in serdeInfo.Attributes)
{
if (attr is { AttributeType: { Name: nameof(CommandAttribute) },
ConstructorArguments: [ { Value: string commandName } ] })
{
name = commandName;
break;
}
}
return name;
}

}
Loading
Loading
0