lib/time_block/cli.rb in time_block-0.1.0 vs lib/time_block/cli.rb in time_block-0.2.0
- old
+ new
@@ -9,13 +9,15 @@
class CLI
class << self
class UnsupportedCommand < StandardError; end
def run
- parse_args
+ parse_args!
set_defaults
execute
+ rescue UnsupportedCommand => e
+ puts e.message
end
private
def execute
@@ -26,10 +28,11 @@
stop
start
end
def start
+ puts "Time #{@time}s ..."
Dante::Runner
.new('timeblock')
.execute(daemonize: true, pid_path: pid_path, log_path: log_path) do |_opts|
TimeBlock::Agent.new(@time).run
end
@@ -52,11 +55,11 @@
def set_defaults
@time ||= 60
@verbose ||= false
end
- def parse_args
+ def parse_args!
opts = GetoptLong.new(['--help', '-h', GetoptLong::NO_ARGUMENT],
['--time', '-t', GetoptLong::REQUIRED_ARGUMENT],
['--verbose', '-v', GetoptLong::NO_ARGUMENT])
@time = nil
@verboes = nil
@@ -65,10 +68,15 @@
when '--help'
print_help
exit(0)
when '--time'
@time = arg.to_i
+ if arg.chars.last == 'm'
+ @time *= 60
+ elsif arg.chars.last == 'h'
+ @time *= 60 * 24
+ end
when '--verbose'
@verbose = true
end
end
@@ -76,10 +84,10 @@
return if supported_commands?(@command)
raise UnsupportedCommand, "Unsupported command detected, #{@command}"
end
def supported_commands?(cmd)
- cmd.nil? || %w[stop start restart].include?(cmd)
+ %w[stop start restart].include?(cmd)
end
def print_help
puts <<~EOF
heartbeat [OPTION]