Class: Worked::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/worked/cli.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments, file) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/worked/cli.rb', line 7

def self.execute(stdout, arguments, file)

  if arguments.empty?
    parse_options(stdout, ["--help"], file)
  end

  begin

   try_to_parse(file, arguments.join(" "))

  rescue CannotParse

   options = parse_options(stdout, arguments, file)

   if options[:graph_path]

     graph(stdout, file, options[:graph_path])

   end
  end
end

.graph(stdout, file, path) ⇒ Object



34
35
36
37
38
39
# File 'lib/worked/cli.rb', line 34

def self.graph stdout, file, path

  Graph::create_weekly(Reader::by_week(file.readlines), path)

  stdout.puts "Graph saved to #{path}."
end

.parse_options(stdout, arguments, file) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/worked/cli.rb', line 41

def self.parse_options(stdout, arguments, file)

  options = {}

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      This application is wonderful!

      Usage: #{File.basename($0)} [options]
             #{File.basename($0)} work time specification

      Options are:
    BANNER
    opts.separator ""
    opts.on("-g", "--graph=PATH", String,
            "Prints a graph to the specified file.") { |arg|
              options[:graph_path] = arg
            }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }

    opts.parse!(arguments)
  end

  options
end

.try_to_parse(file, string) ⇒ Object



29
30
31
32
# File 'lib/worked/cli.rb', line 29

def self.try_to_parse file, string

  Recorder.new(file).record(string)
end