Class: Diary::CLI

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/diary/cli.rb', line 5

def self.parse(args)
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  options = OpenStruct.new
  options.verbose = false
  options.force = false

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: diary ACTION [-vf]"

    opts.separator "\nSpecific options:"
    
    # Verbose
    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end
    
    # Force
    opts.on("-f", "--[no-]force", "Force all items to compile") do |f|
      options.force = f
    end

    opts.separator "\nCommon options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("--version", "Show version") do
      puts "diary #{Diary.version}"
      exit
    end
  end

  opts.parse!(args)
  options
end