writeHelpModern method
The writeHelpModern method is used to write the help of the application in a modern way.
The myControllers is a list of controllers that you want to show in the
help. If it is null it will show all controllers.
you can call this method from the controller to write the help of the application in a modern way.
This method uses ANSI escape codes to color the output and make it more readable.
Controllers whose name contains a colon (e.g. test:subtest) are treated
as sub-commands of the part before the colon (test). They are grouped
under a single namespace header instead of being listed as independent,
unrelated commands, which keeps the help output readable as the number
of sub-commands grows.
Implementation
CappConsole writeHelpModern([List<CappController>? myControllers]) {
var selectedControllers = myControllers ?? [...controllers, main];
var maxNameLen = 0;
for (var controller in selectedControllers) {
for (var option in controller.options) {
if (option.hideInHelp) {
continue;
}
if (option.name.length > maxNameLen) {
maxNameLen = option.name.length;
}
}
}
var groupOrder = <String>[];
var groups = <String, List<CappController>>{};
for (var controller in selectedControllers) {
var key = controller.name.contains(':')
? controller.name.split(':').first
: controller.name;
groups.putIfAbsent(key, () => []);
groups[key]!.add(controller);
if (!groupOrder.contains(key)) {
groupOrder.add(key);
}
}
for (var key in groupOrder) {
var members = groups[key]!;
var isNamespace = key.isNotEmpty && members.any((c) => c.name != key);
if (!isNamespace) {
for (var controller in members) {
_writeControllerHelp(controller, maxNameLen);
}
continue;
}
CappController? parent;
for (var member in members) {
if (member.name == key) {
parent = member;
}
}
_printCommandLine('✔ $key', parent?.description ?? '');
if (parent != null) {
_writeOptions(parent, maxNameLen);
}
for (var member in members) {
if (member == parent) {
continue;
}
_printCommandLine('✔ ${member.name}', member.description,
indent: '\n\t');
_writeOptions(member, maxNameLen, indent: '\t');
}
}
return CappConsole('');
}