Current section

Files

Jump to
outil src outil.erl
Raw

src/outil.erl

-module(outil).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([command/4, print_usage/1, parse_bool/1, print_usage_and_exit/1]).
-export_type([command/0, argument/0, opt/0, opt_value/0, command_return/1]).
-type command() :: {command,
binary(),
binary(),
list(argument()),
list(opt()),
list(binary())}.
-type argument() :: {bool_argument, binary()} |
{float_argument, binary()} |
{int_argument, binary()} |
{string_argument, binary()}.
-type opt() :: {opt,
binary(),
gleam@option:option(binary()),
binary(),
opt_value()}.
-type opt_value() :: bool_opt |
{float_opt, float()} |
{int_opt, integer()} |
{string_opt, binary()}.
-type command_return(FGL) :: {command_line_error,
outil@error:reason(),
binary()} |
{command_error, FGL} |
{help, binary()}.
-spec command(binary(), binary(), list(binary()), fun((command()) -> FGS)) -> FGS.
command(Name, Description, Argv, Continue) ->
Continue({command, Name, Description, [], [], Argv}).
-spec print_usage(command_return(any())) -> nil.
print_usage(Return) ->
case Return of
{command_line_error, _, Usage} ->
gleam@io:println(Usage);
{help, Usage@1} ->
gleam@io:println(Usage@1);
_ ->
nil
end.
-spec parse_bool(binary()) -> {ok, boolean()} | {error, nil}.
parse_bool(Arg) ->
case Arg of
<<"true"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, false};
_ ->
{error, nil}
end.
-spec print_usage_and_exit(command_return(any())) -> nil.
print_usage_and_exit(Return) ->
print_usage(Return),
case Return of
{command_line_error, _, _} ->
erlang:halt(2);
{command_error, _} ->
erlang:halt(1);
{help, _} ->
erlang:halt(0)
end.