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, help/2, subcommands_with_default/2, subcommands/1, run/2]).
-export_type([command/1]).
-opaque command(IQY) :: {command,
clip@arg_info:arg_info(),
gleam@option:option(clip@help:help()),
fun((list(binary())) -> {ok, {IQY, list(binary())}} | {error, binary()})}.
-file("/Users/drew/code/clip/src/clip.gleam", 29).
-spec return(IQZ) -> command(IQZ).
return(Val) ->
{command, clip@arg_info:empty(), none, fun(Args) -> {ok, {Val, Args}} end}.
-file("/Users/drew/code/clip/src/clip.gleam", 56).
-spec parameter(fun((IRB) -> IRC)) -> fun((IRB) -> IRC).
parameter(F) ->
F.
-file("/Users/drew/code/clip/src/clip.gleam", 62).
-spec apply(command(fun((IRD) -> IRE)), command(IRD)) -> command(IRE).
apply(Mf, Ma) ->
{command,
clip@arg_info:merge(erlang:element(2, Mf), erlang:element(2, Ma)),
gleam@option:'or'(erlang:element(3, Mf), erlang:element(3, Ma)),
fun(Args) ->
gleam@result:'try'(
(erlang:element(4, Mf))(Args),
fun(_use0) ->
{F, Args1} = _use0,
gleam@result:'try'(
(erlang:element(4, Ma))(Args1),
fun(_use0@1) ->
{A, Args2} = _use0@1,
{ok, {F(A), Args2}}
end
)
end
)
end}.
-file("/Users/drew/code/clip/src/clip.gleam", 85).
-spec command(fun((IRI) -> IRJ)) -> command(fun((IRI) -> IRJ)).
command(F) ->
return(F).
-file("/Users/drew/code/clip/src/clip.gleam", 90).
-spec fail(binary()) -> command(any()).
fail(Message) ->
{command, clip@arg_info:empty(), none, fun(_) -> {error, Message} end}.
-file("/Users/drew/code/clip/src/clip.gleam", 104).
-spec opt(command(fun((IRN) -> IRO)), clip@opt:opt(IRN)) -> command(IRO).
opt(Command, Opt) ->
apply(
Command,
{command,
clip@opt:to_arg_info(Opt),
none,
fun(_capture) -> clip@opt:run(Opt, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 125).
-spec arg(command(fun((IRS) -> IRT)), clip@arg:arg(IRS)) -> command(IRT).
arg(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info(Arg),
none,
fun(_capture) -> clip@arg:run(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 149).
-spec arg_many(command(fun((list(IRX)) -> IRZ)), clip@arg:arg(IRX)) -> command(IRZ).
arg_many(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many(Arg),
none,
fun(_capture) -> clip@arg:run_many(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 172).
-spec arg_many1(command(fun((list(ISD)) -> ISF)), clip@arg:arg(ISD)) -> command(ISF).
arg_many1(Command, Arg) ->
apply(
Command,
{command,
clip@arg:to_arg_info_many1(Arg),
none,
fun(_capture) -> clip@arg:run_many1(Arg, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 192).
-spec flag(command(fun((boolean()) -> ISJ)), clip@flag:flag()) -> command(ISJ).
flag(Command, Flag) ->
apply(
Command,
{command,
clip@flag:to_arg_info(Flag),
none,
fun(_capture) -> clip@flag:run(Flag, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 240).
-spec help(command(ITA), clip@help:help()) -> command(ITA).
help(Command, Help) ->
erlang:setelement(3, Command, {some, Help}).
-file("/Users/drew/code/clip/src/clip.gleam", 244).
-spec wrap_help(command(ITD), clip@help:help()) -> fun((list(binary())) -> {ok,
{ITD, list(binary())}} |
{error, binary()}).
wrap_help(Command, Help) ->
Help_info = erlang:setelement(
4,
clip@arg_info:empty(),
[{flag_info,
<<"help"/utf8>>,
{some, <<"h"/utf8>>},
{some, <<"Print this help"/utf8>>}}]
),
fun(Args) -> case Args of
[<<"-h"/utf8>> | _] ->
{error,
clip@help:run(
Help,
clip@arg_info:merge(
erlang:element(2, Command),
Help_info
)
)};
[<<"--help"/utf8>> | _] ->
{error,
clip@help:run(
Help,
clip@arg_info:merge(
erlang:element(2, Command),
Help_info
)
)};
Other ->
(erlang:element(4, Command))(Other)
end end.
-file("/Users/drew/code/clip/src/clip.gleam", 262).
-spec run_aux(command(ITG), list(binary())) -> {ok, {ITG, list(binary())}} |
{error, binary()}.
run_aux(Command, Args) ->
F = case erlang:element(3, Command) of
none ->
erlang:element(4, Command);
{some, Help} ->
wrap_help(Command, Help)
end,
F(Args).
-file("/Users/drew/code/clip/src/clip.gleam", 199).
-spec run_subcommands(
list({binary(), command(ISM)}),
command(ISM),
list(binary())
) -> {ok, {ISM, list(binary())}} | {error, binary()}.
run_subcommands(Subcommands, Default, Args) ->
case {Subcommands, Args} of
{[{Name, Command} | _], [Head | Rest]} when Name =:= Head ->
run_aux(Command, Rest);
{[_ | Rest@1], _} ->
run_subcommands(Rest@1, Default, Args);
{[], _} ->
run_aux(Default, Args)
end.
-file("/Users/drew/code/clip/src/clip.gleam", 215).
-spec subcommands_with_default(list({binary(), command(ISR)}), command(ISR)) -> command(ISR).
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,
none,
fun(_capture) -> run_subcommands(Subcommands, Default, _capture) end}
).
-file("/Users/drew/code/clip/src/clip.gleam", 233).
-spec subcommands(list({binary(), command(ISW)})) -> command(ISW).
subcommands(Subcommands) ->
subcommands_with_default(
Subcommands,
fail(<<"No subcommand provided"/utf8>>)
).
-file("/Users/drew/code/clip/src/clip.gleam", 273).
-spec run(command(ITK), list(binary())) -> {ok, ITK} | {error, binary()}.
run(Command, Args) ->
case run_aux(Command, Args) of
{ok, {A, _}} ->
{ok, A};
{error, E} ->
{error, E}
end.