Class: Cron::ArgumentsParser
- Defined in:
- lib/kanseishitsu/cron/argument_parser.rb
Overview
Define the ArgumentsParser class
Constant Summary collapse
- FLAGS =
i[ show_all list remove verbose version].freeze
- POSITIONAL =
i[crontab].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
-
.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) ⇒ Object
rubocop: disable Metrics/MethodLength.
Instance Method Summary collapse
- #banner ⇒ Object
- #crontab(args) ⇒ Object
- #demand(arg, positional: false) ⇒ Object
-
#initialize(option_parser = OptionParser.new) ⇒ ArgumentsParser
constructor
A new instance of ArgumentsParser.
- #list ⇒ Object
- #options? ⇒ Boolean
- #positional!(args) ⇒ Object
- #remove ⇒ Object
- #show_all ⇒ Object
- #usage! ⇒ Object
- #verbose ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(option_parser = OptionParser.new) ⇒ ArgumentsParser
Returns a new instance of ArgumentsParser.
22 23 24 25 26 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 22 def initialize(option_parser = OptionParser.new) @parser = option_parser = {} FLAGS.each { |method_name| self.method(method_name).call } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 20 def end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
20 21 22 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 20 def parser @parser end |
Class Method Details
.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) ⇒ Object
rubocop: disable Metrics/MethodLength
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 101 def self.parse(args = ARGV, _file_path = ARGF, arguments_parser = ArgumentsParser.new) arguments_parser.parser.parse!(args) if !arguments_parser. && ARGV.length != 1 = 'A crontab definition is required' raise OptionParser::MissingArgument, elsif !args.empty? arguments_parser.positional!(args) end arguments_parser. rescue OptionParser::AmbiguousOption => e abort e. rescue OptionParser::ParseError => e puts e. arguments_parser.usage! end |
Instance Method Details
#banner ⇒ Object
28 29 30 31 32 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 28 def @parser. = "Usage: #{File.basename($PROGRAM_NAME)} <crontab>|<options>" @parser.separator '' @parser.separator 'Options:' end |
#crontab(args) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 66 def crontab(args) crontab = args.shift.gsub(/\A['"]|['"]\z/, '').split [:crontab] = crontab [:cron_schedule] = crontab.take(LaunchAgentManager::SCHEDULE_PARTS_COUNT).join(' ') [:executable_path_with_args] = crontab.drop(LaunchAgentManager::SCHEDULE_PARTS_COUNT).join(' ') end |
#demand(arg, positional: false) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 75 def demand(arg, positional: false) return [arg] unless [arg].nil? required_arg = if positional then "<#{arg}>" else "--#{arg.to_s.gsub(UNDERSCORE_PATTERN, HYPHEN_STRING)}" end raise OptionParser::MissingArgument, "Required argument: #{required_arg}" end |
#list ⇒ Object
40 41 42 43 44 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 40 def list @parser.on('-l', '--list', 'List cron jobs labels') do [:list] = true end end |
#options? ⇒ Boolean
91 92 93 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 91 def ArgumentsParser::FLAGS.any? { |flag| .include?(flag) } end |
#positional!(args) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 84 def positional!(args) POSITIONAL.each do |opt| self.method(opt).call(args) self.demand(opt, positional: true) end end |
#remove ⇒ Object
46 47 48 49 50 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 46 def remove @parser.on('-r', '--remove=<label>', 'Remove a cron job by label') do |label| [:remove] = label end end |
#show_all ⇒ Object
34 35 36 37 38 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 34 def show_all @parser.on('--show-all', 'Show cron jobs') do [:show_all] = true end end |
#usage! ⇒ Object
95 96 97 98 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 95 def usage! puts @parser exit end |
#verbose ⇒ Object
52 53 54 55 56 57 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 52 def verbose [:log_level] ||= Logger::INFO @parser.on_tail('-v', '--verbose', 'Increase verbosity') do [:log_level] -= 1 end end |
#version ⇒ Object
59 60 61 62 63 64 |
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 59 def version @parser.on_tail('--version', 'Show version') do puts "#{File.basename($PROGRAM_NAME)} version #{Kanseishitsu::VERSION}" exit end end |