Class: Watson::Command
- Inherits:
-
Object
- Object
- Watson::Command
- Extended by:
- Watson
- Defined in:
- lib/watson/command.rb
Overview
Command line parser class Controls program flow and parses options given by command line
Constant Summary collapse
- DEBUG =
Debug printing for this class
false
Constants included from Watson
BLUE, BOLD, CYAN, GLOBAL_DEBUG_OFF, GLOBAL_DEBUG_ON, GRAY, GREEN, MAGENTA, RED, RESET, UNDERLINE, VERSION, WHITE, YELLOW
Class Method Summary collapse
-
.execute(*args) ⇒ Object
Command line controller Manages program flow from given command line arguments.
-
.help ⇒ Object
Print help for watson.
-
.set_context(args) ⇒ Object
set_context Set context_depth parameter in config.
-
.set_dirs(args) ⇒ Object
set_dirs Set directories to be parsed by watson.
-
.set_files(args) ⇒ Object
set_files Set files to be parsed by watson.
-
.set_ignores(args) ⇒ Object
set_ignores Set files and dirs to be ignored when parsing by watson.
-
.set_output_format(args) ⇒ Object
set_output_format Set format watson should output in.
-
.set_parse_depth(args) ⇒ Object
set_parse_depth Set how deep to recursively parse directories.
-
.set_show_type(args) ⇒ Object
set_show Set what files watson should show.
-
.set_tags(args) ⇒ Object
set_tags Set tags to look for when parsing files and folders.
-
.setup_remote(args) ⇒ Object
setup_remote Handle setup of remote issue posting for GitHub and Bitbucket.
-
.version ⇒ Object
Print version information about watson.
Methods included from Watson
Class Method Details
.execute(*args) ⇒ Object
Command line controller Manages program flow from given command line arguments
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/watson/command.rb', line 18 def execute(*args) # [review] - Should command line args append or overwrite config/RC parameters? # Currently we overwrite, maybe add flag to overwrite or not? # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # List of possible flags, used later in parsing and for user reference _flag_list = %w[ -c --context-depth -d --dirs -f --files -h --help -i --ignore -p --parse-depth -r --remote -s --show -t --tags --format -u --update -v --version ] # If we get the version or help flag, ignore all other flags # Just display these and exit # Using .index instead of .include? to stay consistent with other checks return help if args.index('-h') != nil || args.index('--help') != nil return version if args.index('-v') != nil || args.index('--version') != nil # If not one of the above then we are performing actual watson stuff # Create all the necessary watson components so we can perform # all actions associated with command line args # Only create Config, don't call run method # Populate Config parameters from CL THEN call run to fill in gaps from RC # Saves some messy checking and sorting/organizing of parameters @config = Watson::Config.new @parser = Watson::Parser.new(@config) @printer = Watson::Printer.new(@config) # Capture Ctrl+C interrupt for clean exit # [review] - Not sure this is the correct place to put the Ctrl+C capture trap("INT") do File.delete(@config.tmp_file) if File.exists?(@config.tmp_file) exit 2 end # Parse command line options # Begin by slicing off until we reach a valid flag # Always look at first array element in case and then slice off what we need # Accept parameters to be added / overwritten if called twice # Slice out from argument until next argument # Clean up argument list by removing elements until the first valid flag until _flag_list.include?(args[0]) || args.length == 0 # [review] - Make this non-debug print to user? debug_print "Unrecognized flag #{ args[0] }\n" args.slice!(0) end # Parse command line options # Grab flag (should be first arg) then slice off args until next flag # Repeat until all args have been dealt with until args.length == 0 # Set flag for calling function later _flag = args.slice!(0) debug_print "Current Flag: #{ _flag }\n" # Go through args until we find the next valid flag or all args are parsed _i = 0 until _flag_list.include?(args[_i]) || _i > (args.length - 1) debug_print "Arg: #{ args[_i] }\n" _i = _i + 1 end # Slice off the args for the flag (inclusive) using index from above # [review] - This is a bit messy (to slice by _i - 1) when we have control # over the _i index above but I don't want to # think about the logic right now so look at later _flag_args = args.slice!(0..(_i-1)) case _flag when "-c", "--context-depth" debug_print "Found -c/--context-depth argument\n" set_context(_flag_args) when "-d", "--dirs" debug_print "Found -d/--dirs argument\n" set_dirs(_flag_args) when "-f", "--files" debug_print "Found -f/--files argument\n" set_files(_flag_args) when "-i", "--ignore" debug_print "Found -i/--ignore argument\n" set_ignores(_flag_args) when '--format' debug_print "Found --format argument\n" set_output_format(_flag_args) when "-p", "--parse-depth" debug_print "Found -r/--parse-depth argument\n" set_parse_depth(_flag_args) when "-r", "--remote" debug_print "Found -r/--remote argument\n" # Run config to populate all the fields and such # [review] - Not a fan of running these here but want to avoid getting all issues when running remote (which @config.run does) @config.check_conf @config.read_conf setup_remote(_flag_args) # If setting up remote, exit afterwards exit true when "-s", "--show" debug_print "Found -s/--show argument\n" set_show_type(_flag_args) when "-t", "--tags" debug_print "Found -t/--tags argument\n" (_flag_args) when "-u", "--update" debug_print "Found -u/--update argument\n" @config.remote_valid = true else print "Unknown argument #{ _flag }\n" end end debug_print "Args length 0, running watson...\n" @config.run structure = @parser.run @printer.run(structure) end |
.help ⇒ Object
Print help for watson
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/watson/command.rb', line 169 def help # [todo] - Add bold and colored printing # Identify method entry debug_print "#{ self } : #{ __method__ }\n" puts " \#{BOLD}Usage: watson [OPTION]...\n Running watson with no arguments will parse with settings in RC file\n If no RC file exists, default RC file will be created\n\n -c, --context-depth number of lines of context to provide with posted issue\n -d, --dirs list of directories to search in\n -f, --files list of files to search in\n --format set output format for watson [print, json, unite, silent]\n -h, --help print help\n -i, --ignore list of files, directories, or types to ignore\n -p, --parse-depth depth to recursively parse directories\n -r, --remote list / create tokens for Bitbucket/GitHub\n -t, --tags list of tags to search for\n -u, --update update remote repos with current issues\n -v, --version print watson version and info\n\n Any number of files, tags, dirs, and ignores can be listed after flag\n Ignored files should be space separated\n To use *.filetype identifier, encapsulate in \\\"\\\" to avoid shell substitutions\n\n Report bugs to: watson\\@goosecode.com\n watson home page: <http://goosecode.com/projects/watson>\n [goosecode] labs | 2012-2013\#{RESET}\n HELP\n\n return true\nend\n".gsub(/^ {,6}/, '') |
.set_context(args) ⇒ Object
set_context Set context_depth parameter in config
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/watson/command.rb', line 226 def set_context(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one dir in args if args.length <= 0 # [review] - Make this a non-debug print to user? debug_print "No args passed, exiting\n" return false end # For context_depth we do NOT append to RC, ALWAYS overwrite # For each argument passed, make sure valid, then set @config.parse_depth args.each do | _context_depth | if _context_depth.match(/^(\d+)/) debug_print "Setting #{ _context_depth } to config context_depth\n" @config.context_depth = _context_depth.to_i else debug_print "#{ _context_depth } invalid depth, ignoring\n" end end # Doesn't make much sense to set context_depth for each individual post # When you use this command line arg, it writes the config parameter @config.update_conf("context_depth") debug_print "Updated context_depth: #{ @config.context_depth }\n" return true end |
.set_dirs(args) ⇒ Object
set_dirs Set directories to be parsed by watson
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/watson/command.rb', line 262 def set_dirs(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one dir in args if args.length <= 0 # [review] - Make this a non-debug print to user? debug_print "No args passed, exiting\n" return false end # Set config flag for CL entryset in config @config.cl_entry_set = true debug_print "Updated cl_entry_set flag: #{ @config.cl_entry_set }\n" # [review] - Should we clean the dir before adding here? # For each argument passed, make sure valid, then add to @config.dir_list args.each do | _dir | # Error check on input if !Watson::FS.check_dir(_dir) print "Unable to open #{ _dir }\n" else # Clean up path, remove trailing forward slashes _dir = _dir.gsub(/(\/)+$/, "") if !_dir.empty? debug_print "Adding #{ _dir } to config dir_list\n" @config.dir_list.push(_dir) end end end debug_print "Updated dirs: #{ @config.dir_list }\n" return true end |
.set_files(args) ⇒ Object
set_files Set files to be parsed by watson
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/watson/command.rb', line 302 def set_files(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one file in args if args.length <= 0 debug_print "No args passed, exiting\n" return false end # Set config flag for CL entryset in config @config.cl_entry_set = true debug_print "Updated cl_entry_set flag: #{ @config.cl_entry_set }\n" # For each argument passed, make sure valid, then add to @config.file_list args.each do | _file | # Error check on input if !Watson::FS.check_file(_file) print "Unable to open #{ _file }\n" else debug_print "Adding #{ _file } to config file_list\n" @config.file_list.push(_file) end end debug_print "Updated files: #{ @config.file_list }\n" return true end |
.set_ignores(args) ⇒ Object
set_ignores Set files and dirs to be ignored when parsing by watson
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/watson/command.rb', line 336 def set_ignores(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one ignore in args if args.length <= 0 debug_print "No args passed, exiting\n" return false end # Set config flag for CL ignore set in config @config.cl_ignore_set = true debug_print "Updated cl_ignore_set flag: #{ @config.cl_ignore_set }\n" # For ignores we do NOT overwrite RC, just append # For each argument passed, add to @config.ignore_list args.each do | _ignore | debug_print "Adding #{ _ignore } to config ignore_list\n" @config.ignore_list.push(_ignore) end debug_print "Updated ignores: #{ @config.ignore_list }\n" return true end |
.set_output_format(args) ⇒ Object
set_output_format Set format watson should output in
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/watson/command.rb', line 484 def set_output_format(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one file in args unless args.length == 1 debug_print "Invalid argument passed\n" return false end @config.output_format = case args.pop.to_s when 'j', 'json' Watson::Formatters::JsonFormatter when 'unite' Watson::Formatters::UniteFormatter when 'silent' Watson::Formatters::SilentFormatter else Watson::Formatters::DefaultFormatter end debug_print "Updated output_format to: #{@config.output_format}\n" end |
.set_parse_depth(args) ⇒ Object
set_parse_depth Set how deep to recursively parse directories
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/watson/command.rb', line 367 def set_parse_depth(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # This should be a single, numeric, value # If they pass more, just take the last valid value if args.length <= 0 debug_print "No args passed, exiting\n" return false end # For max_dpeth we do NOT append to RC, ALWAYS overwrite # For each argument passed, make sure valid, then set @config.parse_depth args.each do | _parse_depth | if _parse_depth.match(/^(\d+)/) debug_print "Setting #{ _parse_depth } to config parse_depth\n" @config.parse_depth = _parse_depth else debug_print "#{ _parse_depth } invalid depth, ignoring\n" end end debug_print "Updated parse_depth: #{ @config.parse_depth }\n" return true end |
.set_show_type(args) ⇒ Object
set_show Set what files watson should show
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/watson/command.rb', line 511 def set_show_type(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # This should be a single value, either all, clean, or dirty # If they pass more, just take the last valid value if args.length <= 0 debug_print "No args passed, exiting\n" return false end args.each do | _show | case _show.downcase when 'clean' debug_print "Setting config show to #{ _show }\n" @config.show_type = 'clean' when 'dirty' debug_print "Setting config show to #{ _show }\n" @config.show_type = 'dirty' else debug_print "Setting config show to #{ _show }\n" @config.show_type = 'all' end end debug_print "Updated show to: #{ @config.show_type }\n" return true end |
.set_tags(args) ⇒ Object
set_tags Set tags to look for when parsing files and folders
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/watson/command.rb', line 398 def (args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" # Need at least one tag in args if args.length <= 0 debug_print "No args passed, exiting\n" return false end # Set config flag for CL tag set in config @config.cl_tag_set = true debug_print "Updated cl_tag_set flag: #{ @config.cl_tag_set }\n" # If set from CL, we overwrite the RC parameters # For each argument passed, add to @config.tag_list args.each do | _tag | debug_print "Adding #{ _tag } to config tag_list\n" @config.tag_list.push(_tag) end debug_print "Updated tags: #{ @config.tag_list }\n" return true end |
.setup_remote(args) ⇒ Object
setup_remote Handle setup of remote issue posting for GitHub and Bitbucket
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/watson/command.rb', line 428 def setup_remote(args) # Identify method entry debug_print "#{ self } : #{ __method__ }\n" formatter = Printer.new(@config).build_formatter formatter.print_header print BOLD + "Existing Remotes:\n" + RESET # Check the config for any remote entries (GitHub or Bitbucket) and print # We *should* always have a repo + API together, but API should be enough if @config.github_api.empty? && @config.bitbucket_api.empty? formatter.print_status "!", YELLOW print BOLD + "No remotes currently exist\n\n" + RESET end if !@config.github_api.empty? print BOLD + "GitHub User : " + RESET + "#{ @config.github_api }\n" print BOLD + "GitHub Repo : " + RESET + "#{ @config.github_repo }\n\n" end if !@config.bitbucket_api.empty? print BOLD + "Bitbucket User : " + RESET + "#{ @config.bitbucket_api }\n" + RESET print BOLD + "Bitbucket Repo : " + RESET + "#{ @config.bitbucket_repo }\n\n" + RESET end # If github or bitbucket passed, setup # If just -r (0 args) do nothing and only have above printed # If more than 1 arg is passed, unrecognized, warn user if args.length == 1 case args[0].downcase when "github" debug_print "GitHub setup called from CL\n" Watson::Remote::GitHub.setup(@config) when "bitbucket" debug_print "Bitbucket setup called from CL\n" Watson::Remote::Bitbucket.setup(@config) end elsif args.length > 1 formatter.print_status "x", RED puts " \#{BOLD}Incorrect arguments passed\#{RESET}\n Please specify either Github or Bitbucket to setup remote\n Or pass without argument to see current remotes\n See help (-h/--help) for more details\n SUMMERY\n\n return false\n end\nend\n".gsub(/^ {,8}/, '') |
.version ⇒ Object
Print version information about watson
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/watson/command.rb', line 207 def version # Identify method entry debug_print "#{ self } : #{ __method__ }\n" puts " watson v\#{::Watson::VERSION}\n Copyright (c) 2012-2013 goosecode labs\n Licensed under MIT, see LICENSE for details\n\n Written by nhmood, see <http://goosecode.com/projects/watson>\n VERSION\n\n return true\nend\n".gsub(/^ {,6}/, '') |