Class: Grouik::Helpers::Cli
- Inherits:
-
Object
- Object
- Grouik::Helpers::Cli
- Defined in:
- src/lib/grouik/helpers/cli.rb
Overview
Cli helper, see Grouik::Cli
Instance Method Summary collapse
- #host_os ⇒ String
-
#license ⇒ String
Get the license.
-
#make_parser(options = {}) ⇒ OptionParser
Provide an
OptionParser
. -
#prepare_options(options) ⇒ Hash
Prepare options.
-
#read_config(path) ⇒ Hash
Read a config file.
-
#version_chapter ⇒ String
Get a displayable version.
Instance Method Details
#host_os ⇒ String
84 85 86 |
# File 'src/lib/grouik/helpers/cli.rb', line 84 def host_os RbConfig::CONFIG['host_os'] end |
#license ⇒ String
Get the license
68 69 70 |
# File 'src/lib/grouik/helpers/cli.rb', line 68 def license Grouik.version_info[:license].to_s.gsub(/\n{3}/mi, "\n\n") end |
#make_parser(options = {}) ⇒ OptionParser
Provide an OptionParser
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'src/lib/grouik/helpers/cli.rb', line 17 def make_parser( = {}) parser = OptionParser.new { basedir: ['--basedir=BASEDIR', 'Basedir [%s]' % [:basedir]], output: ['-o=OUTPUT', '--output=OUTPUT', 'Output [/dev/stdout]'], require: ['-r=REQUIRE', '--require=REQUIRE', 'Required file on startup'], ignores: ['--ignores x,y,z', Array, 'Ignores'], paths: ['--paths x,y,z', Array, 'Paths'], stats: ['--[no-]stats', 'Display some stats'], version: ['--version', 'Display the version and exit'] }.each do |k, v| parser.on(*v) { |o| [k] = o } end parser end |
#prepare_options(options) ⇒ Hash
Prepare options
Process values in order to easify their use
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'src/lib/grouik/helpers/cli.rb', line 42 def () [:require, :output].each do |k| next unless [k] begin [k] = Pathname.new([k]) rescue TypeError next end unless [k].absolute? [k] = Pathname.new(Dir.pwd).join([k]) end end [:ignores, :paths].each do |k| next unless [k] [k] = [[k]] if [k].is_a? String [k] = [k].to_a.map { |s| /#{s}/ } if :ignores == k end end |
#read_config(path) ⇒ Hash
Read a config file
92 93 94 95 96 97 98 99 100 101 |
# File 'src/lib/grouik/helpers/cli.rb', line 92 def read_config(path) file = Pathname.new(path.to_s) config = YAML.safe_load(file.read) h = config.each_with_object({}) do |(k, v), n| n[k.intern] = v end h end |