Class: Sapling::CLI

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

Overview

CLI is the class for option parsing, and the gateway to the program, on the command line

Instance Method Summary collapse

Instance Method Details

#talk(options) ⇒ Object

Option parsing, and gateway to either reading and traversing a tree, or editing/creating a tree.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sapling.rb', line 21

def talk(options)
  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: sapling -t FILE\n" \
                 "Usage: sapling -e [FILE]"

    opt.on_tail("-h", "--help", "Show this menu") do
      puts opt
      exit
    end

    opt.on("-t", "--talk",
           "Begin traversing the provided dialogue tree") do

      if ARGV.empty?
        puts "No tree file provided. Please provide a tree file."
        puts opt_parser
        exit
      end

      unless verify_tree(ARGV[0])
        puts "\n#{opt}\n"
        exit
      end

      puts "Welcome to Sapling, a Dialogue Tree Utility.\n"
      speaker = Dialogue::Speaker.new
      speaker.file = YAML.load_file(ARGV[0])
      speaker.conversation
    end

    opt.on("-e", "--edit",
           "Create or edit a dialogue tree") do

      if ARGV.empty?
        puts "Creating a new tree."
        tree = SKELETON_TREE
      else
        puts "Using tree at #{ARGV[0]}."
        unless verify_tree(ARGV[0])
          puts "\n#{opt}\n"
          exit
        end
        tree = YAML.load_file(ARGV[0])
      end

      puts "Welcome to Sapling, a Dialogue Tree Utility.\n"
      gardner = Planter::Spade.new
      gardner.file = tree
      gardner.plant
    end

  end

  # Hacky way of dealing with bad options
  begin
    opt_parser.parse!(options)
  rescue OptionParser::InvalidOption
    puts "Invalid option."
    puts opt_parser
    exit
  end
end