Class: VV::CLI
Instance Attribute Summary collapse
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#data_path ⇒ Object
readonly
Returns the value of attribute data_path.
-
#option_router ⇒ Object
readonly
Returns the value of attribute option_router.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#verbosity ⇒ Object
readonly
Returns the value of attribute verbosity.
-
#width_override ⇒ Object
writeonly
Sets the attribute width_override.
Instance Method Summary collapse
- #await_input(message: nil, force_match: false, input: nil) ⇒ Object
- #help? ⇒ Boolean
-
#initialize(version: nil, name: nil, argv: nil, config_path: nil, cache_path: nil, data_path: nil, default_width: nil) ⇒ CLI
constructor
A new instance of CLI.
- #name ⇒ Object
- #name_version ⇒ Object
- #parse_flags(argv) ⇒ Object
- #parse_message!(input:) ⇒ Object
- #print_error(error, meta: nil) ⇒ Object
- #print_help ⇒ Object
- #print_version ⇒ Object
- #set_default_paths ⇒ Object
- #set_normal_verbosity ⇒ Object
- #set_verbosity ⇒ Object
- #show_options(options) ⇒ Object
- #version? ⇒ Boolean
- #width ⇒ Object
Constructor Details
#initialize(version: nil, name: nil, argv: nil, config_path: nil, cache_path: nil, data_path: nil, default_width: nil) ⇒ CLI
Returns a new instance of CLI.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 |
Instance Attribute Details
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def cache_path @cache_path end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def config_path @config_path end |
#data_path ⇒ Object (readonly)
Returns the value of attribute data_path.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def data_path @data_path end |
#option_router ⇒ Object (readonly)
Returns the value of attribute option_router.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def option_router @option_router end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def settings @settings end |
#verbosity ⇒ Object (readonly)
Returns the value of attribute verbosity.
7 8 9 |
# File 'lib/vv/cli.rb', line 7 def verbosity @verbosity end |
#width_override=(value) ⇒ Object (writeonly)
Sets the attribute width_override
5 6 7 |
# File 'lib/vv/cli.rb', line 5 def width_override=(value) @width_override = value end |
Instance Method Details
#await_input(message: nil, force_match: false, input: nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/vv/cli.rb', line 88 def await_input message: nil, force_match: false, input: nil input = input input ||= Readline.prompt @prompt_message = .unstyled @prompt_message ||= "" response = self. input: input @prompt_message = nil response end |
#help? ⇒ Boolean
122 123 124 125 |
# File 'lib/vv/cli.rb', line 122 def help? return false if @settings.nil? @settings["-h"] end |
#name ⇒ Object
52 53 54 |
# File 'lib/vv/cli.rb', line 52 def name @option_router.name end |
#name_version ⇒ Object
56 57 58 |
# File 'lib/vv/cli.rb', line 56 def name_version [ self.name.unstyled, @version ].join("-") end |
#parse_flags(argv) ⇒ Object
60 61 62 63 64 |
# 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:) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/vv/cli.rb', line 105 def input: @prompt_table = LookupTable.new ignore_case: true = @prompt_message.split(String.colon).last tokens = .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) ⇒ Object
156 157 158 159 |
# File 'lib/vv/cli.rb', line 156 def print_error( error, meta: nil ) error = error.style :red, :bold error.cli_puts end |
#print_help ⇒ Object
132 133 134 |
# File 'lib/vv/cli.rb', line 132 def print_help option_router.help_doc.cli_print width: self.width end |
#print_version ⇒ Object
136 137 138 139 |
# 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 ⇒ Object
46 47 48 49 50 |
# 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 ⇒ Object
66 67 68 |
# File 'lib/vv/cli.rb', line 66 def set_normal_verbosity @verbosity = :normal end |
#set_verbosity ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# 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) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/vv/cli.rb', line 141 def .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? ⇒ Boolean
127 128 129 130 |
# File 'lib/vv/cli.rb', line 127 def version? return false if @settings.nil? @settings["-V"] end |
#width ⇒ Object
152 153 154 |
# File 'lib/vv/cli.rb', line 152 def width @width_override || @default_width end |