Class CommandBuilder
- Namespace
- Albatross.CommandLine
- Assembly
- Albatross.CommandLine.dll
Builds and manages a hierarchical command structure for command-line interfaces. Commands are organized by space-separated keys (e.g., "parent child") that define the hierarchy.
public class CommandBuilder
- Inheritance
-
CommandBuilder
- Inherited Members
Constructors
CommandBuilder(string)
Creates a new command builder with the specified description for the root command.
public CommandBuilder(string rootCommandDescription)
Parameters
rootCommandDescriptionstringThe description to display in help text for the root command.
Fields
RootCommandKey
The key that identifies the root command. Register a keyed IAsyncCommandHandler under this key - or declare a verb with an empty name - to give the root command its own handler.
public const string RootCommandKey = ""
Field Value
Properties
RootCommand
Gets the root command of the command hierarchy. Mutate it directly to give the root its own options and arguments - code generation cannot, because a verb with an empty name produces no command class.
public RootCommand RootCommand { get; }
Property Value
Methods
Add<T>(string)
Adds a new command instance of the specified type to the command hierarchy.
public T Add<T>(string key) where T : Command
Parameters
keystringThe space-separated key defining the command's position in the hierarchy.
Returns
- T
The newly created command instance, or the existing RootCommand when
keyis RootCommandKey.
Type Parameters
TThe command type to instantiate and add. It must expose a parameterless constructor unless
keyis RootCommandKey, in which case nothing is constructed. The constraint cannot be expressed asnew()because RootCommand declares only a constructor with an optional parameter, which does not satisfy it.
Add<T>(string, T)
Adds an existing command to the command hierarchy.
public void Add<T>(string key, T command) where T : Command
Parameters
keystringThe space-separated key defining the command's position in the hierarchy.
commandTThe command instance to add.
Type Parameters
TThe command type.
Exceptions
- ArgumentException
Thrown when a command with the same key already exists.
BuildTree(Func<IServiceProvider>)
Builds the complete command tree by linking child commands to their parents and setting up command actions. This method should be called after all commands have been added and before parsing.
public void BuildTree(Func<IServiceProvider> serviceFactory)
Parameters
serviceFactoryFunc<IServiceProvider>A factory function that provides the service provider for dependency injection.
ParseCommandText(string, out string, out string)
Parse the command text and return the immediate (last) sub command and its complete parent command if the text is "a b c", it will return "c" as self and "a b" as parent
public static void ParseCommandText(string commandText, out string parent, out string self)