Class: Modname::Driver
Overview
Driver class handles command-line argument parsing and execution
Constant Summary collapse
- FLAGS =
boolean options toggled by a single flag with no argument
{ '-f' => :force, '-r' => :recurse, '--dirs' => :dirs }.freeze
- COMMANDS =
flags that select which command to run
{ '-e' => 'ext', '--ext' => 'ext', '-h' => 'help', '--help' => 'help', '-v' => 'version', '--version' => 'version' }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ Driver
constructor
A new instance of Driver.
-
#parse(args) ⇒ Object
parse out arguments.
-
#run(args) ⇒ Object
parse user arguments.
Methods included from Modder
classify, confirm?, execute, #exts, files, finish, parse, #regex, rename, rename_base, resolve_dir_renames, safe_stat, status, #undercase_ext, undercase_ext_get, undercase_ext_set
Constructor Details
#initialize ⇒ Driver
Returns a new instance of Driver.
23 24 25 26 |
# File 'lib/modname.rb', line 23 def initialize @options = { force: false, recurse: false, dirs: false } @transfer = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/modname.rb', line 21 def @options end |
Instance Method Details
#parse(args) ⇒ Object
parse out arguments
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/modname.rb', line 63 def parse(args) opts = { cmd: 'file', args: [] } args.each do |opt| if (flag = FLAGS[opt]) @options[flag] = true elsif (cmd = COMMANDS[opt]) opts[:cmd] = cmd else opts[:args] << opt # command argument end end opts end |
#run(args) ⇒ Object
parse user arguments
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/modname.rb', line 29 def run(args) if args.empty? puts Modname::HELP_BANNER else opts = parse args cmd = opts[:cmd] case cmd when 'file' regex opts[:args] when 'ext' exts opts[:args] when 'help' puts Modname::V_HELP_BANNER when 'version' puts Modname::VERSION end end end |