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([return/1, parameter/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_custom_help/2, add_help/3, run/2]).
-export_type([command/1]).
-opaque command(IQW) :: {command,
clip@arg_info:arg_info(),
fun((list(binary())) -> {ok, {IQW, list(binary())}} | {error, binary()})}.
-file("/Users/drew/code/clip/src/clip.gleam", 28).
-spec return(IQX) -> command(IQX).
return(Val) ->
{command, clip@arg_info:empty(), fun(Args) -> {ok, {Val, Args}} end}.
-file("/Users/drew/code/clip/src/clip.gleam", 55).
-spec parameter(fun((IQZ) -> IRA)) -> fun((IQZ) -> IRA).
parameter(F) ->
F.
-file("/Users/drew/code/clip/src/clip.gleam", 61).
-spec apply(command(fun((IRB) -> IRC)), command(IRB)) -> command(IRC).
apply(Mf, Ma) ->
{command,
clip@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}.
-file("/Users/drew/code/clip/src/clip.gleam", 80).
-spec command(fun((IRG) -> IRH)) -> command(fun((IRG) -> IRH)).
command(F) ->
return(F).
-file("/Users/drew/code/clip/src/clip.gleam", 85).
-spec fail(binary()) -> command(any()).
fail(Message) ->
{command, clip@arg_info:empty(), fun(_) -> {error, Message} end}.
-file("/Users/drew/code/clip/src/clip.gleam", 99).
-spec opt(command(fun((IRL) -> IRM)), clip@opt:opt(IRL)) -> command(IRM).
opt(Command, Opt) ->
apply(
Command,
{command,
clip@opt:to_arg_info(Opt),
fun(_capture) -> clip@opt:run(Opt, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 117).
-spec arg(command(fun((IRQ) -> IRR)), clip@arg:arg(IRQ)) -> command(IRR).
arg(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info(Arg),
fun(_capture) -> clip@arg:run(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 138).
-spec arg_many(command(fun((list(IRV)) -> IRX)), clip@arg:arg(IRV)) -> command(IRX).
arg_many(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many(Arg),
fun(_capture) -> clip@arg:run_many(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 161).
-spec arg_many1(command(fun((list(ISB)) -> ISD)), clip@arg:arg(ISB)) -> command(ISD).
arg_many1(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many1(Arg),
fun(_capture) -> clip@arg:run_many1(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 178).
-spec flag(command(fun((boolean()) -> ISH)), clip@flag:flag()) -> command(ISH).
flag(Command, Flag) ->
apply(
Command,
{command,
clip@flag:to_arg_info(Flag),
fun(_capture) -> clip@flag:run(Flag, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 182).
-spec run_subcommands(
list({binary(), command(ISK)}),
command(ISK),
list(binary())
) -> {ok, {ISK, 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.
-file("/Users/drew/code/clip/src/clip.gleam", 197).
-spec subcommands_with_default(list({binary(), command(ISP)}), command(ISP)) -> command(ISP).
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(
return(fun(A) -> A end),
{command,
Sub_arg_info,
fun(_capture) -> run_subcommands(Subcommands, Default, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 211).
-spec subcommands(list({binary(), command(ISU)})) -> command(ISU).
subcommands(Subcommands) ->
subcommands_with_default(
Subcommands,
fail(<<"No subcommand provided"/utf8>>)
).
-file("/Users/drew/code/clip/src/clip.gleam", 220).
-spec add_custom_help(command(ISY), fun((clip@arg_info:arg_info()) -> binary())) -> command(ISY).
add_custom_help(Command, F) ->
Help_info = erlang:setelement(
4,
clip@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,
F(
clip@arg_info:merge(
erlang:element(2, Command),
Help_info
)
)};
[<<"--help"/utf8>> | _] ->
{error,
F(
clip@arg_info:merge(
erlang:element(2, Command),
Help_info
)
)};
Other ->
(erlang:element(3, Command))(Other)
end end).
-file("/Users/drew/code/clip/src/clip.gleam", 246).
-spec add_help(command(ITB), binary(), binary()) -> command(ITB).
add_help(Command, Name, Description) ->
add_custom_help(
Command,
fun(_capture) ->
clip@arg_info:help_text(_capture, Name, Description)
end
).
-file("/Users/drew/code/clip/src/clip.gleam", 256).
-spec run(command(ITE), list(binary())) -> {ok, ITE} | {error, binary()}.
run(Command, Args) ->
case (erlang:element(3, Command))(Args) of
{ok, {A, _}} ->
{ok, A};
{error, E} ->
{error, E}
end.