Current section
Files
Jump to
Current section
Files
src/clip@opt.erl
-module(clip@opt).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_arg_info/1, try_map/2, map/2, default/2, optional/1, help/2, new/1, short/2, int/1, float/1, run/2]).
-export_type([opt/1]).
-opaque opt(IMS) :: {opt,
binary(),
gleam@option:option(IMS),
gleam@option:option(binary()),
fun((binary()) -> {ok, IMS} | {error, binary()}),
gleam@option:option(binary())}.
-file("/Users/drew/code/clip/src/clip/opt.gleam", 23).
-spec to_arg_info(opt(any())) -> clip@arg_info:arg_info().
to_arg_info(Opt) ->
erlang:setelement(
2,
clip@arg_info:empty(),
[{named_info,
erlang:element(2, Opt),
erlang:element(6, Opt),
begin
_pipe = erlang:element(3, Opt),
gleam@option:map(_pipe, fun gleam@string:inspect/1)
end,
erlang:element(4, Opt)}]
).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 51).
-spec try_map(opt(IMV), fun((IMV) -> {ok, IMX} | {error, binary()})) -> opt(IMX).
try_map(Opt, F) ->
{opt,
erlang:element(2, Opt),
none,
erlang:element(4, Opt),
fun(Arg) ->
gleam@result:'try'(
(erlang:element(5, Opt))(Arg),
fun(A) -> F(A) end
)
end,
erlang:element(6, Opt)}.
-file("/Users/drew/code/clip/src/clip/opt.gleam", 73).
-spec map(opt(INB), fun((INB) -> IND)) -> opt(IND).
map(Opt, F) ->
try_map(Opt, fun(A) -> {ok, F(A)} end).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 78).
-spec default(opt(INF), INF) -> opt(INF).
default(Opt, Default) ->
erlang:setelement(3, Opt, {some, Default}).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 83).
-spec optional(opt(INI)) -> opt({ok, INI} | {error, nil}).
optional(Opt) ->
_pipe = Opt,
_pipe@1 = map(_pipe, fun(Field@0) -> {ok, Field@0} end),
default(_pipe@1, {error, nil}).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 88).
-spec help(opt(INN), binary()) -> opt(INN).
help(Opt, Help) ->
erlang:setelement(4, Opt, {some, Help}).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 95).
-spec new(binary()) -> opt(binary()).
new(Name) ->
{opt, Name, none, none, fun(Field@0) -> {ok, Field@0} end, none}.
-file("/Users/drew/code/clip/src/clip/opt.gleam", 109).
-spec short(opt(binary()), binary()) -> opt(binary()).
short(Opt, Short_name) ->
erlang:setelement(6, Opt, {some, Short_name}).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 122).
-spec int(opt(binary())) -> opt(integer()).
int(Opt) ->
_pipe = Opt,
try_map(_pipe, fun(Val) -> _pipe@1 = gleam@int:parse(Val),
gleam@result:map_error(
_pipe@1,
fun(_) ->
<<"Non-integer value provided for "/utf8,
(erlang:element(2, Opt))/binary>>
end
) end).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 139).
-spec float(opt(binary())) -> opt(float()).
float(Opt) ->
_pipe = Opt,
try_map(_pipe, fun(Val) -> _pipe@1 = gleam@float:parse(Val),
gleam@result:map_error(
_pipe@1,
fun(_) ->
<<"Non-float value provided for "/utf8,
(erlang:element(2, Opt))/binary>>
end
) end).
-file("/Users/drew/code/clip/src/clip/opt.gleam", 149).
-spec run(opt(INX), list(binary())) -> {ok, {INX, list(binary())}} |
{error, binary()}.
run(Opt, Args) ->
Long_name = <<"--"/utf8, (erlang:element(2, Opt))/binary>>,
Short_name = gleam@option:map(
erlang:element(6, Opt),
fun(S) -> <<"-"/utf8, S/binary>> end
),
Names = begin
_pipe = Short_name,
_pipe@1 = gleam@option:map(_pipe, fun(S@1) -> [S@1] end),
gleam@option:unwrap(_pipe@1, [])
end,
Names@1 = begin
_pipe@2 = [Long_name | Names],
gleam@string:join(_pipe@2, <<", "/utf8>>)
end,
case {Args, erlang:element(3, Opt)} of
{[Key, Val | Rest], _} when (Key =:= Long_name) orelse ({some, Key} =:= Short_name) ->
gleam@result:'try'(
(erlang:element(5, Opt))(Val),
fun(A) -> {ok, {A, Rest}} end
);
{[Head | Rest@1], _} ->
_pipe@3 = run(Opt, Rest@1),
gleam@result:map(
_pipe@3,
fun(V) ->
{erlang:element(1, V), [Head | erlang:element(2, V)]}
end
);
{[], {some, V@1}} ->
{ok, {V@1, []}};
{[], none} ->
{error, <<"missing required arg: "/utf8, Names@1/binary>>}
end.