Skip to content

Commit e9b150a

Browse files
author
Mark Allen
committed
Merge pull request #40 from basho/feature/RIAK-1425-repl-cli
Allow registration of functions as usage
2 parents 8c7e01c + 9d2858d commit e9b150a

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ these points is hit, via longest match with the command string, the registered
275275
usage string will be shown. Note that "Usage: " will be prepended to the string,
276276
so don't add that part in.
277277

278+
If you'd like to generate usage output dynamically, pass a 0-arity
279+
function that returns an `iolist()` and it will be called when output
280+
is generated.
281+
278282
```erlang
279283
handoff_usage() ->
280284
["riak-admin handoff <sub-command>\n\n",
@@ -289,16 +293,20 @@ handoff_limit_usage() ->
289293
"Options\n\n",
290294
" -n <Node>, --node <Node>\n",
291295
" Show the handoff limit for the given node only\n\n",
296+
io_lib:format(" This node is: ~p~n", [node()]),
292297
" -f, --force-rpc\n",
293298
" Retrieve the latest value from a given node or nodes via rpc\n",
294-
" instead of using cluster metadata which may not have propogated\n",
299+
" instead of using cluster metadata which may not have propagated\n",
295300
" to the local node yet. WARNING: The use of this flag is not\n",
296301
" recommended as it spams the cluster with messages instead of\n",
297302
" just talking to the local node.\n\n"
298303
].
299304

305+
%% Use a static iolist():
300306
clique:register_usage(["riak-admin", "handoff"], handoff_usage()),
301-
clique:register_usage(["riak-admin", "handoff", "limit"], handoff_limit_usage()).
307+
308+
%% Register a callback for dynamic output:
309+
clique:register_usage(["riak-admin", "handoff", "limit"], fun handoff_limit_usage/0).
302310
```
303311

304312
### run/1

src/clique_usage.erl

+27-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
%% -------------------------------------------------------------------
2020
-module(clique_usage).
2121

22+
-ifdef(TEST).
23+
-compile(export_all).
24+
-include_lib("eunit/include/eunit.hrl").
25+
-endif.
26+
2227
-define(usage_table, clique_usage).
28+
-define(usage_prefix, "Usage: ").
2329

2430
-type err() :: {error, term()}.
31+
-type usage_function() :: fun(() -> iolist()).
2532

2633
%% API
2734
-export([init/0,
@@ -35,9 +42,8 @@ init() ->
3542

3643
%% @doc Register usage for a given command sequence. Lookups are by longest
3744
%% match.
38-
-spec register([string()], iolist()) -> true.
39-
register(Cmd, Usage0) ->
40-
Usage = ["Usage: ", Usage0],
45+
-spec register([string()], iolist() | usage_function()) -> true.
46+
register(Cmd, Usage) ->
4147
ets:insert(?usage_table, {Cmd, Usage}).
4248

4349
-spec print(iolist()) -> ok.
@@ -55,10 +61,27 @@ find([]) ->
5561
{error, "Error: Usage information not found for the given command\n\n"};
5662
find(Cmd) ->
5763
case ets:lookup(?usage_table, Cmd) of
64+
[{Cmd, Fun}] when is_function(Fun) ->
65+
[?usage_prefix, Fun()];
5866
[{Cmd, Usage}] ->
59-
Usage;
67+
[?usage_prefix, Usage];
6068
[] ->
6169
Cmd2 = lists:reverse(tl(lists:reverse(Cmd))),
6270
find(Cmd2)
6371
end.
6472

73+
-ifdef(TEST).
74+
find_different_types_test() ->
75+
{setup,
76+
fun init/0,
77+
fun(_) -> ets:delete(?usage_table) end,
78+
?_test(begin
79+
String = "clique foo [-f]\n",
80+
Fun = fun() -> String end,
81+
?MODULE:register(["fun", "usage"], Fun),
82+
?MODULE:register(["string", "usage"], String),
83+
?assertEqual([?usage_prefix, String], find(["fun", "usage"])),
84+
?assertEqual([?usage_prefix, String], find(["string", "usage"])),
85+
?assertMatch({error, _}, find(["foo"]))
86+
end)}.
87+
-endif.

0 commit comments

Comments
 (0)