<commit_before>// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';

import 'package:dart2wasm/compiler_options.dart';

class Option<T> {
  final String name;
  final void Function(ArgParser a) applyToParser;
  final void Function(CompilerOptions o, T v) _applyToOptions;
  final T Function(dynamic v) converter;

  void applyToOptions(CompilerOptions o, dynamic v) =>
      _applyToOptions(o, converter(v));

  Option(this.name, this.applyToParser, this._applyToOptions, this.converter);
}

class Flag extends Option<bool> {
  Flag(String name, void applyToOptions(CompilerOptions o, bool v),
      {String? abbr,
      String? help,
      bool? defaultsTo = false,
      bool negatable = true})
      : super(
            name,
            (a) => a.addFlag(name,
                abbr: abbr,
                help: help,
                defaultsTo: defaultsTo,
                negatable: negatable),
            applyToOptions,
            (v) => v == 'true' ? true : false);
}

class ValueOption<T> extends Option<T> {
  ValueOption(String name, void applyToOptions(CompilerOptions o, T v),
      T converter(dynamic v), {String? defaultsTo})
      : super(name, (a) => a.addOption(name, defaultsTo: defaultsTo),
            applyToOptions, converter);
}

class IntOption extends ValueOption<int> {
  IntOption(String name, void applyToOptions(CompilerOptions o, int v),
      {String? defaultsTo})
      : super(name, applyToOptions, (v) => int.parse(v),
            defaultsTo: defaultsTo);
}

class UriOption extends ValueOption<Uri> {
  UriOption(String name, void applyToOptions(CompilerOptions o, Uri v),
      {String? defaultsTo})
      : super(name, applyToOptions, (v) => Uri.file(Directory(v).absolute.path),
            defaultsTo: defaultsTo);
}
<commit_msg>[dart2wasm] Fix parsing of commandline flag options<commit_after>// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';

import 'package:dart2wasm/compiler_options.dart';

class Option<T> {
  final String name;
  final void Function(ArgParser a) applyToParser;
  final void Function(CompilerOptions o, T v) _applyToOptions;
  final T Function(dynamic v) converter;

  void applyToOptions(CompilerOptions o, dynamic v) =>
      _applyToOptions(o, converter(v));

  Option(this.name, this.applyToParser, this._applyToOptions, this.converter);
}

class Flag extends Option<bool> {
  Flag(String name, void applyToOptions(CompilerOptions o, bool v),
      {String? abbr,
      String? help,
      bool? defaultsTo = false,
      bool negatable = true})
      : super(
            name,
            (a) => a.addFlag(name,
                abbr: abbr,
                help: help,
                defaultsTo: defaultsTo,
                negatable: negatable),
            applyToOptions,
            (v) => v);
}

class ValueOption<T> extends Option<T> {
  ValueOption(String name, void applyToOptions(CompilerOptions o, T v),
      T converter(dynamic v), {String? defaultsTo})
      : super(name, (a) => a.addOption(name, defaultsTo: defaultsTo),
            applyToOptions, converter);
}

class IntOption extends ValueOption<int> {
  IntOption(String name, void applyToOptions(CompilerOptions o, int v),
      {String? defaultsTo})
      : super(name, applyToOptions, (v) => int.parse(v),
            defaultsTo: defaultsTo);
}

class UriOption extends ValueOption<Uri> {
  UriOption(String name, void applyToOptions(CompilerOptions o, Uri v),
      {String? defaultsTo})
      : super(name, applyToOptions, (v) => Uri.file(Directory(v).absolute.path),
            defaultsTo: defaultsTo);
}

{'fraction_non_alphanumeric': 0.10879919779393332, 'fraction_numerical': 0.0027575833542241163, 'mean_word_length': 3.1605839416058394, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6175188', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1166, 'n_words': 355}