Initial commit (see review reques in rhbz #639263)
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
This commit is contained in:
parent
e102585077
commit
8ead4a2357
|
@ -0,0 +1 @@
|
||||||
|
/basho-rebar-RELEASE-1-327-g90058c7.tar.gz
|
|
@ -0,0 +1,92 @@
|
||||||
|
%global realname rebar
|
||||||
|
%global debug_package %{nil}
|
||||||
|
%global git_tag 90058c7
|
||||||
|
|
||||||
|
|
||||||
|
Name: erlang-%{realname}
|
||||||
|
Version: 2
|
||||||
|
Release: 2.20101120git%{git_tag}%{?dist}
|
||||||
|
Summary: Erlang Build Tools
|
||||||
|
Group: Development/Tools
|
||||||
|
License: MIT
|
||||||
|
URL: https://github.com/basho/rebar
|
||||||
|
# wget --no-check-certificate https://github.com/basho/rebar/tarball/90058c7
|
||||||
|
Source0: basho-%{realname}-RELEASE-1-327-g%{git_tag}.tar.gz
|
||||||
|
Source1: rebar.escript
|
||||||
|
Patch1: rebar-0001-No-need-to-create-bundle.patch
|
||||||
|
Patch2: rebar-0002-Remove-bundled-mustache.patch
|
||||||
|
Patch3: rebar-0003-Remove-bundled-getopt.patch
|
||||||
|
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
|
||||||
|
BuildRequires: erlang-erts >= R13B-03
|
||||||
|
BuildRequires: erlang-reltool
|
||||||
|
Requires: erlang-asn1
|
||||||
|
Requires: erlang-compiler
|
||||||
|
Requires: erlang-crypto
|
||||||
|
Requires: erlang-dialyzer
|
||||||
|
Requires: erlang-edoc
|
||||||
|
Requires: erlang-erlydtl
|
||||||
|
Requires: erlang-erts
|
||||||
|
Requires: erlang-eunit
|
||||||
|
Requires: erlang-getopt
|
||||||
|
Requires: erlang-kernel
|
||||||
|
Requires: erlang-lfe
|
||||||
|
Requires: erlang-mustache
|
||||||
|
Requires: erlang-neotoma
|
||||||
|
Requires: erlang-protobuffs
|
||||||
|
Requires: erlang-reltool
|
||||||
|
Requires: erlang-snmp
|
||||||
|
Requires: erlang-stdlib
|
||||||
|
Requires: erlang-syntax_tools
|
||||||
|
Requires: erlang-tools
|
||||||
|
Provides: %{realname} = %{version}-%{release}
|
||||||
|
|
||||||
|
|
||||||
|
%description
|
||||||
|
Erlang Build Tools.
|
||||||
|
|
||||||
|
|
||||||
|
%prep
|
||||||
|
%setup -q -n basho-%{realname}-%{git_tag}
|
||||||
|
%patch1 -p1 -b .no_bundle
|
||||||
|
%patch2 -p1 -b .remove_bundled_mustache
|
||||||
|
%patch3 -p1 -b .remove_bundled_getopt
|
||||||
|
|
||||||
|
|
||||||
|
%build
|
||||||
|
./bootstrap
|
||||||
|
|
||||||
|
|
||||||
|
%install
|
||||||
|
rm -rf $RPM_BUILD_ROOT
|
||||||
|
install -D -p -m 0755 %{SOURCE1} $RPM_BUILD_ROOT%{_bindir}/rebar
|
||||||
|
mkdir -p $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin
|
||||||
|
mkdir -p $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/include
|
||||||
|
install -m 644 ebin/%{realname}.app $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin
|
||||||
|
install -m 644 ebin/*.beam $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/ebin
|
||||||
|
install -m 644 include/*.hrl $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/include
|
||||||
|
cp -a priv $RPM_BUILD_ROOT%{_libdir}/erlang/lib/%{realname}-%{version}/
|
||||||
|
|
||||||
|
|
||||||
|
%clean
|
||||||
|
rm -rf $RPM_BUILD_ROOT
|
||||||
|
|
||||||
|
|
||||||
|
%files
|
||||||
|
%defattr(-,root,root,-)
|
||||||
|
%doc THANKS rebar.config.sample
|
||||||
|
%{_bindir}/rebar
|
||||||
|
%{_libdir}/erlang/lib/%{realname}-%{version}
|
||||||
|
|
||||||
|
|
||||||
|
%changelog
|
||||||
|
* Sat Nov 20 2010 Peter Lemenkov <lemenkov@gmail.com> - 2-2.20101120git90058c7
|
||||||
|
- Removed bundled mustache and getopt
|
||||||
|
- Fixed license tag
|
||||||
|
- Removed wrong license text from package
|
||||||
|
- Simplified %%files section
|
||||||
|
- Fixed links (project was moved to GitHub)
|
||||||
|
- Changed versioning scheme (post-release)
|
||||||
|
|
||||||
|
* Sun Sep 5 2010 Peter Lemenkov <lemenkov@gmail.com> - 2-1
|
||||||
|
- Initial build
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
From 0b38555707bd90d102b538a0055925a7493c74d5 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
Date: Tue, 28 Sep 2010 23:04:36 +0400
|
||||||
|
Subject: [PATCH 1/3] No need to create bundle
|
||||||
|
|
||||||
|
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
---
|
||||||
|
bootstrap | 38 +-------------------------------------
|
||||||
|
1 files changed, 1 insertions(+), 37 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/bootstrap b/bootstrap
|
||||||
|
index 5be3183..634e1f6 100755
|
||||||
|
--- a/bootstrap
|
||||||
|
+++ b/bootstrap
|
||||||
|
@@ -51,43 +51,7 @@ main(Args) ->
|
||||||
|
true = code:add_path("ebin"),
|
||||||
|
|
||||||
|
%% Run rebar to do proper .app validation and such
|
||||||
|
- rebar:main(["compile"] ++ Args),
|
||||||
|
-
|
||||||
|
- %% Read the contents of the files in ebin and templates; note that we place
|
||||||
|
- %% all the beam files at the top level of the code archive so that code loading
|
||||||
|
- %% works properly.
|
||||||
|
- Files = load_files("*", "ebin") ++ load_files("priv/templates/*", "."),
|
||||||
|
-
|
||||||
|
- case zip:create("mem", Files, [memory]) of
|
||||||
|
- {ok, {"mem", ZipBin}} ->
|
||||||
|
- %% Archive was successfully created. Prefix that binary with our
|
||||||
|
- %% header and write to "rebar" file
|
||||||
|
- Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n", ZipBin/binary>>,
|
||||||
|
- case file:write_file("rebar", Script) of
|
||||||
|
- ok ->
|
||||||
|
- ok;
|
||||||
|
- {error, WriteError} ->
|
||||||
|
- io:format("Failed to write rebar script: ~p\n", [WriteError]),
|
||||||
|
- halt(1)
|
||||||
|
- end;
|
||||||
|
- {error, ZipError} ->
|
||||||
|
- io:format("Failed to construct rebar script archive: ~p\n", [ZipError]),
|
||||||
|
- halt(1)
|
||||||
|
- end,
|
||||||
|
-
|
||||||
|
- %% Finally, update executable perms for our script
|
||||||
|
- case os:type() of
|
||||||
|
- {unix,_} ->
|
||||||
|
- [] = os:cmd("chmod u+x rebar"),
|
||||||
|
- ok;
|
||||||
|
- _ ->
|
||||||
|
- ok
|
||||||
|
- end,
|
||||||
|
-
|
||||||
|
- %% Add a helpful message
|
||||||
|
- io:format("Congratulations! You now have a self-contained script called \"rebar\" in\n"
|
||||||
|
- "your current working directory. Place this script anywhere in your path\n"
|
||||||
|
- "and you can use rebar to build OTP-compliant apps.\n").
|
||||||
|
+ rebar:main(["compile"] ++ Args).
|
||||||
|
|
||||||
|
rm(Path) ->
|
||||||
|
NativePath = filename:nativename(Path),
|
||||||
|
--
|
||||||
|
1.7.2.3
|
||||||
|
|
|
@ -0,0 +1,254 @@
|
||||||
|
From 3cb4ccb50a6f1175e3b2a6723641befd3b8cb444 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
Date: Sat, 20 Nov 2010 16:36:45 +0300
|
||||||
|
Subject: [PATCH 2/3] Remove bundled mustache
|
||||||
|
|
||||||
|
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
---
|
||||||
|
ebin/rebar.app | 3 +-
|
||||||
|
src/mustache.erl | 219 ------------------------------------------------------
|
||||||
|
2 files changed, 1 insertions(+), 221 deletions(-)
|
||||||
|
delete mode 100644 src/mustache.erl
|
||||||
|
|
||||||
|
diff --git a/ebin/rebar.app b/ebin/rebar.app
|
||||||
|
index 6f9b6f5..75aaec1 100644
|
||||||
|
--- a/ebin/rebar.app
|
||||||
|
+++ b/ebin/rebar.app
|
||||||
|
@@ -33,8 +33,7 @@
|
||||||
|
rebar_templater,
|
||||||
|
rebar_utils,
|
||||||
|
rebar_xref,
|
||||||
|
- getopt,
|
||||||
|
- mustache ]},
|
||||||
|
+ getopt ]},
|
||||||
|
{registered, []},
|
||||||
|
{applications, [kernel,
|
||||||
|
stdlib,
|
||||||
|
diff --git a/src/mustache.erl b/src/mustache.erl
|
||||||
|
deleted file mode 100644
|
||||||
|
index df81aed..0000000
|
||||||
|
--- a/src/mustache.erl
|
||||||
|
+++ /dev/null
|
||||||
|
@@ -1,219 +0,0 @@
|
||||||
|
-%% The MIT License
|
||||||
|
-%%
|
||||||
|
-%% Copyright (c) 2009 Tom Preston-Werner <tom@mojombo.com>
|
||||||
|
-%%
|
||||||
|
-%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
-%% of this software and associated documentation files (the "Software"), to deal
|
||||||
|
-%% in the Software without restriction, including without limitation the rights
|
||||||
|
-%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
-%% copies of the Software, and to permit persons to whom the Software is
|
||||||
|
-%% furnished to do so, subject to the following conditions:
|
||||||
|
-%%
|
||||||
|
-%% The above copyright notice and this permission notice shall be included in
|
||||||
|
-%% all copies or substantial portions of the Software.
|
||||||
|
-%%
|
||||||
|
-%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
-%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
-%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
-%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
-%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
-%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
-%% THE SOFTWARE.
|
||||||
|
-
|
||||||
|
-%% See the README at http://github.com/mojombo/mustache.erl for additional
|
||||||
|
-%% documentation and usage examples.
|
||||||
|
-
|
||||||
|
--module(mustache). %% v0.1.0
|
||||||
|
--author("Tom Preston-Werner").
|
||||||
|
--export([compile/1, compile/2, render/1, render/2, render/3, get/2, get/3, escape/1, start/1]).
|
||||||
|
-
|
||||||
|
--record(mstate, {mod = undefined,
|
||||||
|
- section_re = undefined,
|
||||||
|
- tag_re = undefined}).
|
||||||
|
-
|
||||||
|
-compile(Body) when is_list(Body) ->
|
||||||
|
- State = #mstate{},
|
||||||
|
- CompiledTemplate = pre_compile(Body, State),
|
||||||
|
- % io:format("~p~n~n", [CompiledTemplate]),
|
||||||
|
- % io:format(CompiledTemplate ++ "~n", []),
|
||||||
|
- {ok, Tokens, _} = erl_scan:string(CompiledTemplate),
|
||||||
|
- {ok, [Form]} = erl_parse:parse_exprs(Tokens),
|
||||||
|
- Bindings = erl_eval:new_bindings(),
|
||||||
|
- {value, Fun, _} = erl_eval:expr(Form, Bindings),
|
||||||
|
- Fun;
|
||||||
|
-compile(Mod) ->
|
||||||
|
- TemplatePath = template_path(Mod),
|
||||||
|
- compile(Mod, TemplatePath).
|
||||||
|
-
|
||||||
|
-compile(Mod, File) ->
|
||||||
|
- code:purge(Mod),
|
||||||
|
- {module, _} = code:load_file(Mod),
|
||||||
|
- {ok, TemplateBin} = file:read_file(File),
|
||||||
|
- Template = re:replace(TemplateBin, "\"", "\\\\\"", [global, {return,list}]),
|
||||||
|
- State = #mstate{mod = Mod},
|
||||||
|
- CompiledTemplate = pre_compile(Template, State),
|
||||||
|
- % io:format("~p~n~n", [CompiledTemplate]),
|
||||||
|
- % io:format(CompiledTemplate ++ "~n", []),
|
||||||
|
- {ok, Tokens, _} = erl_scan:string(CompiledTemplate),
|
||||||
|
- {ok, [Form]} = erl_parse:parse_exprs(Tokens),
|
||||||
|
- Bindings = erl_eval:new_bindings(),
|
||||||
|
- {value, Fun, _} = erl_eval:expr(Form, Bindings),
|
||||||
|
- Fun.
|
||||||
|
-
|
||||||
|
-render(Mod) ->
|
||||||
|
- TemplatePath = template_path(Mod),
|
||||||
|
- render(Mod, TemplatePath).
|
||||||
|
-
|
||||||
|
-render(Body, Ctx) when is_list(Body) ->
|
||||||
|
- TFun = compile(Body),
|
||||||
|
- render(undefined, TFun, Ctx);
|
||||||
|
-render(Mod, File) when is_list(File) ->
|
||||||
|
- render(Mod, File, dict:new());
|
||||||
|
-render(Mod, CompiledTemplate) ->
|
||||||
|
- render(Mod, CompiledTemplate, dict:new()).
|
||||||
|
-
|
||||||
|
-render(Mod, File, Ctx) when is_list(File) ->
|
||||||
|
- CompiledTemplate = compile(Mod, File),
|
||||||
|
- render(Mod, CompiledTemplate, Ctx);
|
||||||
|
-render(Mod, CompiledTemplate, Ctx) ->
|
||||||
|
- Ctx2 = dict:store('__mod__', Mod, Ctx),
|
||||||
|
- lists:flatten(CompiledTemplate(Ctx2)).
|
||||||
|
-
|
||||||
|
-pre_compile(T, State) ->
|
||||||
|
- SectionRE = "\{\{\#([^\}]*)}}\s*(.+?){{\/\\1\}\}\s*",
|
||||||
|
- {ok, CompiledSectionRE} = re:compile(SectionRE, [dotall]),
|
||||||
|
- TagRE = "\{\{(#|=|!|<|>|\{)?(.+?)\\1?\}\}+",
|
||||||
|
- {ok, CompiledTagRE} = re:compile(TagRE, [dotall]),
|
||||||
|
- State2 = State#mstate{section_re = CompiledSectionRE, tag_re = CompiledTagRE},
|
||||||
|
- "fun(Ctx) -> " ++
|
||||||
|
- "CFun = fun(A, B) -> A end, " ++
|
||||||
|
- compiler(T, State2) ++ " end.".
|
||||||
|
-
|
||||||
|
-compiler(T, State) ->
|
||||||
|
- Res = re:run(T, State#mstate.section_re),
|
||||||
|
- case Res of
|
||||||
|
- {match, [{M0, M1}, {N0, N1}, {C0, C1}]} ->
|
||||||
|
- Front = string:substr(T, 1, M0),
|
||||||
|
- Back = string:substr(T, M0 + M1 + 1),
|
||||||
|
- Name = string:substr(T, N0 + 1, N1),
|
||||||
|
- Content = string:substr(T, C0 + 1, C1),
|
||||||
|
- "[" ++ compile_tags(Front, State) ++
|
||||||
|
- " | [" ++ compile_section(Name, Content, State) ++
|
||||||
|
- " | [" ++ compiler(Back, State) ++ "]]]";
|
||||||
|
- nomatch ->
|
||||||
|
- compile_tags(T, State)
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-compile_section(Name, Content, State) ->
|
||||||
|
- Mod = State#mstate.mod,
|
||||||
|
- Result = compiler(Content, State),
|
||||||
|
- "fun() -> " ++
|
||||||
|
- "case mustache:get(" ++ Name ++ ", Ctx, " ++ atom_to_list(Mod) ++ ") of " ++
|
||||||
|
- "true -> " ++
|
||||||
|
- Result ++ "; " ++
|
||||||
|
- "false -> " ++
|
||||||
|
- "[]; " ++
|
||||||
|
- "List when is_list(List) -> " ++
|
||||||
|
- "[fun(Ctx) -> " ++ Result ++ " end(dict:merge(CFun, SubCtx, Ctx)) || SubCtx <- List]; " ++
|
||||||
|
- "Else -> " ++
|
||||||
|
- "throw({template, io_lib:format(\"Bad context for ~p: ~p\", [" ++ Name ++ ", Else])}) " ++
|
||||||
|
- "end " ++
|
||||||
|
- "end()".
|
||||||
|
-
|
||||||
|
-compile_tags(T, State) ->
|
||||||
|
- Res = re:run(T, State#mstate.tag_re),
|
||||||
|
- case Res of
|
||||||
|
- {match, [{M0, M1}, K, {C0, C1}]} ->
|
||||||
|
- Front = string:substr(T, 1, M0),
|
||||||
|
- Back = string:substr(T, M0 + M1 + 1),
|
||||||
|
- Content = string:substr(T, C0 + 1, C1),
|
||||||
|
- Kind = tag_kind(T, K),
|
||||||
|
- Result = compile_tag(Kind, Content, State),
|
||||||
|
- "[\"" ++ Front ++
|
||||||
|
- "\" | [" ++ Result ++
|
||||||
|
- " | " ++ compile_tags(Back, State) ++ "]]";
|
||||||
|
- nomatch ->
|
||||||
|
- "[\"" ++ T ++ "\"]"
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-tag_kind(_T, {-1, 0}) ->
|
||||||
|
- none;
|
||||||
|
-tag_kind(T, {K0, K1}) ->
|
||||||
|
- string:substr(T, K0 + 1, K1).
|
||||||
|
-
|
||||||
|
-compile_tag(none, Content, State) ->
|
||||||
|
- Mod = State#mstate.mod,
|
||||||
|
- "mustache:escape(mustache:get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ "))";
|
||||||
|
-compile_tag("{", Content, State) ->
|
||||||
|
- Mod = State#mstate.mod,
|
||||||
|
- "mustache:get(" ++ Content ++ ", Ctx, " ++ atom_to_list(Mod) ++ ")";
|
||||||
|
-compile_tag("!", _Content, _State) ->
|
||||||
|
- "[]".
|
||||||
|
-
|
||||||
|
-template_path(Mod) ->
|
||||||
|
- ModPath = code:which(Mod),
|
||||||
|
- re:replace(ModPath, "\.beam$", ".mustache", [{return, list}]).
|
||||||
|
-
|
||||||
|
-get(Key, Ctx) when is_list(Key) ->
|
||||||
|
- {ok, Mod} = dict:find('__mod__', Ctx),
|
||||||
|
- get(list_to_atom(Key), Ctx, Mod);
|
||||||
|
-get(Key, Ctx) ->
|
||||||
|
- {ok, Mod} = dict:find('__mod__', Ctx),
|
||||||
|
- get(Key, Ctx, Mod).
|
||||||
|
-
|
||||||
|
-get(Key, Ctx, Mod) when is_list(Key) ->
|
||||||
|
- get(list_to_atom(Key), Ctx, Mod);
|
||||||
|
-get(Key, Ctx, Mod) ->
|
||||||
|
- case dict:find(Key, Ctx) of
|
||||||
|
- {ok, Val} ->
|
||||||
|
- % io:format("From Ctx {~p, ~p}~n", [Key, Val]),
|
||||||
|
- to_s(Val);
|
||||||
|
- error ->
|
||||||
|
- case erlang:function_exported(Mod, Key, 1) of
|
||||||
|
- true ->
|
||||||
|
- Val = to_s(Mod:Key(Ctx)),
|
||||||
|
- % io:format("From Mod/1 {~p, ~p}~n", [Key, Val]),
|
||||||
|
- Val;
|
||||||
|
- false ->
|
||||||
|
- case erlang:function_exported(Mod, Key, 0) of
|
||||||
|
- true ->
|
||||||
|
- Val = to_s(Mod:Key()),
|
||||||
|
- % io:format("From Mod/0 {~p, ~p}~n", [Key, Val]),
|
||||||
|
- Val;
|
||||||
|
- false ->
|
||||||
|
- []
|
||||||
|
- end
|
||||||
|
- end
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-to_s(Val) when is_integer(Val) ->
|
||||||
|
- integer_to_list(Val);
|
||||||
|
-to_s(Val) when is_float(Val) ->
|
||||||
|
- io_lib:format("~.2f", [Val]);
|
||||||
|
-to_s(Val) when is_boolean(Val) ->
|
||||||
|
- Val;
|
||||||
|
-to_s(Val) when is_atom(Val) ->
|
||||||
|
- atom_to_list(Val);
|
||||||
|
-to_s(Val) ->
|
||||||
|
- Val.
|
||||||
|
-
|
||||||
|
-escape(HTML) ->
|
||||||
|
- escape(HTML, []).
|
||||||
|
-
|
||||||
|
-escape([], Acc) ->
|
||||||
|
- lists:reverse(Acc);
|
||||||
|
-escape(["<" | Rest], Acc) ->
|
||||||
|
- escape(Rest, lists:reverse("<", Acc));
|
||||||
|
-escape([">" | Rest], Acc) ->
|
||||||
|
- escape(Rest, lists:reverse(">", Acc));
|
||||||
|
-escape(["&" | Rest], Acc) ->
|
||||||
|
- escape(Rest, lists:reverse("&", Acc));
|
||||||
|
-escape([X | Rest], Acc) ->
|
||||||
|
- escape(Rest, [X | Acc]).
|
||||||
|
-
|
||||||
|
-%%---------------------------------------------------------------------------
|
||||||
|
-
|
||||||
|
-start([T]) ->
|
||||||
|
- Out = render(list_to_atom(T)),
|
||||||
|
- io:format(Out ++ "~n", []).
|
||||||
|
-
|
||||||
|
--
|
||||||
|
1.7.2.3
|
||||||
|
|
|
@ -0,0 +1,536 @@
|
||||||
|
From b2673b1ffc3b12652adef9f6453bca2b86247cad Mon Sep 17 00:00:00 2001
|
||||||
|
From: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
Date: Sat, 20 Nov 2010 16:40:15 +0300
|
||||||
|
Subject: [PATCH 3/3] Remove bundled getopt
|
||||||
|
|
||||||
|
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
|
||||||
|
---
|
||||||
|
ebin/rebar.app | 3 +-
|
||||||
|
src/getopt.erl | 501 --------------------------------------------------------
|
||||||
|
2 files changed, 1 insertions(+), 503 deletions(-)
|
||||||
|
delete mode 100644 src/getopt.erl
|
||||||
|
|
||||||
|
diff --git a/ebin/rebar.app b/ebin/rebar.app
|
||||||
|
index 75aaec1..8589819 100644
|
||||||
|
--- a/ebin/rebar.app
|
||||||
|
+++ b/ebin/rebar.app
|
||||||
|
@@ -32,8 +32,7 @@
|
||||||
|
rebar_subdirs,
|
||||||
|
rebar_templater,
|
||||||
|
rebar_utils,
|
||||||
|
- rebar_xref,
|
||||||
|
- getopt ]},
|
||||||
|
+ rebar_xref ]},
|
||||||
|
{registered, []},
|
||||||
|
{applications, [kernel,
|
||||||
|
stdlib,
|
||||||
|
diff --git a/src/getopt.erl b/src/getopt.erl
|
||||||
|
deleted file mode 100644
|
||||||
|
index bb7fae2..0000000
|
||||||
|
--- a/src/getopt.erl
|
||||||
|
+++ /dev/null
|
||||||
|
@@ -1,501 +0,0 @@
|
||||||
|
-%%%-------------------------------------------------------------------
|
||||||
|
-%%% @author Juan Jose Comellas <juanjo@comellas.org>
|
||||||
|
-%%% @copyright (C) 2009 Juan Jose Comellas
|
||||||
|
-%%% @doc Parses command line options with a format similar to that of GNU getopt.
|
||||||
|
-%%% @end
|
||||||
|
-%%%
|
||||||
|
-%%% This source file is subject to the New BSD License. You should have received
|
||||||
|
-%%% a copy of the New BSD license with this software. If not, it can be
|
||||||
|
-%%% retrieved from: http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
-%%%-------------------------------------------------------------------
|
||||||
|
--module(getopt).
|
||||||
|
--author('juanjo@comellas.org').
|
||||||
|
-
|
||||||
|
--export([parse/2, usage/2, usage/3, usage/4]).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--define(TAB_LENGTH, 8).
|
||||||
|
-%% Indentation of the help messages in number of tabs.
|
||||||
|
--define(INDENTATION, 3).
|
||||||
|
-
|
||||||
|
-%% Position of each field in the option specification tuple.
|
||||||
|
--define(OPT_NAME, 1).
|
||||||
|
--define(OPT_SHORT, 2).
|
||||||
|
--define(OPT_LONG, 3).
|
||||||
|
--define(OPT_ARG, 4).
|
||||||
|
--define(OPT_HELP, 5).
|
||||||
|
-
|
||||||
|
--define(IS_OPT_SPEC(Opt), (tuple_size(Opt) =:= ?OPT_HELP)).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% Atom indicating the data type that an argument can be converted to.
|
||||||
|
--type arg_type() :: 'atom' | 'binary' | 'boolean' | 'float' | 'integer' | 'string'.
|
||||||
|
-%% Data type that an argument can be converted to.
|
||||||
|
--type arg_value() :: atom() | binary() | boolean() | float() | integer() | string().
|
||||||
|
-%% Argument specification.
|
||||||
|
--type arg_spec() :: arg_type() | {arg_type(), arg_value()} | undefined.
|
||||||
|
-%% Option type and optional default argument.
|
||||||
|
--type simple_option() :: atom().
|
||||||
|
--type compound_option() :: {atom(), arg_value()}.
|
||||||
|
--type option() :: simple_option() | compound_option().
|
||||||
|
-%% Command line option specification.
|
||||||
|
--type option_spec() :: {
|
||||||
|
- Name :: atom(),
|
||||||
|
- Short :: char() | undefined,
|
||||||
|
- Long :: string() | undefined,
|
||||||
|
- ArgSpec :: arg_spec(),
|
||||||
|
- Help :: string() | undefined
|
||||||
|
- }.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Parse the command line options and arguments returning a list of tuples
|
||||||
|
-%% and/or atoms using the Erlang convention for sending options to a
|
||||||
|
-%% function.
|
||||||
|
--spec parse([option_spec()], string() | [string()]) ->
|
||||||
|
- {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
|
||||||
|
-parse(OptSpecList, CmdLine) ->
|
||||||
|
- try
|
||||||
|
- Args = if
|
||||||
|
- is_integer(hd(CmdLine)) ->
|
||||||
|
- string:tokens(CmdLine, " \t\n");
|
||||||
|
- true ->
|
||||||
|
- CmdLine
|
||||||
|
- end,
|
||||||
|
- parse(OptSpecList, [], [], 0, Args)
|
||||||
|
- catch
|
||||||
|
- throw: {error, {_Reason, _Data}} = Error ->
|
||||||
|
- Error
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec parse([option_spec()], [option()], [string()], integer(), [string()]) ->
|
||||||
|
- {ok, {[option()], [string()]}}.
|
||||||
|
-%% Process the option terminator.
|
||||||
|
-parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, ["--" | Tail]) ->
|
||||||
|
- % Any argument present after the terminator is not considered an option.
|
||||||
|
- {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc, Tail)}};
|
||||||
|
-%% Process long options.
|
||||||
|
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | OptArg] = OptStr | Tail]) ->
|
||||||
|
- parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
|
||||||
|
-%% Process short options.
|
||||||
|
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | [_Char | _] = OptArg] = OptStr | Tail]) ->
|
||||||
|
- parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
|
||||||
|
-%% Process non-option arguments.
|
||||||
|
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail]) ->
|
||||||
|
- case find_non_option_arg(OptSpecList, ArgPos) of
|
||||||
|
- {value, OptSpec} when ?IS_OPT_SPEC(OptSpec) ->
|
||||||
|
- parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc],
|
||||||
|
- ArgAcc, ArgPos + 1, Tail);
|
||||||
|
- false ->
|
||||||
|
- parse(OptSpecList, OptAcc, [Arg | ArgAcc], ArgPos, Tail)
|
||||||
|
- end;
|
||||||
|
-parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
|
||||||
|
- % Once we have completed gathering the options we add the ones that were
|
||||||
|
- % not present but had default arguments in the specification.
|
||||||
|
- {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Parse a long option, add it to the option accumulator and continue
|
||||||
|
-%% parsing the rest of the arguments recursively.
|
||||||
|
-%% A long option can have the following syntax:
|
||||||
|
-%% --foo Single option 'foo', no argument
|
||||||
|
-%% --foo=bar Single option 'foo', argument "bar"
|
||||||
|
-%% --foo bar Single option 'foo', argument "bar"
|
||||||
|
--spec parse_option_long([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
|
||||||
|
- {ok, {[option()], [string()]}}.
|
||||||
|
-parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptArg) ->
|
||||||
|
- case split_assigned_arg(OptArg) of
|
||||||
|
- {Long, Arg} ->
|
||||||
|
- % Get option that has its argument within the same string
|
||||||
|
- % separated by an equal ('=') character (e.g. "--port=1000").
|
||||||
|
- parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg);
|
||||||
|
-
|
||||||
|
- Long ->
|
||||||
|
- case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
|
||||||
|
- {value, {Name, _Short, Long, undefined, _Help}} ->
|
||||||
|
- parse(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
-
|
||||||
|
- {value, {_Name, _Short, Long, _ArgSpec, _Help} = OptSpec} ->
|
||||||
|
- % The option argument string is empty, but the option requires
|
||||||
|
- % an argument, so we look into the next string in the list.
|
||||||
|
- parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
|
||||||
|
- false ->
|
||||||
|
- throw({error, {invalid_option, OptStr}})
|
||||||
|
- end
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Parse an option where the argument is 'assigned' in the same string using
|
||||||
|
-%% the '=' character, add it to the option accumulator and continue parsing the
|
||||||
|
-%% rest of the arguments recursively. This syntax is only valid for long options.
|
||||||
|
--spec parse_option_assigned_arg([option_spec()], [option()], [string()], integer(),
|
||||||
|
- [string()], string(), string(), string()) ->
|
||||||
|
- {ok, {[option()], [string()]}}.
|
||||||
|
-parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg) ->
|
||||||
|
- case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
|
||||||
|
- {value, {_Name, _Short, Long, ArgSpec, _Help} = OptSpec} ->
|
||||||
|
- case ArgSpec of
|
||||||
|
- undefined ->
|
||||||
|
- throw({error, {invalid_option_arg, OptStr}});
|
||||||
|
- _ ->
|
||||||
|
- parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args)
|
||||||
|
- end;
|
||||||
|
- false ->
|
||||||
|
- throw({error, {invalid_option, OptStr}})
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Split an option string that may contain an option with its argument
|
||||||
|
-%% separated by an equal ('=') character (e.g. "port=1000").
|
||||||
|
--spec split_assigned_arg(string()) -> {Name :: string(), Arg :: string()} | string().
|
||||||
|
-split_assigned_arg(OptStr) ->
|
||||||
|
- split_assigned_arg(OptStr, OptStr, []).
|
||||||
|
-
|
||||||
|
-split_assigned_arg(_OptStr, [$= | Tail], Acc) ->
|
||||||
|
- {lists:reverse(Acc), Tail};
|
||||||
|
-split_assigned_arg(OptStr, [Char | Tail], Acc) ->
|
||||||
|
- split_assigned_arg(OptStr, Tail, [Char | Acc]);
|
||||||
|
-split_assigned_arg(OptStr, [], _Acc) ->
|
||||||
|
- OptStr.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Parse a short option, add it to the option accumulator and continue
|
||||||
|
-%% parsing the rest of the arguments recursively.
|
||||||
|
-%% A short option can have the following syntax:
|
||||||
|
-%% -a Single option 'a', no argument or implicit boolean argument
|
||||||
|
-%% -a foo Single option 'a', argument "foo"
|
||||||
|
-%% -afoo Single option 'a', argument "foo"
|
||||||
|
-%% -abc Multiple options: 'a'; 'b'; 'c'
|
||||||
|
-%% -bcafoo Multiple options: 'b'; 'c'; 'a' with argument "foo"
|
||||||
|
--spec parse_option_short([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
|
||||||
|
- {ok, {[option()], [string()]}}.
|
||||||
|
-parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, [Short | Arg]) ->
|
||||||
|
- case lists:keysearch(Short, ?OPT_SHORT, OptSpecList) of
|
||||||
|
- {value, {Name, Short, _Long, undefined, _Help}} ->
|
||||||
|
- parse_option_short(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg);
|
||||||
|
-
|
||||||
|
- {value, {_Name, Short, _Long, ArgSpec, _Help} = OptSpec} ->
|
||||||
|
- case Arg of
|
||||||
|
- [] ->
|
||||||
|
- % The option argument string is empty, but the option requires
|
||||||
|
- % an argument, so we look into the next string in the list.
|
||||||
|
- parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
|
||||||
|
-
|
||||||
|
- _ ->
|
||||||
|
- case is_valid_arg(ArgSpec, Arg) of
|
||||||
|
- true ->
|
||||||
|
- parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
- _ ->
|
||||||
|
- parse_option_short(OptSpecList, [convert_option_no_arg(OptSpec) | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg)
|
||||||
|
- end
|
||||||
|
- end;
|
||||||
|
-
|
||||||
|
- false ->
|
||||||
|
- throw({error, {invalid_option, OptStr}})
|
||||||
|
- end;
|
||||||
|
-parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, _OptStr, []) ->
|
||||||
|
- parse(OptSpecList, OptAcc, ArgAcc, ArgPos, Args).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Retrieve the argument for an option from the next string in the list of
|
||||||
|
-%% command-line parameters.
|
||||||
|
-parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail] = Args, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec) ->
|
||||||
|
- % Special case for booleans: when the next string is an option we assume
|
||||||
|
- % the value is 'true'.
|
||||||
|
- case (arg_spec_type(ArgSpec) =:= boolean) andalso not is_boolean_arg(Arg) of
|
||||||
|
- true ->
|
||||||
|
- parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
- _ ->
|
||||||
|
- parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Tail)
|
||||||
|
- end;
|
||||||
|
-parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [] = Args, {Name, _Short, _Long, ArgSpec, _Help}) ->
|
||||||
|
- % Special case for booleans: when the next string is missing we assume the
|
||||||
|
- % value is 'true'.
|
||||||
|
- case arg_spec_type(ArgSpec) of
|
||||||
|
- boolean ->
|
||||||
|
- parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
- _ ->
|
||||||
|
- throw({error, {missing_option_arg, Name}})
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Find the option for the discrete argument in position specified in the
|
||||||
|
-%% Pos argument.
|
||||||
|
--spec find_non_option_arg([option_spec()], integer()) -> {value, option_spec()} | false.
|
||||||
|
-find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} = OptSpec | _Tail], 0) ->
|
||||||
|
- {value, OptSpec};
|
||||||
|
-find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} | Tail], Pos) ->
|
||||||
|
- find_non_option_arg(Tail, Pos - 1);
|
||||||
|
-find_non_option_arg([_Head | Tail], Pos) ->
|
||||||
|
- find_non_option_arg(Tail, Pos);
|
||||||
|
-find_non_option_arg([], _Pos) ->
|
||||||
|
- false.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Append options that were not present in the command line arguments with
|
||||||
|
-%% their default arguments.
|
||||||
|
--spec append_default_options([option_spec()], [option()]) -> [option()].
|
||||||
|
-append_default_options([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail], OptAcc) ->
|
||||||
|
- append_default_options(Tail,
|
||||||
|
- case lists:keymember(Name, 1, OptAcc) of
|
||||||
|
- false ->
|
||||||
|
- [{Name, DefaultArg} | OptAcc];
|
||||||
|
- _ ->
|
||||||
|
- OptAcc
|
||||||
|
- end);
|
||||||
|
-%% For options with no default argument.
|
||||||
|
-append_default_options([_Head | Tail], OptAcc) ->
|
||||||
|
- append_default_options(Tail, OptAcc);
|
||||||
|
-append_default_options([], OptAcc) ->
|
||||||
|
- OptAcc.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec convert_option_no_arg(option_spec()) -> compound_option().
|
||||||
|
-convert_option_no_arg({Name, _Short, _Long, ArgSpec, _Help}) ->
|
||||||
|
- case ArgSpec of
|
||||||
|
- % Special case for booleans: if there is no argument we assume
|
||||||
|
- % the value is 'true'.
|
||||||
|
- {boolean, _DefaultValue} ->
|
||||||
|
- {Name, true};
|
||||||
|
- boolean ->
|
||||||
|
- {Name, true};
|
||||||
|
- _ ->
|
||||||
|
- throw({error, {missing_option_arg, Name}})
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Convert the argument passed in the command line to the data type
|
||||||
|
-%% indicated by the argument specification.
|
||||||
|
--spec convert_option_arg(option_spec(), string()) -> compound_option().
|
||||||
|
-convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) ->
|
||||||
|
- try
|
||||||
|
- {Name, to_type(arg_spec_type(ArgSpec), Arg)}
|
||||||
|
- catch
|
||||||
|
- error:_ ->
|
||||||
|
- throw({error, {invalid_option_arg, {Name, Arg}}})
|
||||||
|
- end.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Retrieve the data type form an argument specification.
|
||||||
|
--spec arg_spec_type(arg_spec()) -> arg_type() | undefined.
|
||||||
|
-arg_spec_type({Type, _DefaultArg}) ->
|
||||||
|
- Type;
|
||||||
|
-arg_spec_type(Type) when is_atom(Type) ->
|
||||||
|
- Type.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Convert an argument string to its corresponding data type.
|
||||||
|
--spec to_type(arg_type(), string()) -> arg_value().
|
||||||
|
-to_type(binary, Arg) ->
|
||||||
|
- list_to_binary(Arg);
|
||||||
|
-to_type(atom, Arg) ->
|
||||||
|
- list_to_atom(Arg);
|
||||||
|
-to_type(integer, Arg) ->
|
||||||
|
- list_to_integer(Arg);
|
||||||
|
-to_type(float, Arg) ->
|
||||||
|
- list_to_float(Arg);
|
||||||
|
-to_type(boolean, Arg) ->
|
||||||
|
- LowerArg = string:to_lower(Arg),
|
||||||
|
- case is_arg_true(LowerArg) of
|
||||||
|
- true ->
|
||||||
|
- true;
|
||||||
|
- _ ->
|
||||||
|
- case is_arg_false(LowerArg) of
|
||||||
|
- true ->
|
||||||
|
- false;
|
||||||
|
- false ->
|
||||||
|
- erlang:error(badarg)
|
||||||
|
- end
|
||||||
|
- end;
|
||||||
|
-to_type(_Type, Arg) ->
|
||||||
|
- Arg.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_arg_true(string()) -> boolean().
|
||||||
|
-is_arg_true(Arg) ->
|
||||||
|
- (Arg =:= "true") orelse (Arg =:= "t") orelse
|
||||||
|
- (Arg =:= "yes") orelse (Arg =:= "y") orelse
|
||||||
|
- (Arg =:= "on") orelse (Arg =:= "enabled") orelse
|
||||||
|
- (Arg =:= "1").
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_arg_false(string()) -> boolean().
|
||||||
|
-is_arg_false(Arg) ->
|
||||||
|
- (Arg =:= "false") orelse (Arg =:= "f") orelse
|
||||||
|
- (Arg =:= "no") orelse (Arg =:= "n") orelse
|
||||||
|
- (Arg =:= "off") orelse (Arg =:= "disabled") orelse
|
||||||
|
- (Arg =:= "0").
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_valid_arg(arg_spec(), nonempty_string()) -> boolean().
|
||||||
|
-is_valid_arg({Type, _DefaultArg}, Arg) ->
|
||||||
|
- is_valid_arg(Type, Arg);
|
||||||
|
-is_valid_arg(boolean, Arg) ->
|
||||||
|
- is_boolean_arg(Arg);
|
||||||
|
-is_valid_arg(integer, Arg) ->
|
||||||
|
- is_integer_arg(Arg);
|
||||||
|
-is_valid_arg(float, Arg) ->
|
||||||
|
- is_float_arg(Arg);
|
||||||
|
-is_valid_arg(_Type, _Arg) ->
|
||||||
|
- true.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_boolean_arg(string()) -> boolean().
|
||||||
|
-is_boolean_arg(Arg) ->
|
||||||
|
- LowerArg = string:to_lower(Arg),
|
||||||
|
- is_arg_true(LowerArg) orelse is_arg_false(LowerArg).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_integer_arg(string()) -> boolean().
|
||||||
|
-is_integer_arg([Head | Tail]) when Head >= $0, Head =< $9 ->
|
||||||
|
- is_integer_arg(Tail);
|
||||||
|
-is_integer_arg([_Head | _Tail]) ->
|
||||||
|
- false;
|
||||||
|
-is_integer_arg([]) ->
|
||||||
|
- true.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
--spec is_float_arg(string()) -> boolean().
|
||||||
|
-is_float_arg([Head | Tail]) when (Head >= $0 andalso Head =< $9) orelse Head =:= $. ->
|
||||||
|
- is_float_arg(Tail);
|
||||||
|
-is_float_arg([_Head | _Tail]) ->
|
||||||
|
- false;
|
||||||
|
-is_float_arg([]) ->
|
||||||
|
- true.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Show a message on stdout indicating the command line options and
|
||||||
|
-%% arguments that are supported by the program.
|
||||||
|
--spec usage([option_spec()], string()) -> ok.
|
||||||
|
-usage(OptSpecList, ProgramName) ->
|
||||||
|
- io:format("Usage: ~s~s~n~n~s~n",
|
||||||
|
- [ProgramName, usage_cmd_line(OptSpecList), usage_options(OptSpecList)]).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Show a message on stdout indicating the command line options and
|
||||||
|
-%% arguments that are supported by the program. The CmdLineTail argument
|
||||||
|
-%% is a string that is added to the end of the usage command line.
|
||||||
|
--spec usage([option_spec()], string(), string()) -> ok.
|
||||||
|
-usage(OptSpecList, ProgramName, CmdLineTail) ->
|
||||||
|
- io:format("Usage: ~s~s ~s~n~n~s~n",
|
||||||
|
- [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail, usage_options(OptSpecList)]).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Show a message on stdout indicating the command line options and
|
||||||
|
-%% arguments that are supported by the program. The CmdLineTail and OptionsTail
|
||||||
|
-%% arguments are a string that is added to the end of the usage command line
|
||||||
|
-%% and a list of tuples that are added to the end of the options' help lines.
|
||||||
|
--spec usage([option_spec()], string(), string(), [{string(), string()}]) -> ok.
|
||||||
|
-usage(OptSpecList, ProgramName, CmdLineTail, OptionsTail) ->
|
||||||
|
- UsageOptions = lists:foldl(
|
||||||
|
- fun ({Prefix, Help}, Acc) ->
|
||||||
|
- add_option_help(Prefix, Help, Acc)
|
||||||
|
- end, usage_options_reverse(OptSpecList, []), OptionsTail),
|
||||||
|
- io:format("Usage: ~s~s ~s~n~n~s~n",
|
||||||
|
- [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail,
|
||||||
|
- lists:flatten(lists:reverse(UsageOptions))]).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Return a string with the syntax for the command line options and
|
||||||
|
-%% arguments.
|
||||||
|
--spec usage_cmd_line([option_spec()]) -> string().
|
||||||
|
-usage_cmd_line(OptSpecList) ->
|
||||||
|
- usage_cmd_line(OptSpecList, []).
|
||||||
|
-
|
||||||
|
-usage_cmd_line([{Name, Short, Long, ArgSpec, _Help} | Tail], Acc) ->
|
||||||
|
- CmdLine =
|
||||||
|
- case ArgSpec of
|
||||||
|
- undefined ->
|
||||||
|
- if
|
||||||
|
- % For options with short form and no argument.
|
||||||
|
- Short =/= undefined ->
|
||||||
|
- [$\s, $[, $-, Short, $]];
|
||||||
|
- % For options with only long form and no argument.
|
||||||
|
- Long =/= undefined ->
|
||||||
|
- [$\s, $[, $-, $-, Long, $]];
|
||||||
|
- true ->
|
||||||
|
- []
|
||||||
|
- end;
|
||||||
|
- _ ->
|
||||||
|
- if
|
||||||
|
- % For options with short form and argument.
|
||||||
|
- Short =/= undefined ->
|
||||||
|
- [$\s, $[, $-, Short, $\s, $<, atom_to_list(Name), $>, $]];
|
||||||
|
- % For options with only long form and argument.
|
||||||
|
- Long =/= undefined ->
|
||||||
|
- [$\s, $[, $-, $-, Long, $\s, $<, atom_to_list(Name), $>, $]];
|
||||||
|
- % For options with neither short nor long form and argument.
|
||||||
|
- true ->
|
||||||
|
- [$\s, $<, atom_to_list(Name), $>]
|
||||||
|
- end
|
||||||
|
- end,
|
||||||
|
- usage_cmd_line(Tail, [CmdLine | Acc]);
|
||||||
|
-usage_cmd_line([], Acc) ->
|
||||||
|
- lists:flatten(lists:reverse(Acc)).
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Return a string with the help message for each of the options and
|
||||||
|
-%% arguments.
|
||||||
|
--spec usage_options([option_spec()]) -> string().
|
||||||
|
-usage_options(OptSpecList) ->
|
||||||
|
- lists:flatten(lists:reverse(usage_options_reverse(OptSpecList, []))).
|
||||||
|
-
|
||||||
|
-usage_options_reverse([{Name, Short, Long, _ArgSpec, Help} | Tail], Acc) ->
|
||||||
|
- Prefix =
|
||||||
|
- case Long of
|
||||||
|
- undefined ->
|
||||||
|
- case Short of
|
||||||
|
- % Neither short nor long form (non-option argument).
|
||||||
|
- undefined ->
|
||||||
|
- [$<, atom_to_list(Name), $>];
|
||||||
|
- % Only short form.
|
||||||
|
- _ ->
|
||||||
|
- [$-, Short]
|
||||||
|
- end;
|
||||||
|
- _ ->
|
||||||
|
- case Short of
|
||||||
|
- % Only long form.
|
||||||
|
- undefined ->
|
||||||
|
- [$-, $- | Long];
|
||||||
|
- % Both short and long form.
|
||||||
|
- _ ->
|
||||||
|
- [$-, Short, $,, $\s, $-, $- | Long]
|
||||||
|
- end
|
||||||
|
- end,
|
||||||
|
- usage_options_reverse(Tail, add_option_help(Prefix, Help, Acc));
|
||||||
|
-usage_options_reverse([], Acc) ->
|
||||||
|
- Acc.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Add the help message corresponding to an option specification to a list
|
||||||
|
-%% with the correct indentation.
|
||||||
|
--spec add_option_help(Prefix :: string(), Help :: string(), Acc :: string()) -> string().
|
||||||
|
-add_option_help(Prefix, Help, Acc) when is_list(Help), Help =/= [] ->
|
||||||
|
- FlatPrefix = lists:flatten(Prefix),
|
||||||
|
- case ((?INDENTATION * ?TAB_LENGTH) - 2 - length(FlatPrefix)) of
|
||||||
|
- TabSize when TabSize > 0 ->
|
||||||
|
- Tab = lists:duplicate(ceiling(TabSize / ?TAB_LENGTH), $\t),
|
||||||
|
- [[$\s, $\s, FlatPrefix, Tab, Help, $\n] | Acc];
|
||||||
|
- _ ->
|
||||||
|
- % The indentation for the option description is 3 tabs (i.e. 24 characters)
|
||||||
|
- % IMPORTANT: Change the number of tabs below if you change the
|
||||||
|
- % value of the INDENTATION macro.
|
||||||
|
- [[$\t, $\t, $\t, Help, $\n], [$\s, $\s, FlatPrefix, $\n] | Acc]
|
||||||
|
- end;
|
||||||
|
-add_option_help(_Opt, _Prefix, Acc) ->
|
||||||
|
- Acc.
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-%% @doc Return the smallest integral value not less than the argument.
|
||||||
|
--spec ceiling(float()) -> integer().
|
||||||
|
-ceiling(X) ->
|
||||||
|
- T = erlang:trunc(X),
|
||||||
|
- case (X - T) of
|
||||||
|
- % Neg when Neg < 0 ->
|
||||||
|
- % T;
|
||||||
|
- Pos when Pos > 0 ->
|
||||||
|
- T + 1;
|
||||||
|
- _ ->
|
||||||
|
- T
|
||||||
|
- end.
|
||||||
|
--
|
||||||
|
1.7.2.3
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/usr/bin/env escript
|
||||||
|
%%! -noshell -noinput
|
||||||
|
|
||||||
|
main (Args) ->
|
||||||
|
rebar:main(Args).
|
Loading…
Reference in New Issue