View Source calculator

#!/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.