Packages
glint
0.7.0
1.3.0
1.2.1
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc5
1.0.0-rc4
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.18.1
0.18.0
0.17.1
0.17.0
0.16.0
0.16.0-rc1
0.15.0
0.15.0-rc2
0.15.0-rc1
0.14.0
0.13.0
0.12.0
0.12.0-rc6
0.12.0-rc5
0.12.0-rc4
0.12.0-rc3
0.12.0-rc2
0.12.0-rc1
0.11.4
0.11.3
0.11.2
0.11.0
0.10.0
0.9.0
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Gleam command-line argument parsing with flags and automated help text generation.
Current section
Files
Jump to
Current section
Files
src/glint@flag.erl
-module(glint@flag).
-compile(no_auto_import).
-export([int/3, ints/3, float/3, floats/3, string/3, strings/3, bool/3, build_map/1, update_flags/2, help_flag/0, flags_help/1]).
-export_type([value/0, contents/0]).
-type value() :: {b, boolean()} |
{i, integer()} |
{li, list(integer())} |
{f, float()} |
{lf, list(float())} |
{s, binary()} |
{ls, list(binary())}.
-type contents() :: {contents, value(), binary()}.
-spec int(binary(), integer(), binary()) -> {binary(), contents()}.
int(Name, Value, Description) ->
{Name, {contents, {i, Value}, Description}}.
-spec ints(binary(), list(integer()), binary()) -> {binary(), contents()}.
ints(Name, Value, Description) ->
{Name, {contents, {li, Value}, Description}}.
-spec float(binary(), float(), binary()) -> {binary(), contents()}.
float(Name, Value, Description) ->
{Name, {contents, {f, Value}, Description}}.
-spec floats(binary(), list(float()), binary()) -> {binary(), contents()}.
floats(Name, Value, Description) ->
{Name, {contents, {lf, Value}, Description}}.
-spec string(binary(), binary(), binary()) -> {binary(), contents()}.
string(Name, Value, Description) ->
{Name, {contents, {s, Value}, Description}}.
-spec strings(binary(), list(binary()), binary()) -> {binary(), contents()}.
strings(Name, Value, Description) ->
{Name, {contents, {ls, Value}, Description}}.
-spec bool(binary(), boolean(), binary()) -> {binary(), contents()}.
bool(Name, Value, Description) ->
{Name, {contents, {b, Value}, Description}}.
-spec build_map(list({binary(), contents()})) -> gleam@map:map_(binary(), contents()).
build_map(Flags) ->
gleam@map:from_list(Flags).
-spec update_flags(gleam@map:map_(binary(), contents()), binary()) -> {ok,
gleam@map:map_(binary(), contents())} |
{error, snag:snag()}.
update_flags(Flags, Flag_input) ->
Flag_input@1 = gleam@string:drop_left(
Flag_input,
gleam@string:length(<<"--"/utf8>>)
),
case gleam@string:split_once(Flag_input@1, <<"="/utf8>>) of
{error, _@1} ->
case access_flag(Flags, Flag_input@1) of
{error, _try} -> {error, _try};
{ok, {contents, Default, Desc}} ->
case Default of
{b, Val} ->
_pipe = Val,
_pipe@1 = gleam@bool:negate(_pipe),
_pipe@2 = {b, _pipe@1},
_pipe@3 = {contents, _pipe@2, Desc},
_pipe@4 = gleam@map:insert(
Flags,
Flag_input@1,
_pipe@3
),
{ok, _pipe@4};
_@2 ->
{error, no_value_flag_err(Flag_input@1)}
end
end;
{ok, {Key, Value}} ->
case access_flag(Flags, Key) of
{error, _try@1} -> {error, _try@1};
{ok, {contents, Default@1, Desc@1}} ->
_pipe@5 = Default@1,
_pipe@6 = compute_flag(Key, Value, _pipe@5),
_pipe@7 = gleam@result:map(
_pipe@6,
fun(_capture) -> {contents, _capture, Desc@1} end
),
gleam@result:map(
_pipe@7,
fun(_capture@1) ->
gleam@map:insert(Flags, Key, _capture@1)
end
)
end
end.
-spec access_flag(gleam@map:map_(binary(), contents()), binary()) -> {ok,
contents()} |
{error, snag:snag()}.
access_flag(Flags, Name) ->
_pipe = gleam@map:get(Flags, Name),
gleam@result:replace_error(_pipe, undefined_flag_err(Name)).
-spec compute_flag(binary(), binary(), value()) -> {ok, value()} |
{error, snag:snag()}.
compute_flag(Name, Input, Default) ->
case Default of
{i, _@1} ->
fun parse_int/2;
{li, _@2} ->
fun parse_int_list/2;
{f, _@3} ->
fun parse_float/2;
{lf, _@4} ->
fun parse_float_list/2;
{s, _@5} ->
fun parse_string/2;
{ls, _@6} ->
fun parse_string_list/2;
{b, _@7} ->
fun parse_bool/2
end(Name, Input).
-spec parse_int(binary(), binary()) -> {ok, value()} | {error, snag:snag()}.
parse_int(Key, Value) ->
_pipe = parse_flag(Value, fun gleam@int:parse/1, fun(A) -> {i, A} end),
gleam@result:replace_error(_pipe, cannot_parse(Key, Value, <<"int"/utf8>>)).
-spec parse_int_list(binary(), binary()) -> {ok, value()} | {error, snag:snag()}.
parse_int_list(Key, Value) ->
_pipe = parse_list_flag(Value, fun gleam@int:parse/1, fun(A) -> {li, A} end),
gleam@result:replace_error(
_pipe,
cannot_parse(Key, Value, <<"int list"/utf8>>)
).
-spec parse_float(binary(), binary()) -> {ok, value()} | {error, snag:snag()}.
parse_float(Key, Value) ->
_pipe = parse_flag(Value, fun gleam@float:parse/1, fun(A) -> {f, A} end),
gleam@result:replace_error(
_pipe,
cannot_parse(Key, Value, <<"float"/utf8>>)
).
-spec parse_float_list(binary(), binary()) -> {ok, value()} |
{error, snag:snag()}.
parse_float_list(Key, Value) ->
_pipe = parse_list_flag(
Value,
fun gleam@float:parse/1,
fun(A) -> {lf, A} end
),
gleam@result:replace_error(
_pipe,
cannot_parse(Key, Value, <<"float list"/utf8>>)
).
-spec parse_bool(binary(), binary()) -> {ok, value()} | {error, snag:snag()}.
parse_bool(Key, Value) ->
case Value of
<<"true"/utf8>> ->
{ok, {b, true}};
<<"false"/utf8>> ->
{ok, {b, false}};
_@1 ->
{error, cannot_parse(Key, Value, <<"bool"/utf8>>)}
end.
-spec parse_string(binary(), binary()) -> {ok, value()} | {error, snag:snag()}.
parse_string(_, Value) ->
parse_flag(Value, fun(A) -> {ok, A} end, fun(A) -> {s, A} end).
-spec parse_string_list(binary(), binary()) -> {ok, value()} |
{error, snag:snag()}.
parse_string_list(_, Value) ->
parse_list_flag(Value, fun(A) -> {ok, A} end, fun(A) -> {ls, A} end).
-spec parse_flag(
binary(),
fun((binary()) -> {ok, EGC} | {error, EGD}),
fun((EGC) -> value())
) -> {ok, value()} | {error, EGD}.
parse_flag(Value, Parse, Construct) ->
_pipe = Value,
_pipe@1 = Parse(_pipe),
gleam@result:map(_pipe@1, Construct).
-spec parse_list_flag(
binary(),
fun((binary()) -> {ok, EGI} | {error, EGJ}),
fun((list(EGI)) -> value())
) -> {ok, value()} | {error, EGJ}.
parse_list_flag(Value, Parse, Construct) ->
_pipe = Value,
_pipe@1 = gleam@string:split(_pipe, <<","/utf8>>),
_pipe@2 = gleam@list:try_map(_pipe@1, Parse),
gleam@result:map(_pipe@2, Construct).
-spec layer_invalid_flag(snag:snag(), binary()) -> snag:snag().
layer_invalid_flag(Err, Flag) ->
_pipe = [<<"invalid flag '"/utf8>>, Flag, <<"'"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
snag:layer(Err, _pipe@1).
-spec no_value_flag_err(binary()) -> snag:snag().
no_value_flag_err(Flag_input) ->
_pipe = [<<"flag '"/utf8>>, Flag_input, <<"' has no assigned value"/utf8>>],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Flag_input).
-spec undefined_flag_err(binary()) -> snag:snag().
undefined_flag_err(Key) ->
_pipe = <<"flag provided but not defined"/utf8>>,
_pipe@1 = snag:new(_pipe),
layer_invalid_flag(_pipe@1, Key).
-spec cannot_parse(binary(), binary(), binary()) -> snag:snag().
cannot_parse(Key, Value, Kind) ->
_pipe = [<<"cannot parse flag '"/utf8>>,
Key,
<<"' value '"/utf8>>,
Value,
<<"' as "/utf8>>,
Kind],
_pipe@1 = gleam@string:concat(_pipe),
_pipe@2 = snag:new(_pipe@1),
layer_invalid_flag(_pipe@2, Key).
-spec help_flag() -> binary().
help_flag() ->
gleam@string:append(<<"--"/utf8>>, <<"help"/utf8>>).
-spec flag_help(binary(), contents()) -> binary().
flag_help(Name, Contents) ->
gleam@string:concat(
[<<"--"/utf8>>,
Name,
<<"="/utf8>>,
<<"<"/utf8>>,
gleam@string:uppercase(Name),
<<">"/utf8>>,
<<"\t\t"/utf8>>,
erlang:element(3, Contents)]
).
-spec flags_help(gleam@map:map_(binary(), contents())) -> binary().
flags_help(Flags) ->
_pipe = Flags,
_pipe@1 = gleam@map:map_values(_pipe, fun flag_help/2),
_pipe@2 = gleam@map:values(_pipe@1),
_pipe@3 = gleam@list:sort(_pipe@2, fun gleam@string:compare/2),
gleam@string:join(_pipe@3, <<"\n\t"/utf8>>).