class VV::CLI
Attributes
cache_path[R]
config_path[R]
data_path[R]
option_router[R]
settings[R]
verbosity[R]
width_override[W]
Public Class Methods
new(version: nil, name: nil, argv: nil, config_path: nil, cache_path: nil, data_path: nil, default_width: nil) { |router| ... }
click to toggle source
# File lib/vv/cli.rb, line 14 def initialize version: nil, name: nil, argv: nil, config_path: nil, cache_path: nil, data_path: nil, default_width: nil default_version = "0.0.1" @version = version || default_version @config_path = config_path @cache_path = cache_path @data_path = data_path @default_width = default_width @width_override = nil @option_router = OptionRouter.new( name: name ) do |router| yield router if block_given? end @settings = nil self.set_default_paths # Most of the time we want to just initialize the # thing fully, but it's helpful to test and debug if # we're not forced to. self.parse_flags argv unless argv.nil? end
Public Instance Methods
await_input(message: nil, force_match: false, input: nil)
click to toggle source
# File lib/vv/cli.rb, line 88 def await_input message: nil, force_match: false, input: nil input = input input ||= Readline.prompt message @prompt_message = message.unstyled @prompt_message ||= "" response = self.parse_message! input: input @prompt_message = nil response end
help?()
click to toggle source
# File lib/vv/cli.rb, line 122 def help? return false if @settings.nil? @settings["-h"] end
name()
click to toggle source
# File lib/vv/cli.rb, line 52 def name @option_router.name end
name_version()
click to toggle source
# File lib/vv/cli.rb, line 56 def name_version [ self.name.unstyled, @version ].join("-") end
parse_flags(argv)
click to toggle source
# File lib/vv/cli.rb, line 60 def parse_flags argv argv = argv.split " " if argv.is_a? String @settings = @option_router.parse argv set_verbosity end
parse_message!(input: @prompt_table = LookupTable.new ignore_case: true)
click to toggle source
# File lib/vv/cli.rb, line 105 def parse_message! input: @prompt_table = LookupTable.new ignore_case: true options = @prompt_message.split(String.colon).last tokens = options.split_english tokens.each do |_token| token = _token.gsub("(","").gsub(")","") alias_token = _token.split("(")[-1].split(")")[0] @prompt_table[token] = true @prompt_table.alias key: alias_token, to: token end response = @prompt_table.lookup_key input response || input end
print_error( error, meta: nil )
click to toggle source
# File lib/vv/cli.rb, line 156 def print_error( error, meta: nil ) error = error.style :red, :bold error.cli_puts end
print_help()
click to toggle source
# File lib/vv/cli.rb, line 132 def print_help option_router.help_doc.cli_print width: self.width end
print_version()
click to toggle source
# File lib/vv/cli.rb, line 136 def print_version version = "#{self.name} version #{@version}" version.cli_puts width: self.width end
set_default_paths()
click to toggle source
# File lib/vv/cli.rb, line 46 def set_default_paths @config_path ||= File.config_home! name_version @cache_path ||= File.cache_home! name_version @data_path ||= File.data_home! name_version end
set_normal_verbosity()
click to toggle source
# File lib/vv/cli.rb, line 66 def set_normal_verbosity @verbosity = :normal end
set_verbosity()
click to toggle source
# File lib/vv/cli.rb, line 70 def set_verbosity verbosity_flags = %w[ -v -vv -vvv -q -s ] flag_set = @settings.keys.includes_any? verbosity_flags return self.set_normal_verbosity unless flag_set @settings.keys.includes_one! verbosity_flags flag = (@settings.keys & verbosity_flags).first index = verbosity_flags.index(flag) @verbosity = %i[ verbose very_verbose very_very_verbose quiet absolute_silence ][index] end
show_options(options)
click to toggle source
# File lib/vv/cli.rb, line 141 def show_options options options.each_with_index do | option, index | line = "#{index}) #{option}" even = ( ( index % 2 ) == 0 ) line = line.style :wheat if even line = line.style :violet unless even line.cli_puts width: self.width end puts end
version?()
click to toggle source
# File lib/vv/cli.rb, line 127 def version? return false if @settings.nil? @settings["-V"] end
width()
click to toggle source
# File lib/vv/cli.rb, line 152 def width @width_override || @default_width end