lib/waddup/cli.rb in waddup-0.1.0 vs lib/waddup/cli.rb in waddup-0.2.0
- old
+ new
@@ -1,75 +1,102 @@
require 'chronic'
+require 'waddup'
+
module Waddup
class CLI
+ require 'commander/import'
- attr_accessor :sources, :from, :to
+ program :name, Waddup::NAME
+ program :version, Waddup::VERSION
+ program :description, Waddup::SUMMARY
KEYWORDS = {
sources: %w[with],
from: %w[from since],
to: %w[to until uptil upto through]
}
KEYWORD_BOUNDARY = "(?:\\s#{KEYWORDS.values.flatten.join('|\\s')}|\\Z)"
- def parse!
- parse_keyword :sources do |match|
- sources = match[1]
- @sources = Waddup::Source.usable.select do |source|
- sources.include? source::ALIAS
+ class << self
+
+ attr_accessor :sources, :from, :to
+
+ def parse!(arguments)
+ @arguments = arguments.join ' '
+
+ parse_keyword :sources do |match|
+ sources = match[1]
+ @sources = Waddup::Source.usable.select do |source|
+ sources.include? source::ALIAS
+ end
end
- end
- parse_keyword :from do |match|
- @from = Chronic.parse match[1], context: :past
+ parse_keyword :from do |match|
+ @from = Chronic.parse match[1], context: :past
+ end
+
+ parse_keyword :to do |match|
+ @to = Chronic.parse match[1], context: :past
+ end
end
- parse_keyword :to do |match|
- @to = Chronic.parse match[1], context: :past
+ def parse_keyword(keyword, &block)
+ @arguments.match /(?:#{KEYWORDS[keyword].join('|')})\s(.+?)#{KEYWORD_BOUNDARY}/i, &block
end
- end
- def parse_keyword(keyword, &block)
- @arguments.match /(?:#{KEYWORDS[keyword].join('|')})\s(.+?)#{KEYWORD_BOUNDARY}/i, &block
end
- # Parses given arguments, aggregates events and renders timesheet
- def run!(arguments)
- @arguments = arguments.join ' '
+ default_command :timesheet
- parse!
+ command :timesheet do |c|
+ c.syntax = 'waddup with git and mail since last monday 9:00 until yesterday morning'
+ c.description = 'Aggregates events and generates timesheet between from and to range'
+ c.option '--format visual | json', String, 'Provides timesheet in specified format'
+ c.action do |args, options|
+ parse! args
- # Sanity checking
- @sources ||= Waddup::Source.usable
- @from ||= Time.now
- @to ||= Time.now
+ # Sanity checking
+ @sources ||= Waddup::Source.usable
+ @from ||= Time.now
+ @to ||= Time.now
- # Aggregate events from all sources
- events = sources.map do |source|
- source.new.events from, to
- end
+ # Aggregate events from all sources
+ events = sources.map do |source|
+ source.new.events from, to
+ end
- # Sort events
- events.flatten!
- events.sort_by! &:at
+ # Sort events
+ events.flatten!
+ events.sort_by! &:at
- # Group daily
- days = events.group_by { |event| event.at.to_date }
+ # Group daily
+ days = events.group_by { |event| event.at.to_date }
- # Generate timesheet
- days.each_pair do |day, events|
- puts
- puts day.strftime('%A, %-d %B %Y')
- puts
- events.each do |event|
- puts " #{event.at.strftime('%H:%M')} #{event.source.class::ICON} #{event.label}"
+ # Generate timesheet in requested format
+ case options.format
+ when 'json'
+ require 'json'
+ puts JSON.generate days
+ when 'visual'
+ days.each_pair do |day, events|
+ puts
+ puts day.strftime('%A, %-d %B %Y')
+ puts
+
+ events.each do |event|
+ puts " #{event.at.strftime('%H:%M')} #{event.source.class::ICON} #{event.label}"
+ end
+ end
+
+ puts
+ else
+ raise 'Format needs to be either visual or json'
end
- end
- puts
+ end
end
end
end