-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(ILS) :: {opt, binary(), gleam@option:option(ILS), gleam@option:option(binary()), fun((binary()) -> {ok, ILS} | {error, binary()}), gleam@option:option(binary())}. -file("/Users/drew/code/clip/src/clip/opt.gleam", 23). -spec to_arg_info(opt(any())) -> clip@internal@arg_info:arg_info(). to_arg_info(Opt) -> case Opt of {opt, Name, Default, Help, _, Short} -> erlang:setelement( 2, clip@internal@arg_info:empty(), [{named_info, Name, Short, begin _pipe = Default, gleam@option:map(_pipe, fun gleam@string:inspect/1) end, Help}] ) end. -file("/Users/drew/code/clip/src/clip/opt.gleam", 54). -spec try_map(opt(ILV), fun((ILV) -> {ok, ILX} | {error, binary()})) -> opt(ILX). try_map(Opt, F) -> case Opt of {opt, Name, _, Help, Try_map, Short} -> {opt, Name, none, Help, fun(Arg) -> gleam@result:'try'(Try_map(Arg), fun(A) -> F(A) end) end, Short} end. -file("/Users/drew/code/clip/src/clip/opt.gleam", 73). -spec map(opt(IMB), fun((IMB) -> IMD)) -> opt(IMD). 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(IMF), IMF) -> opt(IMF). default(Opt, Default) -> case Opt of {opt, Name, _, Help, Try_map, Short} -> {opt, Name, {some, Default}, Help, Try_map, Short} end. -file("/Users/drew/code/clip/src/clip/opt.gleam", 86). -spec optional(opt(IMI)) -> opt({ok, IMI} | {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", 91). -spec help(opt(IMN), binary()) -> opt(IMN). help(Opt, Help) -> case Opt of {opt, Name, Default, _, Try_map, Short} -> {opt, Name, Default, {some, Help}, Try_map, Short} end. -file("/Users/drew/code/clip/src/clip/opt.gleam", 101). -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", 115). -spec short(opt(binary()), binary()) -> opt(binary()). short(Opt, Short_name) -> case Opt of {opt, Name, Default, Help, Try_map, _} -> {opt, Name, Default, Help, Try_map, {some, Short_name}} end. -file("/Users/drew/code/clip/src/clip/opt.gleam", 131). -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", 148). -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", 158). -spec run(opt(IMX), list(binary())) -> {ok, {IMX, 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.