Class: VV::CLI

Inherits:
Object show all
Defined in:
lib/vv/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (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_pathObject (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_pathObject (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_routerObject (readonly)

Returns the value of attribute option_router.



7
8
9
# File 'lib/vv/cli.rb', line 7

def option_router
  @option_router
end

#settingsObject (readonly)

Returns the value of attribute settings.



7
8
9
# File 'lib/vv/cli.rb', line 7

def settings
  @settings
end

#verbosityObject (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

Parameters:

  • value

    the value to set the attribute width_override to.



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 message

  @prompt_message   = message.unstyled
  @prompt_message ||= ""

  response = self.parse_message! input: input

  @prompt_message = nil

  response
end

#help?Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/vv/cli.rb', line 122

def help?
  return false if @settings.nil?
  @settings["-h"]
end

#nameObject



52
53
54
# File 'lib/vv/cli.rb', line 52

def name
  @option_router.name
end

#name_versionObject



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 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


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


132
133
134
# File 'lib/vv/cli.rb', line 132

def print_help
  option_router.help_doc.cli_print width: self.width
end


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_pathsObject



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_verbosityObject



66
67
68
# File 'lib/vv/cli.rb', line 66

def set_normal_verbosity
  @verbosity = :normal
end

#set_verbosityObject



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 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?Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/vv/cli.rb', line 127

def version?
  return false if @settings.nil?
  @settings["-V"]
end

#widthObject



152
153
154
# File 'lib/vv/cli.rb', line 152

def width
  @width_override || @default_width
end