Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

OptionParser.Simple is now based in binaries. Closes #208 #220

Merged
merged 1 commit into from

2 participants

@rafaelfranca

No description provided.

@josevalim josevalim merged commit 68c5f36 into from
@josevalim
Owner

Beautiful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 28 deletions.
  1. +15 −15 lib/option_parser/simple.ex
  2. +13 −13 test/elixir/option_parser/simple_test.exs
View
30 lib/option_parser/simple.ex
@@ -6,21 +6,21 @@ defmodule OptionParser.Simple do
## Example
- OptionParser.Simple.parse(['--debug'])
+ OptionParser.Simple.parse(["--debug"])
#=> { [debug: true], [] }
- OptionParser.Simple.parse(['--source', 'lib'])
- #=> { [source: 'lib'], [] }
+ OptionParser.Simple.parse(["--source", "lib"])
+ #=> { [source: "lib"], [] }
- OptionParser.Simple.parse(['--source', 'lib', 'test/enum_test.exs'])
- #=> { [source: 'lib'], ['test/enum_test.exs'] }
+ OptionParser.Simple.parse(["--source", "lib", "test/enum_test.exs"])
+ #=> { [source: "lib"], ["test/enum_test.exs"] }
"""
- def parse(['-' ++ option,h|t], dict // [], args // []) do
+ def parse([<<?-, option|binary>>, h|t], dict // [], args // []) do
option = normalize_option(option)
case h do
- match: '-' ++ _
+ match: <<?-, _|binary>>
dict = Keyword.put dict, option, true
parse([h|t], dict, args)
else:
@@ -29,31 +29,31 @@ defmodule OptionParser.Simple do
end
end
- def parse(['-' ++ option], dict, args) do
+ def parse([<<?-, option|binary>>], dict, args) do
dict = Keyword.put dict, normalize_option(option), true
{ dict, args }
end
def parse(value, dict, args) do
- { dict, List.concat args, value }
+ { dict, List.concat(args, value) }
end
## Helpers
- defp key_value(key, boolean, dict) when boolean == 'false' \
- when boolean == 'true' do
- Keyword.put dict, key, list_to_atom(boolean)
+ defp key_value(key, boolean, dict) when boolean == "false" \
+ when boolean == "true" do
+ Keyword.put dict, key, binary_to_atom(boolean, :utf8)
end
defp key_value(key, value, dict) do
Keyword.put dict, key, value
end
- defp normalize_option('-' ++ option) do
- list_to_atom(option)
+ defp normalize_option(<<?-, option|binary>>) do
+ binary_to_atom(option, :utf8)
end
defp normalize_option(option) do
- list_to_atom(option)
+ binary_to_atom(option, :utf8)
end
end
View
26 test/elixir/option_parser/simple_test.exs
@@ -4,45 +4,45 @@ defmodule OptionParser.SimpleTest do
use ExUnit.Case
test "parses boolean option" do
- assert_equal { [docs: true], [] }, OptionParser.Simple.parse(['--docs'])
+ assert_equal { [docs: true], [] }, OptionParser.Simple.parse(["--docs"])
end
test "parses alias boolean option" do
- assert_equal { [d: true], [] }, OptionParser.Simple.parse(['-d'])
+ assert_equal { [d: true], [] }, OptionParser.Simple.parse(["-d"])
end
test "parses more than one boolean options" do
- assert_equal { [docs: true, compile: true], [] }, OptionParser.Simple.parse(['--docs', '--compile'])
+ assert_equal { [docs: true, compile: true], [] }, OptionParser.Simple.parse(["--docs", "--compile"])
end
test "parses key/value option" do
- assert_equal { [source: 'form_docs/'], [] }, OptionParser.Simple.parse(['--source', 'form_docs/'])
+ assert_equal { [source: "form_docs/"], [] }, OptionParser.Simple.parse(["--source", "form_docs/"])
end
test "parses alias key/value option" do
- assert_equal { [s: 'from_docs/'], [] }, OptionParser.Simple.parse(['-s', 'from_docs/'])
+ assert_equal { [s: "from_docs/"], [] }, OptionParser.Simple.parse(["-s", "from_docs/"])
end
test "parses key/value option when value is false" do
- assert_equal { [docs: false], [] }, OptionParser.Simple.parse(['--docs', 'false'])
+ assert_equal { [docs: false], [] }, OptionParser.Simple.parse(["--docs", "false"])
end
test "parses key/value option when value is true" do
- assert_equal { [docs: true], [] }, OptionParser.Simple.parse(['--docs', 'true'])
+ assert_equal { [docs: true], [] }, OptionParser.Simple.parse(["--docs", "true"])
end
test "parses more than one key/value options" do
- options = OptionParser.Simple.parse(['--source', 'from_docs/', '--docs', 'false'])
- assert_equal { [docs: false, source: 'from_docs/'], [] }, options
+ options = OptionParser.Simple.parse(["--source", "from_docs/", "--docs", "false"])
+ assert_equal { [docs: false, source: "from_docs/"], [] }, options
end
test "parses mixed options" do
- options = OptionParser.Simple.parse(['--source', 'from_docs/', '--docs', 'false', '--compile', '-x'])
- assert_equal { [docs: false, source: 'from_docs/', compile: true, x: true], [] }, options
+ options = OptionParser.Simple.parse(["--source", "from_docs/", "--docs", "false", "--compile", "-x"])
+ assert_equal { [docs: false, source: "from_docs/", compile: true, x: true], [] }, options
end
test "ignores not option arguments" do
- options = OptionParser.Simple.parse(['--source', 'from_docs/', 'test/enum_test.exs'])
- assert_equal { [source: 'from_docs/'], ['test/enum_test.exs'] }, options
+ options = OptionParser.Simple.parse(["--source", "from_docs/", "test/enum_test.exs"])
+ assert_equal { [source: "from_docs/"], ["test/enum_test.exs"] }, options
end
end
Something went wrong with that request. Please try again.