Class: Texas::OptionParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ OptionParser

Returns a new instance of OptionParser.



10
11
12
13
# File 'lib/texas/option_parser.rb', line 10

def initialize(args)
  @args = args
  @options = OpenStruct.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/texas/option_parser.rb', line 8

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/texas/option_parser.rb', line 8

def options
  @options
end

Instance Method Details

#parseObject

Return a structure describing the options.



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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/texas/option_parser.rb', line 18

def parse
  # The options specified on the command line will be collected in *options*.
  # We set default values here.
  options.task = :build
  options.work_dir = find_work_dir
  options.check_mandatory_arguments = true
  options.load_local_libs = true
  options.contents_dir = Texas.contents_subdir_name
  options.contents_template = find_contents_file("contents")
  options.backtrace = false
  options.colors = true
  options.merge_config = nil
  options.verbose = false
  options.warnings = true
  options.open_pdf = true

  lookup_and_execute_require_option(args)
  
  opts = ::OptionParser.new do |opts|
    opts.banner = "Usage: texas [options]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-d", "--dry-run", "Run without pdf generation") do |contents_template|
      options.task = :dry
    end

    opts.on("-m", "--merge-config [CONFIG]",
            "Merge config with key from .texasrc") do |key|
      options.merge_config = key
    end

    opts.on("-n", "--new [NAME]",
            "Create new texas project directory") do |name|
      options.task = :new_project
      options.check_mandatory_arguments = false
      options.load_local_libs = false
      options.new_project_name = name
    end

    opts.on("-r", "--require [LIBRARY]", "Require library before running texas") do |lib|
      # this block does nothing
      # require was already performed by lookup_and_execute_require_option
      # this option is here to ensure the -r switch is listed in the help option
    end

    opts.on("--watch", "Watch the given template") do |contents_template|
      options.task = :watch
      options.open_pdf = false
    end

    parse_additional_options(opts)

    opts.separator ""
    opts.separator "Common options:"

    opts.on("--[no-]backtrace", "Switch backtrace") do |v|
      options.backtrace = v
    end

    opts.on("-c", "--[no-]color", "Switch colors") do |v|
      options.colors = v
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end

    opts.on("-w", "--[no-]warnings", "Switch warnings") do |v|
      options.warnings = v
    end

    # 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 Texas::VERSION::STRING
      exit
    end
  end

  opts.parse!(args)

  unless args.empty?
    f = args.shift
    options.contents_template = find_contents_file(f)
    options.contents_dir = find_contents_dir(f)
  end
  if options.check_mandatory_arguments
    check_mandatory! options
  end
  options
end