Closed je-sendra closed 9 months ago
FYI, this is fixed with v1.8.2:
2.0.0-beta4.24123.1
of System.CommandLine.[CliCommand]
attribute ot it's a nested class and one of its parents
does not have a [CliCommand]
attribute.\"
) at the end of an argument,
is usually a path separator and not an escape for double quote, so it will be trimmed to prevent unnecessary path errors.I don't think we need custom exception classes for this as in your PR but error messages is now more clear:
public static CliCommandBuilder Get(Type definitionType)
{
if (!RegisteredDefinitionTypes.TryGetValue(definitionType, out var commandBuilder))
{
if (definitionType.GetCustomAttribute<CliCommandAttribute>() == null)
throw new Exception($"The class '{definitionType.Name}' should have [CliCommand] attribute.");
var parentWithoutAttribute = definitionType.RecurseWhileNotNull(t => t.DeclaringType)
.FirstOrDefault(t => t.GetCustomAttribute<CliCommandAttribute>() == null);
if (parentWithoutAttribute != null) //nested
throw new Exception($"The parent class '{parentWithoutAttribute.Name}' of nested class '{definitionType.Name}' should have [CliCommand] attribute.");
throw new Exception($"A registered command builder is not found for '{definitionType.Name}'. " +
$"Please ensure the source generator is running and generating a command builder for your definition class.");
}
return commandBuilder;
}
When trying to invoke a command that is inside of a class that is not marked as "CliCommand", like this:
you get the following error:
which didn't make it immediately obvious for me what the problem was. This happened while I was first testing the project.
After further investigation, I discovered that the same simple
System.Exception
and message is thrown in case the class is not marked asCliCommand
at all. Like this:And if you forget to mark the parent command as a
CliCommand
:I guess we should change the
System.Exception
thrown to some custom exceptions likeDotMake.CommandLine.ParentClassInvalidException
and improve the message to something more explanative.