Class: Cane::CLI::Parser
- Inherits:
-
Object
- Object
- Cane::CLI::Parser
- Defined in:
- lib/cane/cli/parser.rb
Overview
Provides a specification for the command line interface that drives documentation, parsing, and default values.
Defined Under Namespace
Classes: OptionsHandled
Instance Attribute Summary collapse
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
- #add_banner ⇒ Object
- #add_cane_options ⇒ Object
- #add_check_options(check) ⇒ Object
- #add_checks_shortcut ⇒ Object
- #add_help ⇒ Object
- #add_option(option, description, opts = {}) ⇒ Object
- #add_user_defined_checks ⇒ Object
- #add_version ⇒ Object
- #get_default_options ⇒ Object
-
#initialize(stdout = $stdout) ⇒ Parser
constructor
A new instance of Parser.
- #options ⇒ Object
- #parse(args, ret = true) ⇒ Object
- #parser ⇒ Object
- #read_options_from_file(file) ⇒ Object
Constructor Details
#initialize(stdout = $stdout) ⇒ Parser
Returns a new instance of Parser.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cane/cli/parser.rb', line 22 def initialize(stdout = $stdout) @stdout = stdout add_user_defined_checks Cane.default_checks.each do |check| (check) end add_checks_shortcut add_version add_help end |
Instance Attribute Details
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
177 178 179 |
# File 'lib/cane/cli/parser.rb', line 177 def stdout @stdout end |
Class Method Details
.parse(*args) ⇒ Object
18 19 20 |
# File 'lib/cane/cli/parser.rb', line 18 def self.parse(*args) new.parse(*args) end |
Instance Method Details
#add_banner ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/cane/cli/parser.rb', line 63 def parser. = <<-BANNER Usage: cane [options] Default options are loaded from a .cane file in the current directory. BANNER end |
#add_cane_options ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cane/cli/parser.rb', line 110 def add_option %w(--max-violations VALUE), "Max allowed violations", default: 0, cast: :to_i add_option %w(--json), "Output as JSON", default: false add_option %w(--parallel), "Use all processors. Slower on small projects, faster on large.", cast: ->(x) { x } add_option %w(--color), "Colorize output", default: false parser.separator "" end |
#add_check_options(check) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/cane/cli/parser.rb', line 87 def (check) check..each do |key, data| cli_key = key.to_s.tr('_', '-') opts = data[1] || {} variable = opts[:variable] || "VALUE" defaults = opts[:default] || [] if opts[:type] == Array parser.on("--#{cli_key} #{variable}", Array, data[0]) do |opts| ([key.to_sym] ||= []) << opts end else if [*defaults].length > 0 add_option ["--#{cli_key}", variable], *data else add_option ["--#{cli_key}"], *data end end end parser.separator "" end |
#add_checks_shortcut ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cane/cli/parser.rb', line 127 def add_checks_shortcut description = "Apply all checks to given file" parser.on("-f", "--all FILE", description) do |f| # This is a bit of a hack, but provides a really useful UI for # dealing with single files. Let's see how it evolves. [:abc_glob] = f [:style_glob] = f [:doc_glob] = f end end |
#add_help ⇒ Object
145 146 147 148 149 150 |
# File 'lib/cane/cli/parser.rb', line 145 def add_help parser.on_tail("-h", "--help", "Show this message") do stdout.puts parser raise OptionsHandled end end |
#add_option(option, description, opts = {}) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cane/cli/parser.rb', line 152 def add_option(option, description, opts={}) option_key = option[0].gsub('--', '').tr('-', '_').to_sym default = opts[:default] cast = opts[:cast] || ->(x) { x } if default description += " (default: %s)" % default end parser.on(option.join(' '), description) do |v| [option_key] = cast.to_proc.call(v) .delete(opts[:clobber]) end end |
#add_user_defined_checks ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cane/cli/parser.rb', line 72 def add_user_defined_checks description = "Load a Ruby file containing user-defined checks" parser.on("-r", "--require FILE", description) do |f| load(f) end description = "Use the given user-defined check" parser.on("-c", "--check CLASS", description) do |c| check = Kernel.const_get(c) [:checks] << check (check) end parser.separator "" end |
#add_version ⇒ Object
138 139 140 141 142 143 |
# File 'lib/cane/cli/parser.rb', line 138 def add_version parser.on_tail("-v", "--version", "Show version") do stdout.puts Cane::VERSION raise OptionsHandled end end |
#get_default_options ⇒ Object
51 52 53 |
# File 'lib/cane/cli/parser.rb', line 51 def './.cane' end |
#options ⇒ Object
167 168 169 170 171 |
# File 'lib/cane/cli/parser.rb', line 167 def @options ||= { checks: Cane.default_checks } end |
#parse(args, ret = true) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cane/cli/parser.rb', line 39 def parse(args, ret = true) parser.parse!( + args) Cane::CLI..merge() rescue OptionParser::InvalidOption, OptionParser::AmbiguousOption args = %w(--help) ret = false retry rescue OptionsHandled ret end |
#parser ⇒ Object
173 174 175 |
# File 'lib/cane/cli/parser.rb', line 173 def parser @parser ||= OptionParser.new end |