<commit_before>defmodule OptionParser.Simple do

  @doc """
  Parses the argv and returns one Tuple with parsed options
  and the arguments.

  ## Example

      OptionParser.Simple.parse(["--debug"])
      #=> { [debug: true], [] }

      OptionParser.Simple.parse(["--source", "lib"])
      #=> { [source: "lib"], [] }

      OptionParser.Simple.parse(["--source", "lib", "test/enum_test.exs"])
      #=> { [source: "lib"], ["test/enum_test.exs"] }

  """
  def parse([<<?-, option|:binary>>, h|t], aliases // [], dict // [], args // []) do
    option = normalize_option(option, aliases)

    case h do
    match: <<?-, _|:binary>>
      dict = Keyword.put dict, option, true
      parse([h|t], aliases, dict, args)
    else:
      dict = key_value(option, h, dict)
      parse(t, aliases, dict, args)
    end
  end

  def parse([<<?-, option|:binary>>], aliases, dict, args) do
    option = normalize_option(option, aliases)
    dict = Keyword.put dict, option, true
    { dict, args }
  end

  def parse(value, _, dict, args) do
    { dict, List.concat(args, value) }
  end

  ## Helpers

  defp key_value(key, boolean, dict) when boolean == "false" \
                                     when boolean == "true" do
    Keyword.put dict, key, binary_to_atom(boolean)
  end

  defp key_value(key, value, dict) do
    Keyword.put dict, key, value
  end

  defp normalize_option(<<?-, option|:binary>>, aliases) do
    option = binary_to_atom(option)
    aliases[option] || option
  end

  defp normalize_option(option, aliases) do
    option = binary_to_atom(option)
    aliases[option] || option
  end
end<commit_msg>Decouple the API from internal implementation on OptionParser.Simple<commit_after>defmodule OptionParser.Simple do

  @doc """
  Parses the argv and returns one Tuple with parsed options
  and the arguments.

  ## Example

      OptionParser.Simple.parse(["--debug"])
      #=> { [debug: true], [] }

      OptionParser.Simple.parse(["--source", "lib"])
      #=> { [source: "lib"], [] }

      OptionParser.Simple.parse(["--source", "lib", "test/enum_test.exs"])
      #=> { [source: "lib"], ["test/enum_test.exs"] }

  """
  def parse(options, aliases // []) when is_list(options) and is_list(aliases) do
    parse(options, aliases, [], [])
  end

  ## Helpers

  defp parse([<<?-, option|:binary>>, h|t], aliases, dict, args) do
    option = normalize_option(option, aliases)

    case h do
    match: <<?-, _|:binary>>
      dict = Keyword.put dict, option, true
      parse([h|t], aliases, dict, args)
    else:
      dict = key_value(option, h, dict)
      parse(t, aliases, dict, args)
    end
  end

  defp parse([<<?-, option|:binary>>], aliases, dict, args) do
    option = normalize_option(option, aliases)
    dict = Keyword.put dict, option, true
    { dict, args }
  end

  defp parse(value, _, dict, args) do
    { dict, List.concat(args, value) }
  end

  defp key_value(key, boolean, dict) when boolean in ["false", "true"] do
    Keyword.put dict, key, binary_to_atom(boolean)
  end

  defp key_value(key, value, dict) do
    Keyword.put dict, key, value
  end

  defp normalize_option(<<?-, option|:binary>>, aliases) do
    option = binary_to_atom(option)
    aliases[option] || option
  end

  defp normalize_option(option, aliases) do
    option = binary_to_atom(option)
    aliases[option] || option
  end
end
{'fraction_non_alphanumeric': 0.15607281408534765, 'fraction_numerical': 0.0, 'mean_word_length': 3.0434258142340167, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '489121', 'n_tokens_mistral': 1152, 'n_tokens_neox': 1107, 'n_words': 298}