Current section

Files

Jump to
clip src clip.erl
Raw

src/clip.erl

-module(clip).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([pure/1, apply/2, command/1, fail/1, opt/2, arg/2, arg_many/2, arg_many1/2, flag/2, subcommands_with_default/2, subcommands/1, add_help/3, run/2]).
-export_type([command/1]).
-opaque command(IRJ) :: {command,
clip@internal@arg_info:arg_info(),
fun((list(binary())) -> {ok, {IRJ, list(binary())}} | {error, binary()})}.
-spec pure(IRK) -> command(IRK).
pure(Val) ->
{command,
clip@internal@arg_info:empty(),
fun(Args) -> {ok, {Val, Args}} end}.
-spec apply(command(fun((IRM) -> IRN)), command(IRM)) -> command(IRN).
apply(Mf, Ma) ->
{command,
clip@internal@arg_info:merge(
erlang:element(2, Mf),
erlang:element(2, Ma)
),
fun(Args) ->
gleam@result:'try'(
(erlang:element(3, Mf))(Args),
fun(_use0) ->
{F, Args1} = _use0,
gleam@result:'try'(
(erlang:element(3, Ma))(Args1),
fun(_use0@1) ->
{A, Args2} = _use0@1,
{ok, {F(A), Args2}}
end
)
end
)
end}.
-spec command(fun((IRR) -> IRS)) -> command(fun((IRR) -> IRS)).
command(F) ->
pure(F).
-spec fail(binary()) -> command(any()).
fail(Message) ->
{command, clip@internal@arg_info:empty(), fun(_) -> {error, Message} end}.
-spec opt(command(fun((IRW) -> IRX)), clip@opt:opt(IRW)) -> command(IRX).
opt(Command, Opt) ->
apply(
Command,
{command,
clip@opt:to_arg_info(Opt),
fun(_capture) -> clip@opt:run(Opt, _capture) end}
).
-spec arg(command(fun((ISB) -> ISC)), clip@arg:arg(ISB)) -> command(ISC).
arg(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info(Arg),
fun(_capture) -> clip@arg:run(Arg, _capture) end}
).
-spec arg_many(command(fun((list(ISG)) -> ISI)), clip@arg:arg(ISG)) -> command(ISI).
arg_many(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many(Arg),
fun(_capture) -> clip@arg:run_many(Arg, _capture) end}
).
-spec arg_many1(command(fun((list(ISM)) -> ISO)), clip@arg:arg(ISM)) -> command(ISO).
arg_many1(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many1(Arg),
fun(_capture) -> clip@arg:run_many1(Arg, _capture) end}
).
-spec flag(command(fun((boolean()) -> ISS)), clip@flag:flag()) -> command(ISS).
flag(Command, Flag) ->
apply(
Command,
{command,
clip@flag:to_arg_info(Flag),
fun(_capture) -> clip@flag:run(Flag, _capture) end}
).
-spec run_subcommands(
list({binary(), command(ISV)}),
command(ISV),
list(binary())
) -> {ok, {ISV, list(binary())}} | {error, binary()}.
run_subcommands(Subcommands, Default, Args) ->
case {Subcommands, Args} of
{[{Name, Command} | _], [Head | Rest]} when Name =:= Head ->
(erlang:element(3, Command))(Rest);
{[_ | Rest@1], _} ->
run_subcommands(Rest@1, Default, Args);
{[], _} ->
(erlang:element(3, Default))(Args)
end.
-spec subcommands_with_default(list({binary(), command(ITA)}), command(ITA)) -> command(ITA).
subcommands_with_default(Subcommands, Default) ->
Sub_names = gleam@list:map(Subcommands, fun(P) -> erlang:element(1, P) end),
Sub_arg_info = erlang:setelement(5, erlang:element(2, Default), Sub_names),
apply(
pure(fun(A) -> A end),
{command,
Sub_arg_info,
fun(_capture) -> run_subcommands(Subcommands, Default, _capture) end}
).
-spec subcommands(list({binary(), command(ITF)})) -> command(ITF).
subcommands(Subcommands) ->
subcommands_with_default(
Subcommands,
fail(<<"No subcommand provided"/utf8>>)
).
-spec add_help(command(ITJ), binary(), binary()) -> command(ITJ).
add_help(Command, Name, Description) ->
Help_info = erlang:setelement(
4,
clip@internal@arg_info:empty(),
[{flag_info,
<<"help"/utf8>>,
{some, <<"h"/utf8>>},
{some, <<"Print this help"/utf8>>}}]
),
erlang:setelement(3, Command, fun(Args) -> case Args of
[<<"-h"/utf8>> | _] ->
{error,
clip@internal@arg_info:help_text(
clip@internal@arg_info:merge(
erlang:element(2, Command),
Help_info
),
Name,
Description
)};
[<<"--help"/utf8>> | _] ->
{error,
clip@internal@arg_info:help_text(
clip@internal@arg_info:merge(
erlang:element(2, Command),
Help_info
),
Name,
Description
)};
Other ->
(erlang:element(3, Command))(Other)
end end).
-spec run(command(ITM), list(binary())) -> {ok, ITM} | {error, binary()}.
run(Command, Args) ->
case (erlang:element(3, Command))(Args) of
{ok, {A, _}} ->
{ok, A};
{error, E} ->
{error, E}
end.