Class: DocoptCompgen::Generator
- Inherits:
-
Object
- Object
- DocoptCompgen::Generator
- Defined in:
- lib/docopt_compgen/generator.rb
Instance Method Summary collapse
- #get_op(node) ⇒ Object
- #include_files?(node) ⇒ Boolean
- #indent(str, level) ⇒ Object
-
#initialize(command, node, command_name: nil, namespace: nil) ⇒ Generator
constructor
A new instance of Generator.
- #make(command_name, node, level) ⇒ Object
- #make_compreply(node) ⇒ Object
-
#make_function(command_name, op, level, words, include_files, subcommand_switch) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #make_subcommand_switch(command_name, level, subcommands) ⇒ Object
- #path_arguments?(arguments) ⇒ Boolean
- #path_options?(options) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(command, node, command_name: nil, namespace: nil) ⇒ Generator
Returns a new instance of Generator.
3 4 5 6 7 8 |
# File 'lib/docopt_compgen/generator.rb', line 3 def initialize(command, node, command_name: nil, namespace: nil) @command = command ? File.basename(command) : command_name @node = node @command_name = command_name || Util.slugify(@command) @namespace = '_' + namespace end |
Instance Method Details
#get_op(node) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/docopt_compgen/generator.rb', line 14 def get_op(node) if node.subcommands.length > 0 'eq' else 'ge' end end |
#include_files?(node) ⇒ Boolean
30 31 32 |
# File 'lib/docopt_compgen/generator.rb', line 30 def include_files?(node) return path_arguments?(node.arguments) || (node.) end |
#indent(str, level) ⇒ Object
10 11 12 |
# File 'lib/docopt_compgen/generator.rb', line 10 def indent(str, level) "\n" + str.strip.lines.map { |s| (' ' * level) + s }.join end |
#make(command_name, node, level) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/docopt_compgen/generator.rb', line 46 def make(command_name, node, level) subcommand_switch = make_subcommand_switch( command_name, level, node.subcommands, ) op = get_op(node) include_files, words = make_compreply(node) functions = [ make_function( command_name, op, level, words, include_files, subcommand_switch, ), ] node.subcommands.each do |subcommand_name, subcommand_node| functions << make( '%s_%s' % [command_name, subcommand_name], subcommand_node, level + 1, ) end return functions.join("\n") end |
#make_compreply(node) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/docopt_compgen/generator.rb', line 34 def make_compreply(node) words = [] if node..length > 0 words << node. end words << node.subcommands.keys return [include_files?(node), words.flatten.join(' ')] end |
#make_function(command_name, op, level, words, include_files, subcommand_switch) ⇒ Object
rubocop:disable Metrics/ParameterLists
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/docopt_compgen/generator.rb', line 78 def make_function(command_name, op, level, words, include_files, subcommand_switch) # rubocop:disable Metrics/ParameterLists files = '' if include_files files = indent(<<~EOF, 8) local IFS=$'\\n' # Handle filenames with spaces. COMPREPLY+=($(compgen -f -- "$cur")) EOF end return <<~EOF function #{@namespace}_#{command_name} { local cur cur="${COMP_WORDS[COMP_CWORD]}" if [ "$COMP_CWORD" -#{op} #{level} ]; then COMPREPLY=($(compgen -W '#{words}' -- "$cur"))#{files}#{subcommand_switch} fi } EOF end |
#make_subcommand_switch(command_name, level, subcommands) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/docopt_compgen/generator.rb', line 99 def make_subcommand_switch(command_name, level, subcommands) if subcommands.length == 0 return '' end cases = subcommands.map do |subcommand_name, _node| "#{subcommand_name}) #{@namespace}_#{command_name}_#{subcommand_name} ;;" end.join("\n ") return indent(<<~EOF, 4) else case ${COMP_WORDS[#{level}]} in #{cases} esac EOF end |
#path_arguments?(arguments) ⇒ Boolean
22 23 24 |
# File 'lib/docopt_compgen/generator.rb', line 22 def path_arguments?(arguments) arguments.any? { |a| %w[<file> <dir> <path>].include?(a) } end |
#path_options?(options) ⇒ Boolean
26 27 28 |
# File 'lib/docopt_compgen/generator.rb', line 26 def () .any? { |a| %w[--file --dir --path].include?(a) } end |
#to_s ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/docopt_compgen/generator.rb', line 116 def to_s content = make(@command_name, @node, 1) return <<~EOF #!/bin/bash # shellcheck disable=SC2207 #{content} complete -o bashdefault -o default -o filenames -F #{@namespace}_#{@command_name} #{@command} EOF end |