Implementation
@override
Iterable<ConsoleCommand> get commands => programs.entries.map((entry) {
final name = entry.key;
final program = entry.value;
final worldOption = {'players', 'save', 'scripts'}.contains(name);
final parser = worldOption
? (ArgParser()..addOption(
'world',
abbr: 'w',
help: "Limit the command to this world.",
))
: null;
Future<void> invoke(CommandContext context, List<String> prefix) async {
var args = context.args;
final world = context.options?['world'] as String?;
if (world != null) {
if (args.isNotEmpty) context.usage();
args = [world];
}
await program.run(name, [...prefix, ...args]);
}
ConsoleCommand child(
String action,
String usage, {
List<String> prefix = const [],
List<ConsoleCommand> children = const [],
}) => ConsoleCommand(
name: action,
usage: usage,
subcommands: children,
description: program.getDescription() ?? '',
run: (context) => invoke(context, [...prefix, action]),
);
final children = switch (name) {
'role' => [
child('list', '<User>'),
child('add', '<User> <Role...>'),
child('remove', '<User> <Role...>'),
child('set', '<User> <Role...>'),
],
'roles' => [
child('list', ''),
child('show', '<Role>'),
child('create', '<Role> <Priority> <Display name...>'),
child('delete', '<Role> [Replacement]'),
child('rename', '<Role> <Display name...>'),
child('priority', '<Role> <Priority>'),
ConsoleCommand(
name: 'permission',
subcommands: [
child('add', '<Role> <Permission>', prefix: ['permission']),
child('remove', '<Role> <Permission>', prefix: ['permission']),
],
),
],
'whitelist' => [child('add', '<User>'), child('remove', '<User>')],
_ => <ConsoleCommand>[],
};
return ConsoleCommand(
name: name,
category: switch (id) {
'worlds' => 'Worlds',
'moderation' => 'Moderation',
'roles' => 'Roles',
'content' => 'Content',
_ => 'Server',
},
parser: parser,
usage: name == 'say' ? '<Message...>' : program.getUsage() ?? '',
description: program.getDescription() ?? '',
examples: _examples[name] ?? const [],
subcommands: children,
run: (context) => invoke(context, const []),
);
});