Class: FoodCritic::CommandLine
- Inherits:
-
Object
- Object
- FoodCritic::CommandLine
- Defined in:
- lib/foodcritic/command_line.rb
Overview
Command line parsing.
Class Method Summary collapse
Instance Method Summary collapse
-
#cookbook_paths ⇒ Array<String>
The cookbook paths to check.
-
#environment_paths ⇒ Array<String>
The environment paths to check.
-
#help ⇒ String
The help text plus the end of life message.
-
#initialize(args) ⇒ CommandLine
constructor
Create a new instance of CommandLine.
-
#list_rules? ⇒ Boolean
Just list enabled rules, don’t actually run a lint check?.
-
#options ⇒ Hash
Parsed command-line options.
-
#original_options ⇒ Hash
The original command-line options.
-
#role_paths ⇒ Array<String>
The role paths to check.
-
#show_context? ⇒ Boolean
If matches should be shown with context rather than the default summary display.
-
#show_end_of_life_msg ⇒ String
The end of life message.
-
#show_help? ⇒ Boolean
Show the command help to the end user?.
-
#show_version? ⇒ Boolean
Show the current version to the end user?.
-
#valid_grammar? ⇒ Boolean
Is the search grammar specified valid?.
-
#valid_paths? ⇒ Boolean
If the paths provided are valid.
-
#version ⇒ String
The version string.
Constructor Details
#initialize(args) ⇒ CommandLine
Create a new instance of CommandLine
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/foodcritic/command_line.rb', line 15 def initialize(args) @args = args @original_args = args.dup = { fail_tags: ["~opensource"], tags: [], include_rules: [], cookbook_paths: [], role_paths: [], environment_paths: [], exclude_paths: ["test/**/*", "spec/**/*", "features/**/*"], progress: true, ast_cache_size: 5, } @parser = OptionParser.new do |opts| opts. = "foodcritic [cookbook_paths]" opts.on("-t", "--tags TAGS", "Check against (or exclude ~) rules with the "\ "specified tags.") do |t| [:tags] << t end opts.on("-l", "--list", "List all enabled rules and their descriptions.") do |t| [:list] = t end opts.on("-f", "--epic-fail TAGS", "Fail the build based on tags. Default of 'any' to fail "\ "on all warnings.") do |t| [:fail_tags] << t end opts.on("-c", "--chef-version VERSION", "Only check against rules valid for this version "\ "of Chef.") do |c| [:chef_version] = c end opts.on("-r", "--rule-file PATH", "Specify file with rules to be used or ignored ") do |c| [:rule_file] = c end opts.on("-s", "--ast-cache-size NUM", Integer, "Change the size of the AST cache.") do |s| [:ast_cache_size] = s end opts.on("-B", "--cookbook-path PATH", "Cookbook path(s) to check.") do |b| [:cookbook_paths] << b end opts.on("-C", "--[no-]context", "Show lines matched against rather than the "\ "default summary.") do |c| [:context] = c end opts.on("-E", "--environment-path PATH", "Environment path(s) to check.") do |e| [:environment_paths] << e end opts.on("-I", "--include PATH", "Additional rule file path(s) to load.") do |i| [:include_rules] << i end opts.on("-G", "--search-gems", "Search rubygems for rule files with the path "\ "foodcritic/rules/**/*.rb") do |g| [:search_gems] = true end opts.on("-P", "--[no-]progress", "Show progress of files being checked. default: true") do |q| [:progress] = q end opts.on("-R", "--role-path PATH", "Role path(s) to check.") do |r| [:role_paths] << r end opts.on("-S", "--search-grammar PATH", "Specify grammar to use when validating search syntax.") do |s| [:search_grammar] = s end opts.on("-V", "--version", "Display the foodcritic version.") do |v| [:version] = true end opts.on("-X", "--exclude PATH", "Exclude path(s) from being linted. PATH is relative to the cookbook, not an absolute PATH. Default test/**/*,spec/**/*,features/**/*") do |e| [:exclude_paths] << e end end # -v is not implemented but OptionParser gives the Foodcritic's version # if that flag is passed if args.include? "-v" help else begin @parser.parse!(args) unless show_help? rescue OptionParser::InvalidOption => e e.recover args end end end |
Class Method Details
.main(argv = ARGV, out = $stdout) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/foodcritic/command_line.rb', line 4 def self.main(argv = ARGV, out = $stdout) cmd_line = CommandLine.new(argv) review, status = Linter.run(cmd_line) printer = cmd_line.show_context? ? ContextOutput.new(out) : SummaryOutput.new(out) printer.output(review) status.to_i end |
Instance Method Details
#cookbook_paths ⇒ Array<String>
The cookbook paths to check
194 195 196 |
# File 'lib/foodcritic/command_line.rb', line 194 def cookbook_paths @args + Array([:cookbook_paths]) end |
#environment_paths ⇒ Array<String>
The environment paths to check
208 209 210 |
# File 'lib/foodcritic/command_line.rb', line 208 def environment_paths Array([:environment_paths]) end |
#help ⇒ String
The help text plus the end of life message.
144 145 146 |
# File 'lib/foodcritic/command_line.rb', line 144 def help @parser.help + show_end_of_life_msg end |
#list_rules? ⇒ Boolean
Just list enabled rules, don’t actually run a lint check?
158 159 160 |
# File 'lib/foodcritic/command_line.rb', line 158 def list_rules? .key?(:list) end |
#options ⇒ Hash
Parsed command-line options
223 224 225 226 227 228 229 230 231 |
# File 'lib/foodcritic/command_line.rb', line 223 def .merge( { cookbook_paths: cookbook_paths, role_paths: role_paths, environment_paths: environment_paths, } ) end |
#original_options ⇒ Hash
The original command-line options
236 237 238 |
# File 'lib/foodcritic/command_line.rb', line 236 def end |
#role_paths ⇒ Array<String>
The role paths to check
201 202 203 |
# File 'lib/foodcritic/command_line.rb', line 201 def role_paths Array([:role_paths]) end |
#show_context? ⇒ Boolean
If matches should be shown with context rather than the default summary display.
216 217 218 |
# File 'lib/foodcritic/command_line.rb', line 216 def show_context? [:context] end |
#show_end_of_life_msg ⇒ String
The end of life message.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/foodcritic/command_line.rb', line 117 def show_end_of_life_msg " ******************************************************************************* WARNING: Foodcritic will be end of life in April 2020 ******************************************************************************* Please use Cookstyle to ensure all of your Chef Infra code meets the latest best practices. Cookstyle is a code linting tool that helps you to write better Chef Infra Cookbooks by detecting and automatically correct cookbook code. For more information, use the command: cookstyle -h " end |
#show_help? ⇒ Boolean
Show the command help to the end user?
137 138 139 |
# File 'lib/foodcritic/command_line.rb', line 137 def show_help? @args.length == 1 && @args.first == "--help" end |
#show_version? ⇒ Boolean
Show the current version to the end user?
151 152 153 |
# File 'lib/foodcritic/command_line.rb', line 151 def show_version? .key?(:version) end |
#valid_grammar? ⇒ Boolean
Is the search grammar specified valid?
182 183 184 185 186 187 188 189 |
# File 'lib/foodcritic/command_line.rb', line 182 def valid_grammar? return true unless .key?(:search_grammar) return false unless File.exist?([:search_grammar]) search = FoodCritic::Chef::Search.new search.create_parser([[:search_grammar]]) search.parser? end |
#valid_paths? ⇒ Boolean
If the paths provided are valid
172 173 174 175 176 |
# File 'lib/foodcritic/command_line.rb', line 172 def valid_paths? paths = [:cookbook_paths] + [:role_paths] + [:environment_paths] paths.any? && paths.all? { |path| File.exist?(path) } end |
#version ⇒ String
The version string.
165 166 167 |
# File 'lib/foodcritic/command_line.rb', line 165 def version "foodcritic #{FoodCritic::VERSION}" end |