Command Resolution

How to control the Command resolution by using Settings

In general, the Commander's behavior can be controlled by providing a Settings object. If no Settings object is provided, of course, default settings are applied.

Commander.ExecuteCommand( 
    args, 
    new Settings { AutoResolveCommands = true, ... } 
);

See also: How Settings control command-line parsing

Default resolution - EntryAssembly

There are two settings relevant for Command resolution:bool AutoResolveCommands and ICommandResolver CommandResolver

AutoResolveCommands is true, by default, so that Commander automatically tries to resolve Command implementations.

CommandResolver is a new AssemblyCommandResolver( Assembly.GetEntryAssembly()), by default, which will find all [Command()] annotated classes in the EntryAssembly, only.

This is good enough for simple application, with a single EXE Assembly.

AutoResolve in user-defined Assemblies

If you application is more complex you may want to implement your Commands in separate Assemblies. So, you need to tell the AssemblyCommandResolver in which assemblies it should look for Commands.

// Define the filepattern for Assemblies 
// which may contain Command implementations
const string SEARCH_PATTERN = "CLArgs.Command.*.dll";

// Get those filenames 
string[] assemblyFileNames 
  = Directory.GetFiles( 
      AppDomain.CurrentDomain.BaseDirectory, 
      SEARCH_PATTERN,
      SearchOption.AllDirectories);
      
      
Commander.ExecuteCommand(args, new Settings
{
    AutoResolveCommands = true,
    // ask the AssemblyCommandResolver to search 
    // in all provided AssemblyFileNames
    CommandResolver     
        = new AssemblyCommandResolver( assemblyFileNames)
});

See CommandRunner skeleton how Assembly resolution may look like.

User-defined resolution

Finally, you can provide your own ICommandResolver implementation to provide a more sophisticated Command resolution, than simply searching Assemblies in the file-system.

What about a DatabaseResolver that reads Assemblies from a database, to look there for Command implementations. Or a HttpResolver ... whatever you want.

What's next?

Read about manual Command Resolution

Last updated