-module(outil@opt). -compile(no_auto_import). -export([bool/4, bool_/5, float/5, float_/6, int/5, int_/6, string/5, string_/6, with_named_option/5, named_opt_parser/4]). -spec bool( outil:command(), binary(), binary(), fun((fun((outil:command()) -> {ok, boolean()} | {error, outil:command_return(any())}), outil:command()) -> EYQ) ) -> EYQ. bool(Cmd, Long, Description, Continue) -> bool_(Cmd, Long, none, Description, Continue). -spec bool_( outil:command(), binary(), gleam@option:option(binary()), binary(), fun((fun((outil:command()) -> {ok, boolean()} | {error, outil:command_return(any())}), outil:command()) -> EYW) ) -> EYW. bool_(Cmd, Long, Short, Description, Continue) -> Opt = {opt, Long, Short, Description, bool_opt}, Opt_parser = bool_opt_parser(Long, Short), Opt_parser@1 = fun(Run_cmd) -> outil@help:wrap_usage(Run_cmd, Opt_parser) end, Continue(Opt_parser@1, add_option(Cmd, Opt)). -spec float( outil:command(), binary(), binary(), float(), fun((fun((outil:command()) -> {ok, float()} | {error, outil:command_return(any())}), outil:command()) -> EZB) ) -> EZB. float(Cmd, Long, Description, Default, Continue) -> float_(Cmd, Long, none, Description, Default, Continue). -spec float_( outil:command(), binary(), gleam@option:option(binary()), binary(), float(), fun((fun((outil:command()) -> {ok, float()} | {error, outil:command_return(any())}), outil:command()) -> EZH) ) -> EZH. float_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {float_opt, Default}}, with_named_option(Cmd, Opt, Default, fun gleam@float:parse/1, Continue). -spec int( outil:command(), binary(), binary(), integer(), fun((fun((outil:command()) -> {ok, integer()} | {error, outil:command_return(any())}), outil:command()) -> EZM) ) -> EZM. int(Cmd, Long, Description, Default, Continue) -> int_(Cmd, Long, none, Description, Default, Continue). -spec int_( outil:command(), binary(), gleam@option:option(binary()), binary(), integer(), fun((fun((outil:command()) -> {ok, integer()} | {error, outil:command_return(any())}), outil:command()) -> EZS) ) -> EZS. int_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {int_opt, Default}}, with_named_option(Cmd, Opt, Default, fun gleam@int:parse/1, Continue). -spec string( outil:command(), binary(), binary(), binary(), fun((fun((outil:command()) -> {ok, binary()} | {error, outil:command_return(any())}), outil:command()) -> EZX) ) -> EZX. string(Cmd, Long, Description, Default, Continue) -> string_(Cmd, Long, none, Description, Default, Continue). -spec string_( outil:command(), binary(), gleam@option:option(binary()), binary(), binary(), fun((fun((outil:command()) -> {ok, binary()} | {error, outil:command_return(any())}), outil:command()) -> FAD) ) -> FAD. string_(Cmd, Long, Short, Description, Default, Continue) -> Opt = {opt, Long, Short, Description, {string_opt, Default}}, with_named_option( Cmd, Opt, Default, fun(Field@0) -> {ok, Field@0} end, Continue ). -spec with_named_option( outil:command(), outil:opt(), FAI, fun((binary()) -> {ok, FAI} | {error, nil}), fun((fun((outil:command()) -> {ok, FAI} | {error, outil:command_return(any())}), outil:command()) -> FAL) ) -> FAL. with_named_option(Cmd, Opt, Default, Parse, Continue) -> Opt_parser = named_opt_parser( erlang:element(2, Opt), erlang:element(3, Opt), Parse, Default ), Opt_parser@1 = fun(Run_cmd) -> outil@help:wrap_usage(Run_cmd, Opt_parser) end, Continue(Opt_parser@1, add_option(Cmd, Opt)). -spec add_option(outil:command(), outil:opt()) -> outil:command(). add_option(Cmd, Opt) -> {command, erlang:element(2, Cmd), erlang:element(3, Cmd), erlang:element(4, Cmd), gleam@list:append(erlang:element(5, Cmd), [Opt]), erlang:element(6, Cmd)}. -spec named_opt_parser( binary(), gleam@option:option(binary()), fun((binary()) -> {ok, FAR} | {error, nil}), FAR ) -> fun((list(binary())) -> {ok, FAR} | {error, outil@error:reason()}). named_opt_parser(Long, Short, Parser, Default) -> fun(Args) -> case find(Long, Short, Args) of {none, _} -> {ok, Default}; {{some, _}, {some, Arg}} -> _pipe = Parser(Arg), gleam@result:map_error( _pipe, fun(_) -> {malformed_argument, Long, Arg} end ); {{some, _}, none} -> {error, {missing_argument, Long}} end end. -spec bool_opt_parser(binary(), gleam@option:option(binary())) -> fun((list(binary())) -> {ok, boolean()} | {error, outil@error:reason()}). bool_opt_parser(Long, Short) -> fun(Args) -> {Opt, Arg} = find(Long, Short, Args), case Opt of none -> {ok, false}; {some, _} -> case Arg of none -> {ok, true}; {some, Arg@1} -> _pipe = outil:parse_bool(Arg@1), gleam@result:map_error( _pipe, fun(_) -> {malformed_argument, Long, Arg@1} end ) end end end. -spec find(binary(), gleam@option:option(binary()), list(binary())) -> {gleam@option:option(binary()), gleam@option:option(binary())}. find(Long, Short, Args) -> Long_opt = <<"--"/utf8, Long/binary>>, Short_opt = begin _pipe = Short, gleam@option:map(_pipe, fun(S) -> <<"-"/utf8, S/binary>> end) end, Opt = gleam@list:find( Args, fun(Arg) -> Is_long = gleam@string:starts_with(Arg, Long_opt), Is_short = begin _pipe@1 = Short_opt, _pipe@2 = gleam@option:map( _pipe@1, fun(S@1) -> gleam@string:starts_with(Arg, S@1) end ), gleam@option:unwrap(_pipe@2, false) end, Is_long orelse Is_short end ), case Opt of {error, _} -> {none, none}; {ok, Opt@1} -> Arg@1 = begin _pipe@3 = gleam@string:split(Opt@1, <<"="/utf8>>), gleam@list:at(_pipe@3, 1) end, {{some, Opt@1}, gleam@option:from_result(Arg@1)} end.