generateToolHelp static method

String generateToolHelp(
  1. ToolDefinition tool, {
  2. Set<String> nestedCommandNames = const {},
})

Generate tool-level help text.

Shows tool description, usage, global options, and available commands. If nestedCommandNames is provided, commands with those names are grouped under a separate "Nested Commands" section.

Implementation

static String generateToolHelp(
  ToolDefinition tool, {
  Set<String> nestedCommandNames = const {},
}) {
  final buf = StringBuffer();

  // Header
  buf.writeln('**${tool.name}** v${tool.version}');
  buf.writeln();
  buf.writeln(tool.description);
  buf.writeln();

  // Usage
  buf.writeln('<cyan>**Usage**</cyan>');
  if (tool.mode == ToolMode.multiCommand) {
    buf.writeln(
      '  ${tool.name} [global-options] ${_prefix(tool)}command [command-options]',
    );
  } else {
    buf.writeln('  ${tool.name} [options]');
  }
  buf.writeln();

  // Global options
  if (tool.allGlobalOptions.isNotEmpty) {
    buf.writeln('<yellow>**Global Options**</yellow>');
    _writeOptions(buf, tool.allGlobalOptions);
    buf.writeln();
  }

  // Split commands into native and nested
  final nativeCommands = <CommandDefinition>[];
  final nestedCommands = <CommandDefinition>[];
  for (final cmd in tool.visibleCommands) {
    if (nestedCommandNames.contains(cmd.name)) {
      nestedCommands.add(cmd);
    } else {
      nativeCommands.add(cmd);
    }
  }

  // Native commands
  if (nativeCommands.isNotEmpty) {
    buf.writeln('<green>**Commands**</green>');
    for (final cmd in nativeCommands) {
      final aliasStr = cmd.aliases.isNotEmpty
          ? ' (${cmd.aliases.join(', ')})'
          : '';
      buf.writeln('  ${_prefix(tool)}${cmd.name}$aliasStr');
      buf.writeln('      ${cmd.description}');
    }
    buf.writeln();
  }

  // Nested commands (from wired tools)
  if (nestedCommands.isNotEmpty) {
    buf.writeln('<green>**Nested Commands**</green>');
    for (final cmd in nestedCommands) {
      buf.writeln('  ${_prefix(tool)}${cmd.name}');
      buf.writeln('      ${cmd.description}');
    }
    buf.writeln();
  }

  if (nativeCommands.isNotEmpty || nestedCommands.isNotEmpty) {
    buf.writeln(
      '  Use `${tool.name} ${_prefix(tool)}command --help` for '
      'command-specific help.',
    );
    buf.writeln();
  }

  // Help topics
  if (tool.helpTopics.isNotEmpty) {
    buf.writeln('<magenta>**Help Topics**</magenta>');
    for (final topic in tool.helpTopics) {
      buf.writeln('  ${topic.name.padRight(20)} ${topic.summary}');
    }
    buf.writeln();
    buf.writeln(
      '  Use `${tool.name} help <topic>` for detailed information.',
    );
    buf.writeln();
  }

  // Footer
  if (tool.helpFooter != null) {
    buf.writeln(tool.helpFooter!);
  }

  return buf.toString();
}