call method

void call(
  1. ArgumentSet args,
  2. UsageInfo info,
  3. UsageContext context, {
  4. required StringSink sink,
  5. required bool showShortUsage,
})

Implementation

void call(ArgumentSet args, UsageInfo info, UsageContext context,
    {required StringSink sink, required bool showShortUsage}) {
  var attributes = attributesFactory(sink);

  final commandsGroupName = 'Commands';
  final positionalGroupName = 'Positional arguments';
  final optionsGroupName = 'Options';

  var commandsGroup = <Definition>[];
  var positionalGroup = <Definition>[];
  var optionsGroup = <Definition>[];

  var usageGroups = <UsageGroup, List<Definition>>{};

  for (var entry in args.allDefinitions(includeInverse: false)) {
    var definition = entry.value;
    var group = definition.usageGroup;
    if (group != null) {
      usageGroups.update(group, (args) => args..add(definition),
          ifAbsent: () => [definition]);
    } else if (definition is CommandDefinition) {
      commandsGroup.add(definition);
    } else if (definition is PositionalArgumentDefinition) {
      positionalGroup.add(definition);
    } else if (definition is OptionArgumentDefinition) {
      optionsGroup.add(definition);
    } else {
      throw ArgumentError.value(definition);
    }
  }

  var usagePrefix = 'Usage: ${info.application ?? '<this application>'} ';
  if (context.path.isNotEmpty) {
    usagePrefix += context.path.map((c) => c.name).join() + ' ';
  }

  sink.write(usagePrefix);

  var usageLines = _wrap(_buildUsageLine(args), attributes,
      widthReduction: usagePrefix.length);
  for (var i = 1; i < usageLines.length; i++) {
    usageLines[i] = ' ' * usagePrefix.length + usageLines[i];
  }

  sink.writeLines(usageLines);

  if (!showShortUsage) {
    var prologue = context.path.isNotEmpty
        ? context.path.last.description
        : info.prologue;
    if (prologue != null) {
      sink.writeln();
      sink.writeLines(_wrap(prologue, attributes));
    }

    _printGroup(sink, attributes, commandsGroupName, commandsGroup);
    _printGroup(sink, attributes, positionalGroupName, positionalGroup);
    _printGroup(sink, attributes, optionsGroupName, optionsGroup);
    for (var entry in usageGroups.entries) {
      _printGroup(sink, attributes, entry.key.name, entry.value);
    }

    sink.writeln();

    var epilogue = info.epilogue;
    if (epilogue != null) {
      sink.writeLines(_wrap(epilogue, attributes));
      sink.writeln();
    }
  }
}