Skip to content
Browse files

Rename escript and add command line options

  • Loading branch information...
1 parent f5ab088 commit 4a6835d61daa8e062e6335c7b90d0ebd02e88263 @dreverri dreverri committed Jul 13, 2012
Showing with 38 additions and 3 deletions.
  1. +4 −2 erl_src/rebar.config
  2. +1 −1 erl_src/src/{eth.app.src → config_to_json.app.src}
  3. +33 −0 erl_src/src/config_to_json.erl
View
6 erl_src/rebar.config
@@ -1,6 +1,8 @@
{deps, [
{jsx, "1.3.1",
- {git, "git://github.com/talentdeficit/jsx.git", {tag, "v1.3.1"}}}
+ {git, "git://github.com/talentdeficit/jsx.git", {tag, "v1.3.1"}}},
+ {getopt, "0.4.4",
+ {git, "git://github.com/jcomellas/getopt.git", {tag, "v0.4.4"}}}
]}.
-{escript_incl_apps, [jsx]}.
+{escript_incl_apps, [jsx, getopt]}.
View
2 erl_src/src/eth.app.src → erl_src/src/config_to_json.app.src
@@ -1,4 +1,4 @@
-{application, eth,
+{application, config_to_json,
[
{description, ""},
{vsn, "1"},
View
33 erl_src/src/config_to_json.erl
@@ -0,0 +1,33 @@
+-module(config_to_json).
+
+-export([main/1]).
+
+main([]) ->
+ usage();
+
+main(Args) ->
+ case getopt:parse(option_spec_list(), Args) of
+ {ok, {Options, _}} ->
+ ConfigFile = proplists:get_value(config_file, Options),
+ {ok, [Terms]} = file:consult(ConfigFile),
+ Config = eth:convert(Terms),
+ Json = jsx:encode(Config),
+ Output = case proplists:get_value(prettify, Options) of
+ true -> jsx:prettify(Json);
+ _ -> Json
+ end,
+ io:format("~s", [Output]);
+ {error, {Reason, Data}} ->
+ io:format("Error: ~s ~p~n~n", [Reason, Data]),
+ usage()
+ end.
+
+usage() ->
+ getopt:usage(option_spec_list(), filename:basename(escript:script_name())),
+ halt(1).
+
+option_spec_list() ->
+ [
+ {prettify, $p, "prettify", boolean, "Prettify JSON output"},
+ {config_file, undefined, undefined, string, "Config file to parse"}
+ ].

0 comments on commit 4a6835d

Please sign in to comment.
Something went wrong with that request. Please try again.