Class: WLang::WLangCommand::Options

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

Overview

Options of the wlang commandline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptions

Initializes the options with default values



38
39
40
41
# File 'lib/wlang/wlang_command_options.rb', line 38

def initialize
  @verbosity = 0
  @template_brace = :braces
end

Instance Attribute Details

#context_fileObject (readonly)

Context file



20
21
22
# File 'lib/wlang/wlang_command_options.rb', line 20

def context_file
  @context_file
end

#context_kindObject (readonly)

Context kind [:yaml, :ruby, :dsl]



26
27
28
# File 'lib/wlang/wlang_command_options.rb', line 26

def context_kind
  @context_kind
end

#context_nameObject (readonly)

Name of the context



29
30
31
# File 'lib/wlang/wlang_command_options.rb', line 29

def context_name
  @context_name
end

#context_objectObject (readonly)

Context object



23
24
25
# File 'lib/wlang/wlang_command_options.rb', line 23

def context_object
  @context_object
end

#output_fileObject (readonly)

Output file



32
33
34
# File 'lib/wlang/wlang_command_options.rb', line 32

def output_file
  @output_file
end

#template_braceObject (readonly)

Which brace kind used in template (:braces, :brackes, :parentheses)



17
18
19
# File 'lib/wlang/wlang_command_options.rb', line 17

def template_brace
  @template_brace
end

#template_dialectObject (readonly)

Template dialect



14
15
16
# File 'lib/wlang/wlang_command_options.rb', line 14

def template_dialect
  @template_dialect
end

#template_fileObject (readonly)

Source template file



11
12
13
# File 'lib/wlang/wlang_command_options.rb', line 11

def template_file
  @template_file
end

#verbosityObject (readonly)

Verbose mode?



35
36
37
# File 'lib/wlang/wlang_command_options.rb', line 35

def verbosity
  @verbosity
end

Instance Method Details

#parse(argv) ⇒ Object

Parses commandline options provided as an array of Strings.



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/wlang/wlang_command_options.rb', line 46

def parse(argv)
  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = WLang::VERSION
    opt.release = nil
    opt.summary_indent = ' ' * 4
    banner = <<-EOF
      # Usage #{opt.program_name} [options] template context-file
  
      # Template is parsed as a wlang dialect (based on its extension by default) and 
      # instantiated through a given context file.
    EOF
    opt.banner = banner.gsub(/[ \t]+# /, "")

    opt.separator nil
    opt.separator "Options:"

    opt.on("-d", "--dialect=DIALECT", 
           "Interpret source template as a given wlang dialect") do |value|
      @template_dialect = value
    end

    opt.on("-m", "--methodize", 
           "Use methodization of hashes?") do |value|
      Kernel.load('wlang/ext/hash_methodize.rb')
    end

    opt.on("-o", "--output=OUTPUT",
           "Flush instantiation result in output file") do |value|
      @output_file = value         
    end

    opt.on("--brace=BRACE", ["brace", "parenthesis", "bracket"],
           "Block delimiters used by the template file (braces, brackets, parentheses)") do |value|
       # handle template brace
       case value
         when "brace", "braces", "{"
           @template_brace = :braces
         when "brackets", "bracket", "["
           @template_brace = :brackets
         when "parentheses", "parenthesis", "("
           @template_brace = :parentheses
         else
           raise "Unknown brace kind #{brace}"
       end
    end

    opt.on("--context-name=NAME",
           "Name of the context object") do |value|
      @context_name = value         
    end

    opt.on("--context-kind=KIND", ["yaml", "ruby", "dsl"],
           "Kind of context object (yaml, ruby, dsl)") do |value|
      @context_kind = '.' + value         
    end

    opt.on("--verbose", "-v",
           "Display extra progress as we parse.") do |value|
      @verbosity = 2
    end

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

    # Another typical switch to print the version.
    opt.on_tail("--version", "Show version") do
      puts "wlang version " << WLang::VERSION << " (c) University of Louvain, Bernard & Louis Lambeau"
      exit
    end

    opt.separator nil
  end   
    
  # handle arguments    
  rest = opts.parse!(argv)
  if rest.length<1 or rest.length>2
    puts opts
    exit
  end
    
  # handle template file
  @template_file = File.expand_path(rest[0])
  raise("File '#{@template_file}' not readable")\
    unless File.file?(@template_file) and File.readable?(@template_file)

  # handle context file
  if rest.length==2
    @context_file = rest[1]
    raise("File '#{@context_file}' not readable")\
      unless File.file?(@context_file) and File.readable?(@context_file)
  end
    
  # handle template dialect
  unless @template_dialect
    extname = File.extname(@template_file)
    @template_dialect = WLang::FILE_EXTENSIONS[extname]
    raise("No known dialect for file extension '#{extname}'\n"\
          "Known extensions are: " << WLang::FILE_EXTENSIONS.keys.join(", "))\
      if @template_dialect.nil?
  end
    
  # handle context object
  if @context_file
    @context_object = WLang::load_data(@context_file, @context_kind)
  end
  
  return self
end