Skip to content

Commit 4d1d33c

Browse files
committed
Restore two fixes from the past:
* Look through all loaded assemblies to find subcommand classes, not just the field type’s own assembly * Use Process.GetCurrentProcess().ProcessName instead of Assembly.GetEntryAssembly().Location
1 parent 19da002 commit 4d1d33c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Src/CommandLine.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using System.Reflection;
23
using RT.Internal;
34
using RT.PostBuild;
@@ -457,7 +458,7 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
457458
{
458459
missingMandatories.Remove(field);
459460
positionals.RemoveAt(0);
460-
foreach (var subclass in field.FieldType.Assembly.GetTypes().Where(t => !t.IsAbstract && t.IsSubclassOf(field.FieldType)))
461+
foreach (var subclass in allTypes().Where(t => !t.IsAbstract && t.IsSubclassOf(field.FieldType)))
461462
if (subclass.GetCustomAttributes<CommandNameAttribute>().First().Names.Any(c => c.Equals(args[i], StringComparison.OrdinalIgnoreCase)))
462463
{
463464
field.SetValue(ret, parseCommandLine(args, subclass, i + 1, helpProcessor));
@@ -518,6 +519,8 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
518519
return ret;
519520
}
520521

522+
private static IEnumerable<Type> allTypes() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes());
523+
521524
private static bool convertStringAndSetField(string value, object cmdLineObject, FieldInfo field)
522525
{
523526
object result;
@@ -545,7 +548,7 @@ private static Func<int, ConsoleColoredString> getHelpGenerator(Type type, Func<
545548

546549
var helpString = new List<ConsoleColoredString>();
547550
var commandNameAttr = type.GetCustomAttributes<CommandNameAttribute>().FirstOrDefault();
548-
string commandName = commandNameAttr == null ? Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location) : "... " + commandNameAttr.Names.OrderByDescending(c => c.Length).First();
551+
string commandName = commandNameAttr == null ? Process.GetCurrentProcess().ProcessName : "... " + commandNameAttr.Names.OrderByDescending(c => c.Length).First();
549552

550553
//
551554
// ## CONSTRUCT THE “USAGE” LINE
@@ -706,7 +709,7 @@ private static bool createParameterHelpRow(ref int row, TextTable table, FieldIn
706709
else if (field.FieldType.IsDefined<CommandGroupAttribute>())
707710
{
708711
int origRow = row;
709-
foreach (var ty in field.FieldType.Assembly.GetTypes()
712+
foreach (var ty in allTypes()
710713
.Where(t => t.IsSubclassOf(field.FieldType) && t.IsDefined<CommandNameAttribute>() && !t.IsAbstract && !t.IsDefined<UndocumentedAttribute>())
711714
.OrderBy(t => t.GetCustomAttributes<CommandNameAttribute>().First().Names.MinElement(c => c.Length)))
712715
{
@@ -919,7 +922,7 @@ private static void postBuildStep(IPostBuildReporter rep, Type commandLineType,
919922
rep.Error($"{commandLineType.FullName}.{field.Name}: CommandGroup fields must be declared [IsPositional].", "class " + commandLineType.Name, field.Name);
920923

921924
// The class must have at least two subclasses with a [CommandName] attribute
922-
var subclasses = field.FieldType.Assembly.GetTypes().Where(t => !t.IsAbstract && t.IsSubclassOf(field.FieldType));
925+
var subclasses = allTypes().Where(t => !t.IsAbstract && t.IsSubclassOf(field.FieldType));
923926
if (!subclasses.Any())
924927
rep.Error($"{commandLineType.FullName}.{field.Name}: The CommandGroup class type must have at least one non-abstract derived class with the [CommandName] attribute.", "class " + field.FieldType.Name);
925928

0 commit comments

Comments
 (0)