Class: Cane::CLI::Spec
- Inherits:
-
Object
- Object
- Cane::CLI::Spec
- 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
- #add_abc_options ⇒ Object
- #add_banner ⇒ Object
- #add_cane_options ⇒ Object
- #add_doc_options ⇒ Object
- #add_help ⇒ Object
- #add_option(option, description) ⇒ Object
- #add_style_options ⇒ Object
- #add_threshold_options ⇒ Object
- #add_version ⇒ Object
- #get_default_options ⇒ Object
-
#initialize ⇒ Spec
constructor
A new instance of Spec.
- #options ⇒ Object
- #parse(args) ⇒ Object
- #parser ⇒ Object
Constructor Details
#initialize ⇒ Spec
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_version add_help end |
Instance Method Details
#add_abc_options ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/cane/cli/spec.rb', line 61 def 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_banner ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/cane/cli/spec.rb', line 52 def parser. = <<-BANNER Usage: cane [options] You can also put these options in a .cane file. BANNER end |
#add_cane_options ⇒ Object
93 94 95 96 97 |
# File 'lib/cane/cli/spec.rb', line 93 def add_option %w(--max-violations VALUE), "Max allowed violations" parser.separator "" end |
#add_doc_options ⇒ Object
77 78 79 80 81 82 |
# File 'lib/cane/cli/spec.rb', line 77 def add_option %w(--doc-glob GLOB), "Glob to run documentation checks over" add_option %w(--no-doc), "Disable documentation checking" parser.separator "" end |
#add_help ⇒ Object
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| [option_key] = v end end |
#add_style_options ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/cane/cli/spec.rb', line 69 def 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_options ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/cane/cli/spec.rb', line 84 def desc = "If FILE contains a number, verify it is >= to THRESHOLD." parser.on("--gte FILE,THRESHOLD", Array, desc) do |opts| ([:threshold] ||= []) << opts.unshift(:>=) end parser.separator "" end |
#add_version ⇒ Object
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_options ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/cane/cli/spec.rb', line 44 def if File.exists?('./.cane') File.read('./.cane').gsub("\n", ' ').split(' ') else [] end end |
#options ⇒ Object
125 126 127 |
# File 'lib/cane/cli/spec.rb', line 125 def @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!( + args) Translator.new(, DEFAULTS).to_hash rescue OptionsHandled nil end |
#parser ⇒ Object
129 130 131 |
# File 'lib/cane/cli/spec.rb', line 129 def parser @parser ||= OptionParser.new end |