Class: Cron::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kanseishitsu/cron/argument_parser.rb

Overview

Define the ArgumentsParser class

Constant Summary collapse

FLAGS =
i[banner show_all list remove verbose version].freeze
POSITIONAL =
i[crontab].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  @options = {}
  FLAGS.each { |method_name| self.method(method_name).call }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 20

def options
  @options
end

#parserObject (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.options? && ARGV.length != 1
    message = 'A crontab definition is required'
    raise OptionParser::MissingArgument, message
  elsif !args.empty?
    arguments_parser.positional!(args)
  end
  arguments_parser.options
rescue OptionParser::AmbiguousOption => e
  abort e.message
rescue OptionParser::ParseError => e
  puts e.message
  arguments_parser.usage!
end

Instance Method Details



28
29
30
31
32
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 28

def banner
  @parser.banner = "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
  @options[:crontab] = crontab
  @options[:cron_schedule] =
    crontab.take(LaunchAgentManager::SCHEDULE_PARTS_COUNT).join(' ')
  @options[:executable_path_with_args] =
    crontab.drop(LaunchAgentManager::SCHEDULE_PARTS_COUNT).join(' ')
end

#demand(arg, positional: false) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


75
76
77
78
79
80
81
82
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 75

def demand(arg, positional: false)
  return @options[arg] unless @options[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

#listObject



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
    @options[:list] = true
  end
end

#options?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 91

def options?
  ArgumentsParser::FLAGS.any? { |flag| @options.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

#removeObject



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|
    @options[:remove] = label
  end
end

#show_allObject



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
    @options[: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

#verboseObject



52
53
54
55
56
57
# File 'lib/kanseishitsu/cron/argument_parser.rb', line 52

def verbose
  @options[:log_level] ||= Logger::INFO
  @parser.on_tail('-v', '--verbose', 'Increase verbosity') do
    @options[:log_level] -= 1
  end
end

#versionObject



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