How Commander uses Annotations

As you have read in Command Resolution the Commander can automatically resolve Command implementation on certain places (e.g. EntryAssembly or directory). Auto resolution will scan for Command-Annotated classes and store them in a list of CommandDesriptors.

It is key to understand Command resolution and execution is a two steps process:

  1. Resolve all annotated classes

    1. store the results in the Commander's public List<CommandDescriptor> CommandDescriptors property.

  2. Find the verb-matching Command in the CommandDescriptors list

    1. create the Command instance

    2. run the Command's Execute()method

Interesting to see, that you have the chance to register additional command-descriptors between step 1 and 2 (see Sample03.Options).

A similar mechanism is used to resolve Options (better OptionDescriptors and its Properties) and you can add your own Options without annotating properties SetOption. You can also modify or remove Options before the Command is finally executed.

Commander commander = new Commander(
  new Settings {IgnoreCase = true, AutoResolveCommands = true });
  
commander.RegisterCommand( 
  new CommandDescriptor( "TheOneAndOnlyCommand", () => new Command()));

// Let's change the argument from commands-line 
CommandLineArguments commandLineArguments = CommandLineParser.Parse(args);
commandLineArguments.SetOption("DatabaseTableName", "AnotherTable");

// Execute the Command with slightly different Options
commander.ExecuteCommand(commandLineArguments);

Last updated