Command<T> class
abstract
Core
Base command class for Artisanal-style CLI commands.
Provides access to the io helper for console output and renders help with proper section formatting.
Commands are the building blocks of your CLI. Each command has a name, description, and an optional set of arguments and subcommands.
Override the run() method to implement the command's logic. You can
access the Console via the io property for styled output.
Access parsed arguments with Laravel-style helpers:
class GreetCommand extends Command<void> {
@override
String get name => 'greet';
@override
String get description => 'Greet someone.';
GreetCommand() {
argParser.addOption('name', abbr: 'n', help: 'Who to greet.');
argParser.addFlag('shout', help: 'SHOUT the greeting.');
}
@override
void run() {
final name = option('name') as String? ?? 'World';
final shout = option('shout') as bool;
final message = argument(0); // first positional arg
io.success('Hello, $name!');
}
}
See Command.option, Command.argument, Command.hasOption, Command.arguments, and Command.argumentCount.
class ServeCommand extends Command<void> {
@override
String get name => 'serve';
@override
String get description => 'Start the development server.';
@override
Future<void> run() async {
io.title('Starting server...');
// ...
}
}
- Inheritance
-
- Object
- Command<
T> - Command
- Implementers
Constructors
Properties
-
aliases
→ List<
String> -
Alternative names for this command.
no setteroverride
- argParser → ArgParser
-
The argument parser for this command.
no setterinherited
- argResults → ArgResults?
-
The parsed argument results for this command.
no setterinherited
- argumentCount → int
-
Number of positional arguments.
no setter
-
arguments
→ List<
String> -
All positional arguments passed to this command.
no setter
- category → String
-
The command's category.
no setterinherited
- description → String
-
A description of this command, included in
usage.no setterinherited - globalResults → ArgResults?
-
The parsed global argument results.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
-
Whether or not this command should be hidden from help listings.
no setterinherited
- invocation → String
-
A single-line template for how to invoke this command (e.g.
"pub getpackage").no setterinherited - io → Console
-
Access to the I/O helper for console output.
no setter
- name → String
-
The name of this command.
no setterinherited
- namespaceSeparator → String
-
Separator used to group subcommands for display.
no setter
-
parent
→ Command<
T> ? -
The command's parent command, if this is a subcommand.
no setterinherited
-
runner
→ CommandRunner<
T> ? -
The command runner for this command.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
subcommands
→ Map<
String, Command< T> > -
An unmodifiable view of all sublevel commands of this command.
no setterinherited
-
suggestionAliases
→ List<
String> -
Alternate non-functional names for this command.
no setterinherited
- summary → String
-
A short description of this command, included in
parent'sCommandRunner.usage.no setterinherited - takesArguments → bool
-
Whether or not this command takes positional arguments in addition to
options.
no setterinherited
- usage → String
-
Generates a string displaying usage information for this command.
no setterinherited
-
An optional footer for
usage.no setterinherited
Methods
-
addSubcommand(
Command< T> command) → void -
Adds
Commandas a subcommand of this.inherited -
alert(
Object message, {Verbosity? verbosity}) → void - Writes an alert box (Laravel-style).
-
argument(
int index) → String? -
Returns the positional argument at
index(0-based), ornullif fewer positional arguments were provided. -
comment(
Object message, {Verbosity? verbosity}) → void - Writes a comment message (Laravel-style).
-
error(
Object message, {Verbosity? verbosity}) → void - Writes an error message (Laravel-style).
-
formatUsage(
{bool includeDescription = true}) → String - Formats help output for this command.
-
hasOption(
String name) → bool -
Returns
trueifnamewas explicitly provided on the command line. -
info(
Object message, {Verbosity? verbosity}) → void - Writes an info message (Laravel-style).
-
line(
Object message, {String? style, Verbosity? verbosity}) → void - Writes a plain line to output (Laravel-style).
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
option(
String name) → Object? -
Returns the value of a named option (e.g.,
--name,--force). -
printUsage(
) → void -
Prints the command usage information.
override
-
question(
Object message, {Verbosity? verbosity}) → void - Writes a question message (Laravel-style).
-
run(
) → FutureOr< T> ? -
Runs this command.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
usageException(
String message) → Never -
Throws a usage error with the given message.
override
-
warn(
Object message, {Verbosity? verbosity}) → void - Writes a warning message (Laravel-style).
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited