Class: Cane::CLI::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/cane/cli/spec.rb

Overview

Provides a specification for the command line interface that drives documentation, parsing, and default values.

Defined Under Namespace

Classes: OptionsHandled

Constant Summary collapse

DEFAULTS =
{
  abc_glob:       '{app,lib}/**/*.rb',
  abc_max:        '15',
  style_glob:     '{app,lib,spec}/**/*.rb',
  style_measure:  '80',
  doc_glob:       '{app,lib}/**/*.rb',
  max_violations: '0',
}

Instance Method Summary collapse

Constructor Details

#initializeSpec

Returns a new instance of Spec.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cane/cli/spec.rb', line 23

def initialize
  add_banner

  add_abc_options
  add_style_options
  add_doc_options
  add_threshold_options
  add_cane_options

  add_version
  add_help
end

Instance Method Details

#add_abc_optionsObject



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

def add_abc_options
  add_option %w(--abc-glob GLOB), "Glob to run ABC metrics over"
  add_option %w(--abc-max VALUE), "Ignore methods under this complexity"
  add_option %w(--no-abc), "Disable ABC checking"

  parser.separator ""
end

#add_bannerObject



52
53
54
55
56
57
58
59
# File 'lib/cane/cli/spec.rb', line 52

def add_banner
  parser.banner = <<-BANNER
Usage: cane [options]

You can also put these options in a .cane file.

BANNER
end

#add_cane_optionsObject



93
94
95
96
97
# File 'lib/cane/cli/spec.rb', line 93

def add_cane_options
  add_option %w(--max-violations VALUE), "Max allowed violations"

  parser.separator ""
end

#add_doc_optionsObject



77
78
79
80
81
82
# File 'lib/cane/cli/spec.rb', line 77

def add_doc_options
  add_option %w(--doc-glob GLOB), "Glob to run documentation checks over"
  add_option %w(--no-doc), "Disable documentation checking"

  parser.separator ""
end

#add_helpObject



106
107
108
109
110
111
# File 'lib/cane/cli/spec.rb', line 106

def add_help
  parser.on_tail("-h", "--help", "Show this message") do
    puts parser
    raise OptionsHandled
  end
end

#add_option(option, description) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cane/cli/spec.rb', line 113

def add_option(option, description)
  option_key = option[0].gsub('--', '').tr('-', '_').to_sym

  if DEFAULTS.has_key?(option_key)
    description += " (default: %s)" % DEFAULTS[option_key]
  end

  parser.on(option.join(' '), description) do |v|
    options[option_key] = v
  end
end

#add_style_optionsObject



69
70
71
72
73
74
75
# File 'lib/cane/cli/spec.rb', line 69

def add_style_options
  add_option %w(--style-glob GLOB), "Glob to run style metrics over"
  add_option %w(--style-measure VALUE), "Max line length"
  add_option %w(--no-style), "Disable style checking"

  parser.separator ""
end

#add_threshold_optionsObject



84
85
86
87
88
89
90
91
# File 'lib/cane/cli/spec.rb', line 84

def add_threshold_options
  desc = "If FILE contains a number, verify it is >= to THRESHOLD."
  parser.on("--gte FILE,THRESHOLD", Array, desc) do |opts|
    (options[:threshold] ||= []) << opts.unshift(:>=)
  end

  parser.separator ""
end

#add_versionObject



99
100
101
102
103
104
# File 'lib/cane/cli/spec.rb', line 99

def add_version
  parser.on_tail("--version", "Show version") do
    puts Cane::VERSION
    raise OptionsHandled
  end
end

#get_default_optionsObject



44
45
46
47
48
49
50
# File 'lib/cane/cli/spec.rb', line 44

def get_default_options
  if File.exists?('./.cane')
    File.read('./.cane').gsub("\n", ' ').split(' ')
  else
    []
  end
end

#optionsObject



125
126
127
# File 'lib/cane/cli/spec.rb', line 125

def options
  @options ||= {}
end

#parse(args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/cane/cli/spec.rb', line 36

def parse(args)
  parser.parse!(get_default_options + args)

  Translator.new(options, DEFAULTS).to_hash
rescue OptionsHandled
  nil
end

#parserObject



129
130
131
# File 'lib/cane/cli/spec.rb', line 129

def parser
  @parser ||= OptionParser.new
end