Current section

Files

Jump to
outil src outil@opt.erl
Raw

src/outil@opt.erl

-module(outil@opt).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([int_/6, int/5, float_/6, float/5, named_opt_parser/4, string/5, string_/6, with_named_option/5, bool_/5, bool/4]).
-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 int_(
outil:command(),
binary(),
gleam@option:option(binary()),
binary(),
integer(),
fun((fun((outil:command(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
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 int(
outil:command(),
binary(),
binary(),
integer(),
fun((fun((outil:command(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
int(Cmd, Long, Description, Default, Continue) ->
int_(Cmd, Long, none, Description, Default, Continue).
-spec float_(
outil:command(),
binary(),
gleam@option:option(binary()),
binary(),
float(),
fun((fun((outil:command(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
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 float(
outil:command(),
binary(),
binary(),
float(),
fun((fun((outil:command(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
float(Cmd, Long, Description, Default, Continue) ->
float_(Cmd, Long, none, Description, Default, Continue).
-spec named_opt_parser(
binary(),
gleam@option:option(binary()),
fun((binary()) -> {ok, FNV} | {error, nil}),
FNV
) -> fun((list(binary())) -> {ok, FNV} | {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 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.
-spec string(
outil:command(),
binary(),
binary(),
binary(),
fun((fun((outil:command(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
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(), fun((any()) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
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(),
FNP,
fun((binary()) -> {ok, FNP} | {error, nil}),
fun((fun((outil:command(), fun((FNP) -> {ok, FQI} |
{error, outil:command_return(FQF)})) -> {ok, FQI} |
{error, outil:command_return(FQF)}), outil:command()) -> FQL)
) -> FQL.
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, And_then) ->
_pipe = outil@help:wrap_usage(Run_cmd, Opt_parser),
gleam@result:then(_pipe, And_then)
end,
Continue(Opt_parser@1, add_option(Cmd, Opt)).
-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 bool_(
outil:command(),
binary(),
gleam@option:option(binary()),
binary(),
fun((fun((outil:command(), fun((boolean()) -> {ok, FRG} |
{error, outil:command_return(FRD)})) -> {ok, FRG} |
{error, outil:command_return(FRD)}), outil:command()) -> FRJ)
) -> FRJ.
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, And_then) ->
_pipe = outil@help:wrap_usage(Run_cmd, Opt_parser),
gleam@result:then(_pipe, And_then)
end,
Continue(Opt_parser@1, add_option(Cmd, Opt)).
-spec bool(
outil:command(),
binary(),
binary(),
fun((fun((outil:command(), fun((boolean()) -> {ok, FRK} |
{error, outil:command_return(FRL)})) -> {ok, FRK} |
{error, outil:command_return(FRL)}), outil:command()) -> FRM)
) -> FRM.
bool(Cmd, Long, Description, Continue) ->
bool_(Cmd, Long, none, Description, Continue).