11require 'completely'
22
33module Bashly
4- # This is a `Command` concern responsible for providing bash completion data
4+ # This is a `Command` and `Flag` concern responsible for providing bash
5+ # completion data
56 module Completions
6- def completion_data ( with_version : true )
7- result = { full_name => completion_words ( with_version : with_version ) }
8-
9- commands . each do |command |
10- result . merge! command . completion_data ( with_version : false )
11- end
7+ module Flag
8+ def completion_data ( command_full_name )
9+ result = { }
10+ comps = allowed || completions
1211
13- result
14- end
12+ if comps
13+ aliases . each do |name |
14+ result [ "#{ command_full_name } *#{ name } " ] = comps
15+ end
16+ end
1517
16- def completion_script
17- completion_generator . script
18+ result
19+ end
1820 end
1921
20- def completion_function ( name = nil )
21- completion_generator . wrapper_function ( name )
22- end
22+ module Command
23+ def completion_data ( with_version : true )
24+ result = { }
2325
24- private
26+ all_full_names . each do |name |
27+ result [ name ] = completion_words ( with_version : with_version )
28+ flags . each do |flag |
29+ result . merge! flag . completion_data ( name )
30+ end
31+ end
32+
33+ commands . each do |command |
34+ result . merge! command . completion_data ( with_version : false )
35+ end
2536
26- def completion_generator
27- Completely ::Completions . new ( completion_data )
28- end
37+ result
38+ end
2939
30- def completion_flag_names
31- flags . map ( & :name ) + flags . map ( & :short )
32- end
40+ def completion_script
41+ completion_generator . script
42+ end
3343
34- def completion_allowed_args
35- flags . map ( & :allowed ) . flatten + args . map ( & :allowed ) . flatten
36- end
44+ def completion_function ( name = nil )
45+ completion_generator . wrapper_function ( name )
46+ end
3747
38- def completion_words ( with_version : false )
39- trivial_flags = %w[ --help -h ]
40- trivial_flags += %w[ --version -v ] if with_version
41- all = (
42- command_names + trivial_flags +
43- completion_flag_names + completion_allowed_args
44- )
48+ private
4549
46- all += completions if completions
47- all . compact . uniq . sort
48- end
50+ def completion_generator
51+ Completely ::Completions . new ( completion_data )
52+ end
53+
54+ def completion_flag_names
55+ flags . map ( &:name ) + flags . map ( &:short )
56+ end
57+
58+ def completion_allowed_args
59+ args . map ( &:allowed ) . flatten
60+ end
61+
62+ def completion_words ( with_version : false )
63+ trivial_flags = %w[ --help -h ]
64+ trivial_flags += %w[ --version -v ] if with_version
65+ all = (
66+ command_aliases + trivial_flags +
67+ completion_flag_names + completion_allowed_args
68+ )
4969
70+ all += completions if completions
71+ all . compact . uniq . sort
72+ end
73+
74+ end
5075 end
51- end
76+ end
0 commit comments