Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Apr 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/option_parser/simple.ex
Expand Up @@ -6,21 +6,21 @@ defmodule OptionParser.Simple do


## Example ## Example


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


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


OptionParser.Simple.parse(['--source', 'lib', 'test/enum_test.exs']) OptionParser.Simple.parse(["--source", "lib", "test/enum_test.exs"])
#=> { [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) option = normalize_option(option)


case h do case h do
match: '-' ++ _ match: <<?-, _|binary>>
dict = Keyword.put dict, option, true dict = Keyword.put dict, option, true
parse([h|t], dict, args) parse([h|t], dict, args)
else: else:
Expand All @@ -29,31 +29,31 @@ defmodule OptionParser.Simple do
end end
end end


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


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


## Helpers ## Helpers


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


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


defp normalize_option('-' ++ option) do defp normalize_option(<<?-, option|binary>>) do
list_to_atom(option) binary_to_atom(option, :utf8)
end end


defp normalize_option(option) do defp normalize_option(option) do
list_to_atom(option) binary_to_atom(option, :utf8)
end end
end end
26 changes: 13 additions & 13 deletions test/elixir/option_parser/simple_test.exs
Expand Up @@ -4,45 +4,45 @@ defmodule OptionParser.SimpleTest do
use ExUnit.Case use ExUnit.Case


test "parses boolean option" do test "parses boolean option" do
assert_equal { [docs: true], [] }, OptionParser.Simple.parse(['--docs']) assert_equal { [docs: true], [] }, OptionParser.Simple.parse(["--docs"])
end end


test "parses alias boolean option" do test "parses alias boolean option" do
assert_equal { [d: true], [] }, OptionParser.Simple.parse(['-d']) assert_equal { [d: true], [] }, OptionParser.Simple.parse(["-d"])
end end


test "parses more than one boolean options" do 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 end


test "parses key/value option" do 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 end


test "parses alias key/value option" do 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 end


test "parses key/value option when value is false" do 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 end


test "parses key/value option when value is true" do 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 end


test "parses more than one key/value options" do test "parses more than one key/value options" do
options = OptionParser.Simple.parse(['--source', 'from_docs/', '--docs', 'false']) options = OptionParser.Simple.parse(["--source", "from_docs/", "--docs", "false"])
assert_equal { [docs: false, source: 'from_docs/'], [] }, options assert_equal { [docs: false, source: "from_docs/"], [] }, options
end end


test "parses mixed options" do test "parses mixed options" do
options = OptionParser.Simple.parse(['--source', 'from_docs/', '--docs', 'false', '--compile', '-x']) options = OptionParser.Simple.parse(["--source", "from_docs/", "--docs", "false", "--compile", "-x"])
assert_equal { [docs: false, source: 'from_docs/', compile: true, x: true], [] }, options assert_equal { [docs: false, source: "from_docs/", compile: true, x: true], [] }, options
end end


test "ignores not option arguments" do test "ignores not option arguments" do
options = OptionParser.Simple.parse(['--source', 'from_docs/', 'test/enum_test.exs']) options = OptionParser.Simple.parse(["--source", "from_docs/", "test/enum_test.exs"])
assert_equal { [source: 'from_docs/'], ['test/enum_test.exs'] }, options assert_equal { [source: "from_docs/"], ["test/enum_test.exs"] }, options
end end
end end