-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcalc
executable file
·49 lines (42 loc) · 1.28 KB
/
calc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env escript
%% calculator, demonstrating sub-commands, and handler specifications
%% how to run from:
%% ERL_FLAGS="-pa ../../../_build/default/lib/argparse/ebin" ./calc mul 2 2
-behaviour(cli).
-mode(compile).
-export([cli/0, sum/1, cos/1, mul/1]).
main(Args) ->
Out = cli:run(Args, #{progname => "calc"}),
io:format("~p~n", [Out]).
cli() ->
#{
commands => #{
"sum" => #{
arguments => [
#{name => num, nargs => nonempty_list, type => int, help => "Numbers to sum"}
]
},
"math" => #{
commands => #{
"sin" => #{handler => {math, sin, undefined}},
"cos" => #{},
"tan" => #{handler => {math, tan, undefined}}
},
arguments => [
#{name => in, type => float, help => "Input value"}
]
},
"mul" => #{
arguments => [
#{name => left, type => int},
#{name => right, type => int}
]
}
}
}.
sum(#{num := Nums}) ->
lists:sum(Nums).
cos(#{in := In}) ->
math:cos(In).
mul(#{left := Left, right := Right}) ->
Left * Right.