cmd method

void cmd(
  1. String pattern,
  2. CliHandler handler, {
  3. required List<OptionSpec> options,
  4. required bool globals,
  5. String? description,
})

Registers a route.

pattern is a space separated sequence of literal words, required <name> parameters, and, only as the very last segment, either one optional [<name>] parameter or one * wildcard.

options is this route's own option schema. globals says whether the router's global options also apply to this route. Both are required: there is no default scope.

Implementation

void cmd(
  String pattern,
  CliHandler handler, {
  required List<OptionSpec> options,
  required bool globals,
  String? description,
}) {
  final ownOptions = List<OptionSpec>.unmodifiable(options);
  final segs = _parseSegments(pattern);
  _validateOptionScope(ownOptions, _globalOptions);

  final lastSeg = segs.isEmpty ? null : segs.last;
  final optionalParamName = lastSeg?.kind == _SegKind.optionalParam
      ? lastSeg!.name
      : null;
  final hasWildcard = lastSeg?.kind == _SegKind.wildcard;

  final positionals = <String>[
    for (final s in segs)
      if (s.kind == _SegKind.requiredParam ||
          s.kind == _SegKind.optionalParam)
        s.name!,
  ];

  final publicSegs = (hasWildcard || optionalParamName != null)
      ? segs.sublist(0, segs.length - 1)
      : segs;

  final route = CliRoute(
    pattern: _segsToPatternString(publicSegs),
    positionals: positionals,
    options: ownOptions,
    globals: globals,
    optionalParam: optionalParamName,
    hasWildcard: hasWildcard,
    description: description,
  );

  // Checked against the trie as it stands before this route is inserted,
  // so the check never sees itself and the trie is never mutated ahead of
  // a build-time error (spec 8.2, items 4+5).
  final existingBoundary = _existingLiteralPrefixNode(_root, segs);
  if (existingBoundary != null) {
    _validateSiblingOptionScope(existingBoundary, route);
  }

  final reg = _RegisteredRoute(
    route: route,
    handler: handler,
    segments: segs,
  );
  _insertSegments(_root, segs, reg);
  _flatRoutes.add(reg);
}