parse static method

CliOptions parse(
  1. List<String> args
)

Parses command line arguments and returns structured options

Takes a list of command-line args and transforms them into a structured CliOptions object. Handles validation and defaults for all options.

If invalid arguments are provided, displays an error message and exits.

Implementation

static CliOptions parse(List<String> args) {
  final parser =
      ArgParser()
        ..addFlag(
          'size',
          abbr: 's',
          help: 'Show file and directory sizes',
          negatable: false,
          defaultsTo: false,
        )
        ..addFlag(
          'help',
          abbr: 'h',
          help: 'Show help information',
          negatable: false,
          defaultsTo: false,
        )
        ..addFlag(
          'quiet',
          abbr: 'q',
          help: 'Suppress error messages',
          negatable: false,
          defaultsTo: false,
        )
        ..addMultiOption(
          'ignore',
          abbr: 'i',
          help: 'Patterns to ignore (can be used multiple times)',
          defaultsTo: <String>[],
        )
        ..addOption(
          'sort-by',
          help: 'Sort entries by (name, size)',
          defaultsTo: 'name',
          allowed: ['name', 'size'],
          allowedHelp: {
            'name': 'Sort entries by name',
            'size': 'Sort entries by size (requires --size flag)',
          },
        )
        ..addOption(
          'sort-direction',
          help: 'Sort direction (asc, desc)',
          defaultsTo: 'asc',
          allowed: ['asc', 'desc'],
          allowedHelp: {
            'asc': 'Sort in ascending order',
            'desc': 'Sort in descending order',
          },
        )
        ..addOption(
          'level',
          abbr: 'l',
          help: 'Maximum directory depth to display',
          valueHelp: 'n',
        );

  try {
    final results = parser.parse(args);

    // Parse sort by
    SortBy sortBy;
    switch (results['sort-by'] as String) {
      case 'size':
        sortBy = SortBy.size;
        break;
      case 'name':
      default:
        sortBy = SortBy.name;
        break;
    }

    // Parse sort direction
    SortDirection sortDirection;
    switch (results['sort-direction'] as String) {
      case 'desc':
        sortDirection = SortDirection.descending;
        break;
      case 'asc':
      default:
        sortDirection = SortDirection.ascending;
        break;
    }

    // Parse max level
    int? maxLevel;
    final levelOption = results['level'];
    if (levelOption != null) {
      try {
        maxLevel = int.parse(levelOption as String);
        if (maxLevel < 0) {
          print(
            ConsoleColors.warning(
              'Warning: Level must be non-negative. Using unlimited depth.',
            ),
          );
          maxLevel = null;
        }
      } catch (e) {
        print(
          ConsoleColors.warning(
            'Warning: Invalid level value. Using unlimited depth.',
          ),
        );
      }
    }

    return CliOptions(
      showSizes: results['size'] as bool,
      showHelp: results['help'] as bool,
      ignorePatterns: results['ignore'] as List<String>,
      sortBy: sortBy,
      sortDirection: sortDirection,
      suppressErrors: results['quiet'] as bool,
      maxLevel: maxLevel,
    );
  } catch (e) {
    // In case of invalid arguments, show help and exit
    print(ConsoleColors.error('Error: Invalid arguments provided.'));
    _printUsage(parser);
    exit(1);
  }
}