Class: Arli::CLI::Parser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/arli/cli/parser.rb

Constant Summary collapse

SUPPORTED_FORMATS =
%w[cmake text json yaml]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Arli.config, command: nil) ⇒ Parser

Returns a new instance of Parser.



13
14
15
16
17
18
19
# File 'lib/arli/cli/parser.rb', line 13

def initialize(config: Arli.config, command: nil)
  super(nil, 22)

  self.config       = config
  self.command      = command
  self.output_lines = ::Array.new
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/arli/cli/parser.rb', line 9

def command
  @command
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/arli/cli/parser.rb', line 9

def config
  @config
end

#output_linesObject

Returns the value of attribute output_lines.



9
10
11
# File 'lib/arli/cli/parser.rb', line 9

def output_lines
  @output_lines
end

Instance Method Details

#command_helpObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/arli/cli/parser.rb', line 188

def command_help

  header 'Available Commands'
  subtext = ''
  factory.command_parsers.each_pair do |command, config|
    subtext << %Q/#{sprintf('    %-12s', command.to_s).green}#{sprintf('%s', config[:sentence]).blue}\n/
  end
  subtext << <<-EOS

See #{Arli::Configuration::ARLI_COMMAND.blue + ' command '.green + '--help'.yellow} for more information on a specific command.
  EOS
  subtext
end

#common_help_optionsObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/arli/cli/parser.rb', line 215

def common_help_options
  on('-C', '--no-color',
     'Disable any color output.') do |*|
    Colored2.disable! # if $stdout.tty?
    config.no_color = true
  end
  on('-D', '--debug',
     'Print debugging info.') do |v|
    config.debug = true
  end
  on('-t', '--trace',
     'Print exception stack traces.') do |v|
    config.trace = v
  end
  on('-v', '--verbose',
     'Print more information.') do |v|
    config.verbose = true
  end
  on('-q', '--quiet',
     'Print less information.') do |v|
    config.quiet = true
  end
  on('-V', '--version',
     'Print current version and exit') do |v|
    print_version_copyright
    Arli.config.help = true
  end
end

#factoryObject



211
212
213
# File 'lib/arli/cli/parser.rb', line 211

def factory
  Arli::CLI::ParserFactory
end

#header(string) ⇒ Object



160
161
162
163
# File 'lib/arli/cli/parser.rb', line 160

def header(string)
  output "#{string.bold.magenta}:"
  output
end

#indentObject



249
250
251
# File 'lib/arli/cli/parser.rb', line 249

def indent
  '    '
end

#option_arlifile_lock_formatObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/arli/cli/parser.rb', line 47

def option_arlifile_lock_format
  on('-f', '--format FMT',
     "Arli writes an #{'Arlifile.lock'.green} with resolved info.",
     "The default format is #{'text'.bold.yellow}. Use -f to set it",
     "to one of: #{SUPPORTED_FORMATS.join(', ').bold.yellow}\n\n") do |v|
    if SUPPORTED_FORMATS.include?(v.downcase)
      config.arlifile.lock_format = v.downcase.to_sym
    else
      raise ::OptionParser::InvalidOption,
            "#{v.yellow} is not a supported lock file format"
    end
  end
end

#option_arlifile_pathObject



37
38
39
40
41
42
43
# File 'lib/arli/cli/parser.rb', line 37

def option_arlifile_path
  on('-a', '--arli-path PATH',
     'An alternate folder with the ' + 'Arlifile'.green + ' file.',
     "Defaults to the current directory.\n\n") do |v|
    config.arlifile.path = v
  end
end

#option_bundleObject



30
31
32
33
34
35
# File 'lib/arli/cli/parser.rb', line 30

def option_bundle
  option_lib_home
  option_arlifile_path
  option_arlifile_lock_format
  option_if_exists
end

#option_generateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/arli/cli/parser.rb', line 100

def option_generate
  on('-w', '--workspace DIR',
     'a top-level folder under which the project will be created',
     'Default is the current folder.' + "\n\n") do |v|
    config.generate.workspace = v
  end

  # on('-L', '--libs "LIBS"',
  #    'Comma separated list of library names, or name ',
  #    'substrings, to be searched for and added to the ',
  #    'initial Arlifile. Multiple matches are added anyway',
  #    'while no matches are skipped' + "\n\n") do |v|
  #   config.generate.libs = v.split(',')
  # end

  option_if_exists('project')
end

#option_help(commands: false, command_name: nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/arli/cli/parser.rb', line 134

def option_help(commands: false, command_name: nil)
  common_help_options
  on('-h', '--help', 'prints this help') do
    ::Arli.config.help = true

    command_hash = output_command_description(command_name)
    output_examples(command_hash[:examples]) if command_hash && command_hash[:examples]
    output_command_help if commands
    output_help
    output_command_aliases(command_name) if command_name
    print_version_copyright
  end
end

#option_help_with_subtextObject



176
177
178
# File 'lib/arli/cli/parser.rb', line 176

def option_help_with_subtext
  option_help
end

#option_if_exists(what = 'library') ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/arli/cli/parser.rb', line 118

def option_if_exists(what = 'library')
  on('-e', '--if-exists ACTION',
     "If a #{what} folder already exists, by default",
     'it will be overwritten or updated if possible.',
     'Alternatively you can either ' + 'abort'.bold.blue + ' or ' + 'backup'.bold.blue
  ) do |v|
    if v == 'abort'
      config.if_exists.abort     = true
      config.if_exists.overwrite = false
    elsif v == 'backup'
      config.if_exists.backup = true
    end
  end
  sep ' '
end

#option_installObject



25
26
27
28
# File 'lib/arli/cli/parser.rb', line 25

def option_install
  option_lib_home
  option_if_exists
end

#option_lib_homeObject



61
62
63
64
65
66
67
# File 'lib/arli/cli/parser.rb', line 61

def option_lib_home
  on('-l', '--lib-path PATH',
     'Destination: typically your Arduino libraries folder',
     "Defaults to #{'~/Documents/Arduino/Libraries'.green}\n\n") do |v|
    config.libraries.path = v
  end
end

#option_searchObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arli/cli/parser.rb', line 69

def option_search
  on('-d', '--database URL',
     'a JSON(.gz) file path or a URL of the library database.',
     'Defaults to the Arduino-maintained database.' + "\n\n") do |v|
    config.database.path = v
  end

  on('-m', '--max NUMBER',
     'if provided, limits the result set using the ',
     'total number of the unique library name matches.',
     'Default is 0, which means no limit.' + "\n\n") do |v|
    config.search.results.limit = v.to_i if v
  end

  formats = Arli::Library::MultiVersion.format_methods

  on('-f', '--format FMT',
     "Optional format of the search results.",
     "The default is #{'short'.bold.yellow}. Available ",
     "formats: #{formats.join(', ').bold.yellow}\n\n") do |v|
    if formats.include?(v.downcase.to_sym)
      config.search.results.output_format = v.downcase.to_sym
    else
      raise ::OptionParser::InvalidOption,
            "#{v.yellow} is not a supported search result format"
    end
  end

  option_search_attributes
end

#option_search_attributesObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/arli/cli/parser.rb', line 149

def option_search_attributes
  on('-A', '--print-attrs', 'prints full list of available library',
     'attributes that can be used in search strings.', ' ') do

    ::Arli.config.help = true
    output ''
    header('Arduino Library Attributes:')
    output "" + Arduino::Library::Types::LIBRARY_PROPERTIES.keys.join("\n  • ") + "\n\n"
  end
end

#output(value = nil) ⇒ Object



202
203
204
205
# File 'lib/arli/cli/parser.rb', line 202

def output(value = nil)
  self.output_lines << value if value
  self.output_lines
end

#output_command_description(command_name) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/arli/cli/parser.rb', line 253

def output_command_description(command_name)
  command_hash = factory.command_parsers[command_name]

  if command_hash
    if command_hash.description
      header 'Description'
      if command_hash.sentence
        output indent + command_hash.sentence.bold.blue
        output ''
      end

      text = Array(command_hash[:description]).flatten.join(' ')
      output text.reformat_wrapped(width = 70, indent_with = 4).yellow.dark
    end
  end

  command_hash
end

#output_command_helpObject



184
185
186
# File 'lib/arli/cli/parser.rb', line 184

def output_command_help
  output command_help
end

#output_examples(examples) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/arli/cli/parser.rb', line 165

def output_examples(examples)
  header 'Examples'
  indent = '    '
  examples.each do |example|
    output
    output indent + '# ' + example[:desc]
    output indent + example[:cmd].green
    output ''
  end
end

#output_helpObject



180
181
182
# File 'lib/arli/cli/parser.rb', line 180

def output_help
  output self.to_s
end


207
208
209
# File 'lib/arli/cli/parser.rb', line 207

def print
  puts output.join("\n") unless output.empty?
end


244
245
246
247
# File 'lib/arli/cli/parser.rb', line 244

def print_version_copyright
  output << Arli::Configuration::ARLI_COMMAND.bold.yellow + ' (' + Arli::VERSION.bold.green + ')' +
      ' © 2017 Konstantin Gredeskoul, MIT License.'.dark unless Arli.config.quiet
end

#sep(text = nil) ⇒ Object



21
22
23
# File 'lib/arli/cli/parser.rb', line 21

def sep(text = nil)
  separator text || ''
end