Class: Backsum::Cli
- Inherits:
-
Object
- Object
- Backsum::Cli
- Defined in:
- lib/backsum/cli.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #execute(argv = []) ⇒ Object
-
#initialize(*args) ⇒ Cli
constructor
A new instance of Cli.
- #option_parser!(argv) ⇒ Object
- #perform_by_dir ⇒ Object
- #perform_by_inputs ⇒ Object
- #show_usage ⇒ Object
- #show_version ⇒ Object
Constructor Details
#initialize(*args) ⇒ Cli
Returns a new instance of Cli.
12 13 14 15 |
# File 'lib/backsum/cli.rb', line 12 def initialize(*args) self. = { projects_path: "./projects" } self.action = nil end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
10 11 12 |
# File 'lib/backsum/cli.rb', line 10 def action @action end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/backsum/cli.rb', line 10 def @options end |
Instance Method Details
#execute(argv = []) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/backsum/cli.rb', line 17 def execute(argv = []) option_parser!(argv) Backsum.logger.level = :debug if ::Backsum.verbose if self.action send self.action else self.show_usage exit 1 end end |
#option_parser!(argv) ⇒ Object
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 |
# File 'lib/backsum/cli.rb', line 30 def option_parser!(argv) @option_parser ||= OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [options] action ..." opts.on("-V", "--version", "Display the Backsum version and exit.") do self.action = :show_version end opts.on("--all[=PATH]", "excute all the files. default: '#{self.[:projects_path]}'") do |path| self.[:projects_path] = path if path self.action = :perform_by_dir end opts.on("-v", "--[no-]verbose", "increase verbosity. default: #{Backsum.verbose}") do |value| Backsum.verbose = value end end @option_parser.parse!(argv) if !self.action && argv.any? self.action = :perform_by_inputs @input_files = argv end end |
#perform_by_dir ⇒ Object
65 66 67 68 |
# File 'lib/backsum/cli.rb', line 65 def perform_by_dir files = Dir[File.join(self.[:projects_path], '*.rb')] perform_files(files) end |
#perform_by_inputs ⇒ Object
70 71 72 |
# File 'lib/backsum/cli.rb', line 70 def perform_by_inputs perform_files(@input_files) end |
#show_usage ⇒ Object
56 57 58 59 |
# File 'lib/backsum/cli.rb', line 56 def show_usage stderr "Please specify one action to execute." stderr @option_parser.help end |