Current section

51 Versions

Jump to

Compare versions

6 files changed
+245 additions
-126 deletions
  @@ -1,5 +1,5 @@
1 1 name = "glint"
2 - version = "0.17.1"
2 + version = "0.18.0"
3 3
4 4 # Fill out these fields if you intend to generate HTML documentation or publishname = "glint"
5 5 # your project to the Hex package manager.
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"glint">>}.
2 2 {<<"app">>, <<"glint">>}.
3 - {<<"version">>, <<"0.17.1">>}.
3 + {<<"version">>, <<"0.18.0">>}.
4 4 {<<"description">>, <<"Gleam command line argument parsing with basic flag support.">>}.
5 5 {<<"licenses">>, [<<"Apache-2.0">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -10,25 +10,25 @@
10 10 {<<"Repository">>, <<"https://github.com/TanklesXL/glint">>}
11 11 ]}.
12 12 {<<"requirements">>, [
13 + {<<"snag">>, [
14 + {<<"app">>, <<"snag">>},
15 + {<<"optional">>, false},
16 + {<<"requirement">>, <<"~> 0.2">>}
17 + ]},
13 18 {<<"gleam_community_colour">>, [
14 19 {<<"app">>, <<"gleam_community_colour">>},
15 20 {<<"optional">>, false},
16 21 {<<"requirement">>, <<"~> 1.0">>}
17 22 ]},
18 - {<<"gleam_community_ansi">>, [
19 - {<<"app">>, <<"gleam_community_ansi">>},
20 - {<<"optional">>, false},
21 - {<<"requirement">>, <<"~> 1.0">>}
22 - ]},
23 23 {<<"gleam_stdlib">>, [
24 24 {<<"app">>, <<"gleam_stdlib">>},
25 25 {<<"optional">>, false},
26 26 {<<"requirement">>, <<"~> 0.36.0 or ~> 1.0">>}
27 27 ]},
28 - {<<"snag">>, [
29 - {<<"app">>, <<"snag">>},
28 + {<<"gleam_community_ansi">>, [
29 + {<<"app">>, <<"gleam_community_ansi">>},
30 30 {<<"optional">>, false},
31 - {<<"requirement">>, <<"~> 0.2">>}
31 + {<<"requirement">>, <<"~> 1.0">>}
32 32 ]}
33 33 ]}.
34 34 {<<"files">>, [
  @@ -1,5 +1 @@
1 - -record(glint, {
2 - config :: glint:config(),
3 - cmd :: glint:command_node(any()),
4 - global_flags :: gleam@dict:dict(binary(), glint@flag:flag())
5 - }).
1 + -record(glint, {config :: glint:config(), cmd :: glint:command_node(any())}).
  @@ -1,5 +1,5 @@
1 1 {application, glint, [
2 - {vsn, "0.17.1"},
2 + {vsn, "0.18.0"},
3 3 {applications, [gleam_community_ansi,
4 4 gleam_community_colour,
5 5 gleam_stdlib,
  @@ -1,7 +1,7 @@
1 1 -module(glint).
2 2 -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
3 3
4 - -export([with_config/2, with_pretty_help/2, without_pretty_help/1, with_name/2, as_gleam_module/1, command/1, description/2, unnamed_args/2, named_args/2, flag/3, flag_tuple/2, flags/2, global_flag/3, global_flag_tuple/2, global_flags/2, default_pretty_help/0, add/3, new/0, help_flag/0, execute/2, run_and_handle/3, run/2]).
4 + -export([with_config/2, with_pretty_help/2, without_pretty_help/1, with_name/2, as_gleam_module/1, command/1, description/2, unnamed_args/2, named_args/2, flag/3, flag_tuple/2, flags/2, group_flags/3, global_flags/2, group_flag/4, global_flag/3, global_flag_tuple/2, group_flag_tuple/3, default_pretty_help/0, add/3, new/0, help_flag/0, execute/2, run_and_handle/3, run/2]).
5 5 -export_type([config/0, pretty_help/0, glint/1, args_count/0, command/1, command_input/0, command_node/1, out/1, metadata/0, flag_help/0, command_help/0]).
6 6
7 7 -type config() :: {config,
  @@ -14,10 +14,7 @@
14 14 gleam_community@colour:colour(),
15 15 gleam_community@colour:colour()}.
16 16
17 - -opaque glint(GKV) :: {glint,
18 - config(),
19 - command_node(GKV),
20 - gleam@dict:dict(binary(), glint@flag:flag())}.
17 + -opaque glint(GKV) :: {glint, config(), command_node(GKV)}.
21 18
22 19 -type args_count() :: {eq_args, integer()} | {min_args, integer()}.
23 20
  @@ -35,7 +32,8 @@
35 32
36 33 -type command_node(GKX) :: {command_node,
37 34 gleam@option:option(command(GKX)),
38 - gleam@dict:dict(binary(), command_node(GKX))}.
35 + gleam@dict:dict(binary(), command_node(GKX)),
36 + gleam@dict:dict(binary(), glint@flag:flag())}.
39 37
40 38 -type out(GKY) :: {out, GKY} | {help, binary()}.
41 39
  @@ -76,7 +74,7 @@ as_gleam_module(Glint) ->
76 74
77 75 -spec empty_command() -> command_node(any()).
78 76 empty_command() ->
79 - {command_node, none, gleam@dict:new()}.
77 + {command_node, none, gleam@dict:new(), gleam@dict:new()}.
80 78
81 79 -spec do_add(command_node(GMA), list(binary()), command(GMA)) -> command_node(GMA).
82 80 do_add(Root, Path, Contents) ->
  @@ -144,36 +142,96 @@ flags(Cmd, Flags) ->
144 142 end
145 143 ).
146 144
147 - -spec global_flag(glint(GNK), binary(), glint@flag:flag_builder(any())) -> glint(GNK).
148 - global_flag(Glint, Key, Flag) ->
149 - erlang:setelement(
150 - 4,
151 - Glint,
152 - gleam@dict:insert(erlang:element(4, Glint), Key, glint@flag:build(Flag))
153 - ).
145 + -spec do_group_flag(
146 + command_node(GOP),
147 + list(binary()),
148 + binary(),
149 + glint@flag:flag()
150 + ) -> command_node(GOP).
151 + do_group_flag(Node, Path, Name, Flag) ->
152 + case Path of
153 + [] ->
154 + erlang:setelement(
155 + 4,
156 + Node,
157 + gleam@dict:insert(erlang:element(4, Node), Name, Flag)
158 + );
154 159
155 - -spec global_flag_tuple(glint(GNP), {binary(), glint@flag:flag_builder(any())}) -> glint(GNP).
156 - global_flag_tuple(Glint, Tup) ->
157 - global_flag(Glint, erlang:element(1, Tup), erlang:element(2, Tup)).
160 + [Head | Tail] ->
161 + erlang:setelement(
162 + 3,
163 + Node,
164 + (gleam@dict:update(
165 + erlang:element(3, Node),
166 + Head,
167 + fun(Node@1) -> _pipe = Node@1,
168 + _pipe@1 = gleam@option:unwrap(_pipe, empty_command()),
169 + do_group_flag(_pipe@1, Tail, Name, Flag) end
170 + ))
171 + )
172 + end.
173 +
174 + -spec group_flags(
175 + glint(GNY),
176 + list(binary()),
177 + list({binary(), glint@flag:flag()})
178 + ) -> glint(GNY).
179 + group_flags(Glint, Path, Flags) ->
180 + gleam@list:fold(
181 + Flags,
182 + Glint,
183 + fun(Glint@1, Flag) ->
184 + erlang:setelement(
185 + 3,
186 + Glint@1,
187 + do_group_flag(
188 + erlang:element(3, Glint@1),
189 + Path,
190 + erlang:element(1, Flag),
191 + erlang:element(2, Flag)
192 + )
193 + )
194 + end
195 + ).
158 196
159 197 -spec global_flags(glint(GNU), list({binary(), glint@flag:flag()})) -> glint(GNU).
160 198 global_flags(Glint, Flags) ->
199 + group_flags(Glint, [], Flags).
200 +
201 + -spec group_flag(
202 + glint(GOD),
203 + list(binary()),
204 + binary(),
205 + glint@flag:flag_builder(any())
206 + ) -> glint(GOD).
207 + group_flag(Glint, Path, Name, Flag) ->
161 208 erlang:setelement(
162 - 4,
209 + 3,
163 210 Glint,
164 - (gleam@list:fold(
165 - Flags,
166 - erlang:element(4, Glint),
167 - fun(Acc, Elem) ->
168 - gleam@dict:insert(
169 - Acc,
170 - erlang:element(1, Elem),
171 - erlang:element(2, Elem)
172 - )
173 - end
174 - ))
211 + do_group_flag(
212 + erlang:element(3, Glint),
213 + Path,
214 + Name,
215 + glint@flag:build(Flag)
216 + )
175 217 ).
176 218
219 + -spec global_flag(glint(GNK), binary(), glint@flag:flag_builder(any())) -> glint(GNK).
220 + global_flag(Glint, Key, Flag) ->
221 + group_flag(Glint, [], Key, Flag).
222 +
223 + -spec global_flag_tuple(glint(GNP), {binary(), glint@flag:flag_builder(any())}) -> glint(GNP).
224 + global_flag_tuple(Glint, Tup) ->
225 + group_flag(Glint, [], erlang:element(1, Tup), erlang:element(2, Tup)).
226 +
227 + -spec group_flag_tuple(
228 + glint(GOJ),
229 + list(binary()),
230 + {binary(), glint@flag:flag_builder(any())}
231 + ) -> glint(GOJ).
232 + group_flag_tuple(Glint, Path, Flag) ->
233 + group_flag(Glint, Path, erlang:element(1, Flag), erlang:element(2, Flag)).
234 +
177 235 -spec args_compare(args_count(), integer()) -> {ok, nil} | {error, snag:snag()}.
178 236 args_compare(Expected, Actual) ->
179 237 _pipe = case Expected of
  @@ -212,7 +270,7 @@ default_pretty_help() ->
212 270 value => _assert_fail,
213 271 module => <<"glint"/utf8>>,
214 272 function => <<"default_pretty_help"/utf8>>,
215 - line => 517})
273 + line => 597})
216 274 end,
217 275 _assert_subject@1 = gleam_community@colour:from_rgb255(255, 175, 243),
218 276 {ok, Flags_colour} = case _assert_subject@1 of
  @@ -223,7 +281,7 @@ default_pretty_help() ->
223 281 value => _assert_fail@1,
224 282 module => <<"glint"/utf8>>,
225 283 function => <<"default_pretty_help"/utf8>>,
226 - line => 518})
284 + line => 598})
227 285 end,
228 286 _assert_subject@2 = gleam_community@colour:from_rgb255(252, 226, 174),
229 287 {ok, Subcommands_colour} = case _assert_subject@2 of
  @@ -234,7 +292,7 @@ default_pretty_help() ->
234 292 value => _assert_fail@2,
235 293 module => <<"glint"/utf8>>,
236 294 function => <<"default_pretty_help"/utf8>>,
237 - line => 519})
295 + line => 599})
238 296 end,
239 297 {pretty_help, Usage_colour, Flags_colour, Subcommands_colour}.
240 298
  @@ -330,12 +388,8 @@ build_subcommands_help(Subcommands) ->
330 388 end
331 389 ).
332 390
333 - -spec build_command_help_metadata(
334 - binary(),
335 - command_node(any()),
336 - gleam@dict:dict(binary(), glint@flag:flag())
337 - ) -> command_help().
338 - build_command_help_metadata(Name, Node, Global_flags) ->
391 + -spec build_command_help_metadata(binary(), command_node(any())) -> command_help().
392 + build_command_help_metadata(Name, Node) ->
339 393 {Description, Flags, Unnamed_args, Named_args} = case erlang:element(
340 394 2,
341 395 Node
  @@ -346,7 +400,10 @@ build_command_help_metadata(Name, Node, Global_flags) ->
346 400 {some, Cmd} ->
347 401 {erlang:element(4, Cmd),
348 402 build_flags_help(
349 - gleam@dict:merge(Global_flags, erlang:element(3, Cmd))
403 + gleam@dict:merge(
404 + erlang:element(4, Node),
405 + erlang:element(3, Cmd)
406 + )
350 407 ),
351 408 erlang:element(5, Cmd),
352 409 erlang:element(6, Cmd)}
  @@ -456,7 +513,7 @@ string_map(S, F) ->
456 513
457 514 -spec new() -> glint(any()).
458 515 new() ->
459 - {glint, {config, none, none, false}, empty_command(), gleam@dict:new()}.
516 + {glint, {config, none, none, false}, empty_command()}.
460 517
461 518 -spec subcommands_help_to_string(list(metadata()), config()) -> binary().
462 519 subcommands_help_to_string(Help, Config) ->
  @@ -581,28 +638,22 @@ command_help_to_string(Help, Config) ->
581 638 _pipe@3 = gleam@list:filter(_pipe@2, fun is_not_empty/1),
582 639 gleam@string:join(_pipe@3, <<"\n\n"/utf8>>).
583 640
584 - -spec cmd_help(
585 - list(binary()),
586 - command_node(any()),
587 - config(),
588 - gleam@dict:dict(binary(), glint@flag:flag())
589 - ) -> binary().
590 - cmd_help(Path, Cmd, Config, Global_flags) ->
641 + -spec cmd_help(list(binary()), command_node(any()), config()) -> binary().
642 + cmd_help(Path, Cmd, Config) ->
591 643 _pipe = Path,
592 644 _pipe@1 = gleam@list:reverse(_pipe),
593 645 _pipe@2 = gleam@string:join(_pipe@1, <<" "/utf8>>),
594 - _pipe@3 = build_command_help_metadata(_pipe@2, Cmd, Global_flags),
646 + _pipe@3 = build_command_help_metadata(_pipe@2, Cmd),
595 647 command_help_to_string(_pipe@3, Config).
596 648
597 649 -spec execute_root(
598 650 list(binary()),
599 651 config(),
600 - command_node(GOK),
601 - gleam@dict:dict(binary(), glint@flag:flag()),
652 + command_node(GPF),
602 653 list(binary()),
603 654 list(binary())
604 - ) -> {ok, out(GOK)} | {error, binary()}.
605 - execute_root(Path, Config, Cmd, Global_flags, Args, Flag_inputs) ->
655 + ) -> {ok, out(GPF)} | {error, binary()}.
656 + execute_root(Path, Config, Cmd, Args, Flag_inputs) ->
606 657 Res = begin
607 658 _pipe@7 = (gleam@option:map(
608 659 erlang:element(2, Cmd),
  @@ -611,7 +662,7 @@ execute_root(Path, Config, Cmd, Global_flags, Args, Flag_inputs) ->
611 662 gleam@list:try_fold(
612 663 Flag_inputs,
613 664 gleam@dict:merge(
614 - Global_flags,
665 + erlang:element(4, Cmd),
615 666 erlang:element(3, Contents)
616 667 ),
617 668 fun glint@flag:update_flags/2
  @@ -702,7 +753,7 @@ execute_root(Path, Config, Cmd, Global_flags, Args, Flag_inputs) ->
702 753 _pipe@9 = snag:context(_pipe@8, <<"failed to run command"/utf8>>),
703 754 gleam@result:map_error(
704 755 _pipe@9,
705 - fun(Err) -> {Err, cmd_help(Path, Cmd, Config, Global_flags)} end
756 + fun(Err) -> {Err, cmd_help(Path, Cmd, Config)} end
706 757 )
707 758 end,
708 759 case Res of
  @@ -717,32 +768,38 @@ execute_root(Path, Config, Cmd, Global_flags, Args, Flag_inputs) ->
717 768 end.
718 769
719 770 -spec do_execute(
720 - command_node(GOC),
771 + command_node(GOX),
721 772 config(),
722 - gleam@dict:dict(binary(), glint@flag:flag()),
723 773 list(binary()),
724 774 list(binary()),
725 775 boolean(),
726 776 list(binary())
727 - ) -> {ok, out(GOC)} | {error, binary()}.
728 - do_execute(Cmd, Config, Global_flags, Args, Flags, Help, Command_path) ->
777 + ) -> {ok, out(GOX)} | {error, binary()}.
778 + do_execute(Cmd, Config, Args, Flags, Help, Command_path) ->
729 779 case Args of
730 780 [] when Help ->
731 781 _pipe = Command_path,
732 - _pipe@1 = cmd_help(_pipe, Cmd, Config, Global_flags),
782 + _pipe@1 = cmd_help(_pipe, Cmd, Config),
733 783 _pipe@2 = {help, _pipe@1},
734 784 {ok, _pipe@2};
735 785
736 786 [] ->
737 - execute_root(Command_path, Config, Cmd, Global_flags, [], Flags);
787 + execute_root(Command_path, Config, Cmd, [], Flags);
738 788
739 789 [Arg | Rest] ->
740 790 case gleam@dict:get(erlang:element(3, Cmd), Arg) of
741 - {ok, Cmd@1} ->
791 + {ok, Sub_command} ->
792 + Sub_command@1 = erlang:setelement(
793 + 4,
794 + Sub_command,
795 + gleam@dict:merge(
796 + erlang:element(4, Cmd),
797 + erlang:element(4, Sub_command)
798 + )
799 + ),
742 800 do_execute(
743 - Cmd@1,
801 + Sub_command@1,
744 802 Config,
745 - Global_flags,
746 803 Rest,
747 804 Flags,
748 805 Help,
  @@ -751,23 +808,16 @@ do_execute(Cmd, Config, Global_flags, Args, Flags, Help, Command_path) ->
751 808
752 809 _ when Help ->
753 810 _pipe@3 = Command_path,
754 - _pipe@4 = cmd_help(_pipe@3, Cmd, Config, Global_flags),
811 + _pipe@4 = cmd_help(_pipe@3, Cmd, Config),
755 812 _pipe@5 = {help, _pipe@4},
756 813 {ok, _pipe@5};
757 814
758 815 _ ->
759 - execute_root(
760 - Command_path,
761 - Config,
762 - Cmd,
763 - Global_flags,
764 - Args,
765 - Flags
766 - )
816 + execute_root(Command_path, Config, Cmd, Args, Flags)
767 817 end
768 818 end.
769 819
770 - -spec execute(glint(GNY), list(binary())) -> {ok, out(GNY)} | {error, binary()}.
820 + -spec execute(glint(GOT), list(binary())) -> {ok, out(GOT)} | {error, binary()}.
771 821 execute(Glint, Args) ->
772 822 Help_flag = help_flag(),
773 823 {Help, Args@2} = case gleam@list:pop(Args, fun(S) -> S =:= Help_flag end) of
  @@ -784,14 +834,13 @@ execute(Glint, Args) ->
784 834 do_execute(
785 835 erlang:element(3, Glint),
786 836 erlang:element(2, Glint),
787 - erlang:element(4, Glint),
788 837 Args@3,
789 838 Flags,
790 839 Help,
791 840 []
792 841 ).
793 842
794 - -spec run_and_handle(glint(GOS), list(binary()), fun((GOS) -> any())) -> nil.
843 + -spec run_and_handle(glint(GPN), list(binary()), fun((GPN) -> any())) -> nil.
795 844 run_and_handle(Glint, Args, Handle) ->
796 845 case execute(Glint, Args) of
797 846 {error, S} ->
Loading more files…