Current section

Files

Jump to
outil src outil.erl
Raw

src/outil.erl

-module(outil).
-compile(no_auto_import).
-export([command/3, implement/2, execute/2, usage/1]).
-spec command(
binary(),
binary(),
fun((outil@core:command(FCM)) -> outil@core:command(FCM))
) -> outil@core:command(FCM).
command(Name, Description, Continue) ->
Continue(
{command,
Name,
Description,
[],
[],
fun(_) -> {error, {not_implemented, Name}} end}
).
-spec implement(
outil@core:command(FCQ),
fun((list(binary())) -> {ok, FCQ} | {error, outil@core:error()})
) -> outil@core:command(FCQ).
implement(Cmd, Run) ->
{command,
erlang:element(2, Cmd),
erlang:element(3, Cmd),
erlang:element(4, Cmd),
erlang:element(5, Cmd),
Run}.
-spec execute(outil@core:command(FCU), list(binary())) -> {ok, FCU} |
{error, outil@core:error()}.
execute(Cmd, Args) ->
(erlang:element(6, Cmd))(Args).
-spec usage(outil@core:command(any())) -> binary().
usage(Cmd) ->
Args = begin
_pipe = erlang:element(4, Cmd),
_pipe@1 = gleam@list:map(_pipe, fun(Arg) -> erlang:element(2, Arg) end),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Arg@1) -> <<<<"<"/utf8, Arg@1/binary>>/binary, ">"/utf8>> end
),
gleam@string:join(_pipe@2, <<" "/utf8>>)
end,
Opts = begin
_pipe@3 = erlang:element(5, Cmd),
_pipe@4 = gleam@list:map(
_pipe@3,
fun(Opt) ->
{opt, Long, Short, Desc, Value} = Opt,
Short@1 = gleam@option:map(
Short,
fun(S) -> <<<<"-"/utf8, S/binary>>/binary, ", "/utf8>> end
),
Short@2 = gleam@option:unwrap(Short@1, <<""/utf8>>),
Long@1 = <<"--"/utf8, Long/binary>>,
Typ = opt_type(Value),
Default = show_default(Value),
Meta = <<<<<<<<"("/utf8, Typ/binary>>/binary,
", default: "/utf8>>/binary,
Default/binary>>/binary,
")"/utf8>>,
<<<<<<<<<<Short@2/binary, Long@1/binary>>/binary, " "/utf8>>/binary,
Desc/binary>>/binary,
" "/utf8>>/binary,
Meta/binary>>
end
),
_pipe@5 = gleam@list:map(
_pipe@4,
fun(Opt@1) -> <<" "/utf8, Opt@1/binary>> end
),
gleam@string:join(_pipe@5, <<"\n"/utf8>>)
end,
Opts@1 = <<<<"Options:\n"/utf8, Opts/binary>>/binary, "\n"/utf8>>,
<<<<<<<<<<<<<<(erlang:element(3, Cmd))/binary, "\n\n"/utf8>>/binary,
"Usage: "/utf8>>/binary,
(erlang:element(2, Cmd))/binary>>/binary,
" "/utf8>>/binary,
Args/binary>>/binary,
"\n\n"/utf8>>/binary,
Opts@1/binary>>.
-spec opt_type(outil@core:opt_value()) -> binary().
opt_type(Value) ->
case Value of
bool_opt ->
<<"bool"/utf8>>;
{float_opt, _} ->
<<"float"/utf8>>;
{int_opt, _} ->
<<"int"/utf8>>;
{string_opt, _} ->
<<"string"/utf8>>
end.
-spec show_default(outil@core:opt_value()) -> binary().
show_default(Value) ->
case Value of
bool_opt ->
<<"false"/utf8>>;
{float_opt, Default} ->
gleam@float:to_string(Default);
{int_opt, Default@1} ->
gleam@int:to_string(Default@1);
{string_opt, Default@2} ->
<<<<"\""/utf8, Default@2/binary>>/binary, "\""/utf8>>
end.