sweet-lang 0.1.9 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/filesystemwatcher.rb +330 -0
- data/lib/lexer/tags.rb +6 -2
- data/lib/servicestate.rb +75 -0
- data/lib/sweet.rb +82 -14
- data/lib/trollop.rb +782 -0
- metadata +4 -1
@@ -0,0 +1,330 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "thread"
|
3
|
+
require "servicestate"
|
4
|
+
|
5
|
+
# This class will watch a directory or a set of directories and alert you of
|
6
|
+
# new files, modified files, deleted files. You can optionally only be alerted
|
7
|
+
# when a files md5 hash has been changed so you only are alerted to real changes.
|
8
|
+
# this of course means slower performance and higher cpu/io usage.
|
9
|
+
class FileSystemWatcher
|
10
|
+
include ServiceState
|
11
|
+
|
12
|
+
CREATED = 0
|
13
|
+
MODIFIED = 1
|
14
|
+
DELETED = 2
|
15
|
+
|
16
|
+
# the time to wait before checking the directories again
|
17
|
+
attr_accessor :sleepTime, :priority
|
18
|
+
|
19
|
+
# you can optionally use the file contents md5 to detect if a file has changed
|
20
|
+
attr_accessor :useMD5
|
21
|
+
|
22
|
+
def initialize(dir=nil, expression="**/*")
|
23
|
+
@sleepTime = 5
|
24
|
+
@useMD5 = false
|
25
|
+
@priority = 0
|
26
|
+
@stopWhen = nil
|
27
|
+
|
28
|
+
@directories = Array.new()
|
29
|
+
@files = Array.new()
|
30
|
+
|
31
|
+
@foundFiles = nil
|
32
|
+
@firstLoad = true
|
33
|
+
@watchThread = nil
|
34
|
+
|
35
|
+
initializeState()
|
36
|
+
|
37
|
+
if dir then
|
38
|
+
addDirectory(dir, expression)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# add a directory to be watched
|
43
|
+
# @param dir the directory to watch
|
44
|
+
# @param expression the glob pattern to search under the watched directory
|
45
|
+
def addDirectory(dir, expression="**/*")
|
46
|
+
if FileTest.exists?(dir) && FileTest.readable?(dir) then
|
47
|
+
@directories << FSWatcher::Directory.new(dir, expression)
|
48
|
+
else
|
49
|
+
raise FSWatcher::InvalidDirectoryError, "Dir '#{dir}' either doesnt exist or isnt readable"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def removeDirectory(dir)
|
54
|
+
@directories.delete(dir)
|
55
|
+
end
|
56
|
+
|
57
|
+
# add a specific file to the watch list
|
58
|
+
# @param file the file to watch
|
59
|
+
def addFile(file)
|
60
|
+
if FileTest.exists?(file) && FileTest.readable?(file) then
|
61
|
+
@files << file
|
62
|
+
else
|
63
|
+
raise FSWatcher::InvalidFileError, "File '#{file}' either doesnt exist or isnt readable"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def removeFile(file)
|
68
|
+
@files.delete(file)
|
69
|
+
end
|
70
|
+
|
71
|
+
# start watching the specified files/directories
|
72
|
+
def start(&block)
|
73
|
+
if isStarted? then
|
74
|
+
raise RuntimeError, "already started"
|
75
|
+
end
|
76
|
+
|
77
|
+
setState(STARTED)
|
78
|
+
|
79
|
+
@firstLoad = true
|
80
|
+
@foundFiles = Hash.new()
|
81
|
+
|
82
|
+
# we watch in a new thread
|
83
|
+
@watchThread = Thread.new {
|
84
|
+
# we will be stopped if someone calls stop or if someone set a stopWhen that becomes true
|
85
|
+
while !isStopped? do
|
86
|
+
if (!@directories.empty?) or (!@files.empty?) then
|
87
|
+
# this will hold the list of the files we looked at this iteration
|
88
|
+
# allows us to not look at the same file again and also to compare
|
89
|
+
# with the foundFile list to see if something was deleted
|
90
|
+
alreadyExamined = Hash.new()
|
91
|
+
|
92
|
+
# check the files in each watched directory
|
93
|
+
if not @directories.empty? then
|
94
|
+
@directories.each { |dirObj|
|
95
|
+
examineFileList(dirObj.getFiles(), alreadyExamined, &block)
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
# now examine any files the user wants to specifically watch
|
100
|
+
examineFileList(@files, alreadyExamined, &block) if not @files.empty?
|
101
|
+
|
102
|
+
# see if we have to delete files from our found list
|
103
|
+
if not @firstLoad then
|
104
|
+
if not @foundFiles.empty? then
|
105
|
+
# now diff the found files and the examined files to see if
|
106
|
+
# something has been deleted
|
107
|
+
allFoundFiles = @foundFiles.keys()
|
108
|
+
allExaminedFiles = alreadyExamined.keys()
|
109
|
+
intersection = allFoundFiles - allExaminedFiles
|
110
|
+
intersection.each { |fileName|
|
111
|
+
# callback
|
112
|
+
block.call(DELETED, fileName)
|
113
|
+
# remove deleted file from the foundFiles list
|
114
|
+
@foundFiles.delete(fileName)
|
115
|
+
}
|
116
|
+
end
|
117
|
+
else
|
118
|
+
@firstLoad = false
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# go to sleep
|
123
|
+
sleep(@sleepTime)
|
124
|
+
end
|
125
|
+
}
|
126
|
+
|
127
|
+
# set the watch thread priority
|
128
|
+
@watchThread.priority = @priority
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
# kill the filewatcher thread
|
133
|
+
def stop()
|
134
|
+
setState(STOPPED)
|
135
|
+
@watchThread.wakeup()
|
136
|
+
end
|
137
|
+
|
138
|
+
# wait for the filewatcher to finish
|
139
|
+
def join()
|
140
|
+
@watchThread.join() if @watchThread
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
# loops over the file list check for new or modified files
|
147
|
+
def examineFileList(fileList, alreadyExamined, &block)
|
148
|
+
fileList.each { |fileName|
|
149
|
+
# expand the file name to the fully qual path
|
150
|
+
fullFileName = File.expand_path(fileName)
|
151
|
+
|
152
|
+
# dont examine the same file 2 times
|
153
|
+
if not alreadyExamined.has_key?(fullFileName) then
|
154
|
+
# we cant do much if the file isnt readable anyway
|
155
|
+
if File.readable?(fullFileName) then
|
156
|
+
# set that we have seen this file
|
157
|
+
alreadyExamined[fullFileName] = true
|
158
|
+
|
159
|
+
# get the file info
|
160
|
+
modTime, size = File.mtime(fullFileName), File.size(fullFileName)
|
161
|
+
|
162
|
+
# on the first iteration just load all of the files into the foundList
|
163
|
+
if @firstLoad then
|
164
|
+
@foundFiles[fullFileName] = FSWatcher::FoundFile.new(fullFileName, modTime, size, false, @useMD5)
|
165
|
+
else
|
166
|
+
# see if we have found this file already
|
167
|
+
foundFile = @foundFiles[fullFileName]
|
168
|
+
|
169
|
+
if foundFile then
|
170
|
+
|
171
|
+
# if a file is marked as new, we still need to make sure it isnt still
|
172
|
+
# being written to. we do this by checking the file sizes.
|
173
|
+
if foundFile.isNew? then
|
174
|
+
|
175
|
+
# if the file size is the same then it is probably done being written to
|
176
|
+
# unless the writer is really slow
|
177
|
+
if size == foundFile.size then
|
178
|
+
|
179
|
+
# callback
|
180
|
+
block.call(CREATED, fullFileName)
|
181
|
+
|
182
|
+
# mark this file as a changed file now
|
183
|
+
foundFile.updateModTime(modTime)
|
184
|
+
|
185
|
+
# generate the md5 for the file since we know it is done
|
186
|
+
# being written to
|
187
|
+
foundFile.genMD5() if @useMD5
|
188
|
+
|
189
|
+
else
|
190
|
+
|
191
|
+
# just update the size so we can check again at the next iteration
|
192
|
+
foundFile.updateSize(size)
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
elsif modTime > foundFile.modTime then
|
197
|
+
|
198
|
+
# if the mod times are different on files we already have
|
199
|
+
# found this is an update
|
200
|
+
willYield = true
|
201
|
+
|
202
|
+
# if we are using md5's then compare them
|
203
|
+
if @useMD5 then
|
204
|
+
filesMD5 = FSWatcher.genFileMD5(fullFileName)
|
205
|
+
if filesMD5 && foundFile.md5 then
|
206
|
+
if filesMD5.to_s == foundFile.md5.to_s then
|
207
|
+
willYield = false
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# if we are yielding then the md5s are dif so
|
212
|
+
# update the cached md5 value
|
213
|
+
foundFile.setMD5(filesMD5) if willYield
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
block.call(MODIFIED, fullFileName) if willYield
|
218
|
+
foundFile.updateModTime(modTime)
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
else
|
223
|
+
|
224
|
+
# this is a new file for our list. dont update the md5 here since
|
225
|
+
# the file might not yet be done being written to
|
226
|
+
@foundFiles[fullFileName] = FSWatcher::FoundFile.new(fullFileName, modTime, size)
|
227
|
+
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
}
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
# Util classes for the FileSystemWatcher
|
237
|
+
module FSWatcher
|
238
|
+
# The directory to watch
|
239
|
+
class Directory
|
240
|
+
attr_reader :dir, :expression
|
241
|
+
|
242
|
+
def initialize(dir, expression)
|
243
|
+
@dir, @expression = dir, expression
|
244
|
+
@dir.chop! if @dir =~ %r{/$}
|
245
|
+
end
|
246
|
+
|
247
|
+
def getFiles()
|
248
|
+
return Dir[@dir + "/" + @expression]
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# A FoundFile entry for the FileSystemWatcher
|
253
|
+
class FoundFile
|
254
|
+
attr_reader :status, :fileName, :modTime, :size, :md5
|
255
|
+
|
256
|
+
def initialize(fileName, modTime, size, isNewFile=true, useMD5=false)
|
257
|
+
@fileName, @modTime, @size, @isNewFile = fileName, modTime, size, isNewFile
|
258
|
+
@md5 = nil
|
259
|
+
if useMD5 then
|
260
|
+
genMD5()
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
def updateModTime(modTime)
|
265
|
+
@modTime = modTime
|
266
|
+
@isNewFile = false
|
267
|
+
end
|
268
|
+
|
269
|
+
def updateSize(size)
|
270
|
+
@size = size
|
271
|
+
end
|
272
|
+
|
273
|
+
def isNew?
|
274
|
+
return @isNewFile
|
275
|
+
end
|
276
|
+
|
277
|
+
def setMD5(newMD5)
|
278
|
+
@md5 = newMD5
|
279
|
+
end
|
280
|
+
|
281
|
+
# generate my files md5 value
|
282
|
+
def genMD5()
|
283
|
+
@md5 = FSWatcher.genFileMD5(@fileName)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
# utility function for generating md5s from a files contents
|
288
|
+
def FSWatcher.genFileMD5(fileName)
|
289
|
+
if FileTest.file?(fileName) then
|
290
|
+
f = File.open(fileName)
|
291
|
+
contents = f.read()
|
292
|
+
f.close()
|
293
|
+
return MD5.new(contents) if contents
|
294
|
+
end
|
295
|
+
return nil
|
296
|
+
end
|
297
|
+
|
298
|
+
# if the directory you want to watch doesnt exist or isnt readable this is thrown
|
299
|
+
class InvalidDirectoryError < StandardError; end
|
300
|
+
|
301
|
+
# if the file you want to watch doesnt exist or isnt readable this is thrown
|
302
|
+
class InvalidFileError < StandardError; end
|
303
|
+
end
|
304
|
+
|
305
|
+
#--- main program ----
|
306
|
+
if __FILE__ == $0
|
307
|
+
watcher = FileSystemWatcher.new()
|
308
|
+
watcher.addDirectory("/cygdrive/c/Inetpub/ftproot/", "*.xml")
|
309
|
+
watcher.sleepTime = 3
|
310
|
+
watcher.useMD5 = true
|
311
|
+
|
312
|
+
test = false
|
313
|
+
watcher.stopWhen {
|
314
|
+
test == true
|
315
|
+
}
|
316
|
+
|
317
|
+
watcher.start() { |status,file|
|
318
|
+
if status == FileSystemWatcher::CREATED then
|
319
|
+
puts "created: #{file}"
|
320
|
+
elsif status == FileSystemWatcher::MODIFIED then
|
321
|
+
puts "modified: #{file}"
|
322
|
+
elsif status == FileSystemWatcher::DELETED then
|
323
|
+
puts "deleted: #{file}"
|
324
|
+
end
|
325
|
+
}
|
326
|
+
|
327
|
+
sleep(10)
|
328
|
+
test = true
|
329
|
+
watcher.join()
|
330
|
+
end
|
data/lib/lexer/tags.rb
CHANGED
@@ -64,10 +64,14 @@ class Tags
|
|
64
64
|
new_code = []
|
65
65
|
@strings = Strings.new
|
66
66
|
@@inline_tags.each do |tag|
|
67
|
-
general = code.scan(/(^(\t)
|
67
|
+
general = code.scan(/(^((\t)+)?(#{tag}(()$| )([ #.=_\-a-zA-Z0-9:\/\?\+,]+)?))/)
|
68
68
|
general.map! {|x| x[0]}
|
69
69
|
general.each_with_index do |block, index|
|
70
|
-
new_indent = block.scan(/((\t)+)[^\t]/)[0]
|
70
|
+
if new_indent = block.scan(/((\t)+)[^\t]/)[0]
|
71
|
+
new_indent = block.scan(/((\t)+)[^\t]/)[0][0].count("\t") + 1
|
72
|
+
else
|
73
|
+
new_indent = indent
|
74
|
+
end
|
71
75
|
_code_block = block
|
72
76
|
code_block = adding_attributes(block, tag, true)
|
73
77
|
atr = code_block.scan(/([a-zA-Z0-9_-]+=)'?"?([\.\/:a-zA-Z0-9_-]+)'?"?/)
|
data/lib/servicestate.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
# The Runnable module is a generic mixin for including state and
|
4
|
+
# status information in a class
|
5
|
+
module ServiceState
|
6
|
+
# state constants
|
7
|
+
NOT_STARTED = 0
|
8
|
+
STARTED = 1
|
9
|
+
STOPPED = 2
|
10
|
+
CONFIGURED = 3
|
11
|
+
|
12
|
+
attr_reader :startTime, :endTime
|
13
|
+
|
14
|
+
# Initialize the state information
|
15
|
+
def initializeState()
|
16
|
+
@configured = false
|
17
|
+
@startTime = 0
|
18
|
+
@stopTime = 0
|
19
|
+
|
20
|
+
@stateMutex = Mutex.new()
|
21
|
+
@stopWhen = nil
|
22
|
+
setState(NOT_STARTED)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set the callback for when someone calls setState. You
|
26
|
+
# will be passed the state CONSTANT being set
|
27
|
+
def onStateChange(&callbackBlock)
|
28
|
+
@stateCallback = callbackBlock
|
29
|
+
end
|
30
|
+
|
31
|
+
# All methods, inside this class or not, should use this
|
32
|
+
# method to change the state of the JobRunner
|
33
|
+
# @param newState The new state value
|
34
|
+
def setState(newState)
|
35
|
+
@stateMutex.synchronize {
|
36
|
+
if newState == CONFIGURED then
|
37
|
+
@configured = true
|
38
|
+
else
|
39
|
+
@state = newState
|
40
|
+
if isStarted? then
|
41
|
+
@startTime = Time.now()
|
42
|
+
elsif isStopped?
|
43
|
+
@stopTime = Time.now()
|
44
|
+
end
|
45
|
+
end
|
46
|
+
}
|
47
|
+
|
48
|
+
if defined?(@stateCallback) then
|
49
|
+
@stateCallback.call(newState)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def isConfigured?
|
54
|
+
return @configured
|
55
|
+
end
|
56
|
+
|
57
|
+
def isStarted?
|
58
|
+
return @state == STARTED
|
59
|
+
end
|
60
|
+
|
61
|
+
def isStopped?
|
62
|
+
if @state == STOPPED then
|
63
|
+
return true
|
64
|
+
elsif @stopWhen && @stopWhen.call() then
|
65
|
+
setState(STOPPED)
|
66
|
+
return true
|
67
|
+
else
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def stopWhen(&block)
|
73
|
+
@stopWhen = block
|
74
|
+
end
|
75
|
+
end
|
data/lib/sweet.rb
CHANGED
@@ -2,27 +2,95 @@
|
|
2
2
|
|
3
3
|
require "interpreter"
|
4
4
|
require "readline"
|
5
|
+
require "trollop"
|
6
|
+
require "filesystemwatcher"
|
5
7
|
|
6
|
-
|
8
|
+
class Sweet
|
9
|
+
|
10
|
+
def parsing_options
|
7
11
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
+
opts = Trollop::options do
|
13
|
+
version "Sweet 0.1.9 (c) 2011 Joao Moura a.k.a D3TR1"
|
14
|
+
banner <<-EOS
|
15
|
+
A Sweet Language, that compiles for HTML.
|
16
|
+
|
17
|
+
Usage:
|
18
|
+
sweet [options] <filename> or <path>
|
19
|
+
|
20
|
+
Examples:
|
21
|
+
sweet -c example.s
|
22
|
+
sweet -w ./
|
23
|
+
sweet -o
|
24
|
+
|
25
|
+
Options:
|
26
|
+
EOS
|
27
|
+
|
28
|
+
opt :compile, "Compile a single file", :type => :string
|
29
|
+
opt :watch, "Watch a directory", :type => :string
|
30
|
+
opt :console, "Open SIC ( Sweet Interactive Console )"
|
31
|
+
end
|
32
|
+
Trollop::die :compile, "the file must exist" unless File.exist?(opts[:compile]) if opts[:compile]
|
33
|
+
return opts
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def sic
|
38
|
+
|
39
|
+
puts "Sweet Interactive Console, CTRL+C to quit"
|
40
|
+
loop do
|
41
|
+
line = Readline::readline(">> ")
|
42
|
+
Readline::HISTORY.push(line)
|
43
|
+
value = interpreter.eval(line)
|
44
|
+
puts "=> #{value.ruby_value.inspect}"
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
def compile_file(file)
|
52
|
+
|
53
|
+
interpreter = Interpreter.new
|
54
|
+
@filename = file.split(".")[0]
|
55
|
+
File.new("#{@filename}.html", "w")
|
12
56
|
interpreter.eval File.read(file)
|
13
57
|
File.open("#{@filename}.html", 'a') do |f|
|
14
58
|
f.puts @code.join("\n")
|
15
59
|
end
|
16
60
|
puts "Sweet code compiled!"
|
17
61
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
62
|
+
end
|
63
|
+
|
64
|
+
sweet = Sweet.new
|
65
|
+
opts = sweet.parsing_options
|
66
|
+
|
67
|
+
|
68
|
+
# Compile a single file
|
69
|
+
if opts[:compile]
|
70
|
+
compile_file(opts[:compile])
|
27
71
|
|
72
|
+
# Start watching a folder
|
73
|
+
elsif opts[:watch]
|
74
|
+
puts "Starting to watch Sweet file(s)"
|
75
|
+
watcher = FileSystemWatcher.new()
|
76
|
+
watcher.addDirectory("#{opts[:watch]}", "*.s")
|
77
|
+
watcher.sleepTime = 0.1
|
78
|
+
watcher.start { |status,file|
|
79
|
+
if status == FileSystemWatcher::CREATED
|
80
|
+
puts "created: #{file}"
|
81
|
+
compile_file(file)
|
82
|
+
|
83
|
+
elsif status == FileSystemWatcher::MODIFIED
|
84
|
+
puts "modified: #{file}"
|
85
|
+
compile_file(file)
|
86
|
+
|
87
|
+
elsif status == FileSystemWatcher::DELETED
|
88
|
+
puts "deleted: #{file}"
|
89
|
+
end
|
90
|
+
}
|
91
|
+
watcher.join()
|
92
|
+
|
93
|
+
# Start the REPL, read-eval-print-loop, or interactive interpreter
|
94
|
+
elsif opts[:console]
|
95
|
+
sweet.sic
|
28
96
|
end
|
data/lib/trollop.rb
ADDED
@@ -0,0 +1,782 @@
|
|
1
|
+
## lib/trollop.rb -- trollop command-line processing library
|
2
|
+
## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
|
3
|
+
## Copyright:: Copyright 2007 William Morgan
|
4
|
+
## License:: the same terms as ruby itself
|
5
|
+
|
6
|
+
require 'date'
|
7
|
+
|
8
|
+
module Trollop
|
9
|
+
|
10
|
+
VERSION = "1.16.2"
|
11
|
+
|
12
|
+
## Thrown by Parser in the event of a commandline error. Not needed if
|
13
|
+
## you're using the Trollop::options entry.
|
14
|
+
class CommandlineError < StandardError; end
|
15
|
+
|
16
|
+
## Thrown by Parser if the user passes in '-h' or '--help'. Handled
|
17
|
+
## automatically by Trollop#options.
|
18
|
+
class HelpNeeded < StandardError; end
|
19
|
+
|
20
|
+
## Thrown by Parser if the user passes in '-h' or '--version'. Handled
|
21
|
+
## automatically by Trollop#options.
|
22
|
+
class VersionNeeded < StandardError; end
|
23
|
+
|
24
|
+
## Regex for floating point numbers
|
25
|
+
FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?[\d]+)?$/
|
26
|
+
|
27
|
+
## Regex for parameters
|
28
|
+
PARAM_RE = /^-(-|\.$|[^\d\.])/
|
29
|
+
|
30
|
+
## The commandline parser. In typical usage, the methods in this class
|
31
|
+
## will be handled internally by Trollop::options. In this case, only the
|
32
|
+
## #opt, #banner and #version, #depends, and #conflicts methods will
|
33
|
+
## typically be called.
|
34
|
+
##
|
35
|
+
## If you want to instantiate this class yourself (for more complicated
|
36
|
+
## argument-parsing logic), call #parse to actually produce the output hash,
|
37
|
+
## and consider calling it from within
|
38
|
+
## Trollop::with_standard_exception_handling.
|
39
|
+
class Parser
|
40
|
+
|
41
|
+
## The set of values that indicate a flag option when passed as the
|
42
|
+
## +:type+ parameter of #opt.
|
43
|
+
FLAG_TYPES = [:flag, :bool, :boolean]
|
44
|
+
|
45
|
+
## The set of values that indicate a single-parameter (normal) option when
|
46
|
+
## passed as the +:type+ parameter of #opt.
|
47
|
+
##
|
48
|
+
## A value of +io+ corresponds to a readable IO resource, including
|
49
|
+
## a filename, URI, or the strings 'stdin' or '-'.
|
50
|
+
SINGLE_ARG_TYPES = [:int, :integer, :string, :double, :float, :io, :date]
|
51
|
+
|
52
|
+
## The set of values that indicate a multiple-parameter option (i.e., that
|
53
|
+
## takes multiple space-separated values on the commandline) when passed as
|
54
|
+
## the +:type+ parameter of #opt.
|
55
|
+
MULTI_ARG_TYPES = [:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
|
56
|
+
|
57
|
+
## The complete set of legal values for the +:type+ parameter of #opt.
|
58
|
+
TYPES = FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
|
59
|
+
|
60
|
+
INVALID_SHORT_ARG_REGEX = /[\d-]/ #:nodoc:
|
61
|
+
|
62
|
+
## The values from the commandline that were not interpreted by #parse.
|
63
|
+
attr_reader :leftovers
|
64
|
+
|
65
|
+
## The complete configuration hashes for each option. (Mainly useful
|
66
|
+
## for testing.)
|
67
|
+
attr_reader :specs
|
68
|
+
|
69
|
+
## Initializes the parser, and instance-evaluates any block given.
|
70
|
+
def initialize *a, &b
|
71
|
+
@version = nil
|
72
|
+
@leftovers = []
|
73
|
+
@specs = {}
|
74
|
+
@long = {}
|
75
|
+
@short = {}
|
76
|
+
@order = []
|
77
|
+
@constraints = []
|
78
|
+
@stop_words = []
|
79
|
+
@stop_on_unknown = false
|
80
|
+
|
81
|
+
#instance_eval(&b) if b # can't take arguments
|
82
|
+
cloaker(&b).bind(self).call(*a) if b
|
83
|
+
end
|
84
|
+
|
85
|
+
## Define an option. +name+ is the option name, a unique identifier
|
86
|
+
## for the option that you will use internally, which should be a
|
87
|
+
## symbol or a string. +desc+ is a string description which will be
|
88
|
+
## displayed in help messages.
|
89
|
+
##
|
90
|
+
## Takes the following optional arguments:
|
91
|
+
##
|
92
|
+
## [+:long+] Specify the long form of the argument, i.e. the form with two dashes. If unspecified, will be automatically derived based on the argument name by turning the +name+ option into a string, and replacing any _'s by -'s.
|
93
|
+
## [+:short+] Specify the short form of the argument, i.e. the form with one dash. If unspecified, will be automatically derived from +name+.
|
94
|
+
## [+:type+] Require that the argument take a parameter or parameters of type +type+. For a single parameter, the value can be a member of +SINGLE_ARG_TYPES+, or a corresponding Ruby class (e.g. +Integer+ for +:int+). For multiple-argument parameters, the value can be any member of +MULTI_ARG_TYPES+ constant. If unset, the default argument type is +:flag+, meaning that the argument does not take a parameter. The specification of +:type+ is not necessary if a +:default+ is given.
|
95
|
+
## [+:default+] Set the default value for an argument. Without a default value, the hash returned by #parse (and thus Trollop::options) will have a +nil+ value for this key unless the argument is given on the commandline. The argument type is derived automatically from the class of the default value given, so specifying a +:type+ is not necessary if a +:default+ is given. (But see below for an important caveat when +:multi+: is specified too.) If the argument is a flag, and the default is set to +true+, then if it is specified on the the commandline the value will be +false+.
|
96
|
+
## [+:required+] If set to +true+, the argument must be provided on the commandline.
|
97
|
+
## [+:multi+] If set to +true+, allows multiple occurrences of the option on the commandline. Otherwise, only a single instance of the option is allowed. (Note that this is different from taking multiple parameters. See below.)
|
98
|
+
##
|
99
|
+
## Note that there are two types of argument multiplicity: an argument
|
100
|
+
## can take multiple values, e.g. "--arg 1 2 3". An argument can also
|
101
|
+
## be allowed to occur multiple times, e.g. "--arg 1 --arg 2".
|
102
|
+
##
|
103
|
+
## Arguments that take multiple values should have a +:type+ parameter
|
104
|
+
## drawn from +MULTI_ARG_TYPES+ (e.g. +:strings+), or a +:default:+
|
105
|
+
## value of an array of the correct type (e.g. [String]). The
|
106
|
+
## value of this argument will be an array of the parameters on the
|
107
|
+
## commandline.
|
108
|
+
##
|
109
|
+
## Arguments that can occur multiple times should be marked with
|
110
|
+
## +:multi+ => +true+. The value of this argument will also be an array.
|
111
|
+
## In contrast with regular non-multi options, if not specified on
|
112
|
+
## the commandline, the default value will be [], not nil.
|
113
|
+
##
|
114
|
+
## These two attributes can be combined (e.g. +:type+ => +:strings+,
|
115
|
+
## +:multi+ => +true+), in which case the value of the argument will be
|
116
|
+
## an array of arrays.
|
117
|
+
##
|
118
|
+
## There's one ambiguous case to be aware of: when +:multi+: is true and a
|
119
|
+
## +:default+ is set to an array (of something), it's ambiguous whether this
|
120
|
+
## is a multi-value argument as well as a multi-occurrence argument.
|
121
|
+
## In thise case, Trollop assumes that it's not a multi-value argument.
|
122
|
+
## If you want a multi-value, multi-occurrence argument with a default
|
123
|
+
## value, you must specify +:type+ as well.
|
124
|
+
|
125
|
+
def opt name, desc="", opts={}
|
126
|
+
raise ArgumentError, "you already have an argument named '#{name}'" if @specs.member? name
|
127
|
+
|
128
|
+
## fill in :type
|
129
|
+
opts[:type] = # normalize
|
130
|
+
case opts[:type]
|
131
|
+
when :boolean, :bool; :flag
|
132
|
+
when :integer; :int
|
133
|
+
when :integers; :ints
|
134
|
+
when :double; :float
|
135
|
+
when :doubles; :floats
|
136
|
+
when Class
|
137
|
+
case opts[:type].name
|
138
|
+
when 'TrueClass', 'FalseClass'; :flag
|
139
|
+
when 'String'; :string
|
140
|
+
when 'Integer'; :int
|
141
|
+
when 'Float'; :float
|
142
|
+
when 'IO'; :io
|
143
|
+
when 'Date'; :date
|
144
|
+
else
|
145
|
+
raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'"
|
146
|
+
end
|
147
|
+
when nil; nil
|
148
|
+
else
|
149
|
+
raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type])
|
150
|
+
opts[:type]
|
151
|
+
end
|
152
|
+
|
153
|
+
## for options with :multi => true, an array default doesn't imply
|
154
|
+
## a multi-valued argument. for that you have to specify a :type
|
155
|
+
## as well. (this is how we disambiguate an ambiguous situation;
|
156
|
+
## see the docs for Parser#opt for details.)
|
157
|
+
disambiguated_default =
|
158
|
+
if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
|
159
|
+
opts[:default].first
|
160
|
+
else
|
161
|
+
opts[:default]
|
162
|
+
end
|
163
|
+
|
164
|
+
type_from_default =
|
165
|
+
case disambiguated_default
|
166
|
+
when Integer; :int
|
167
|
+
when Numeric; :float
|
168
|
+
when TrueClass, FalseClass; :flag
|
169
|
+
when String; :string
|
170
|
+
when IO; :io
|
171
|
+
when Date; :date
|
172
|
+
when Array
|
173
|
+
if opts[:default].empty?
|
174
|
+
raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
|
175
|
+
end
|
176
|
+
case opts[:default][0] # the first element determines the types
|
177
|
+
when Integer; :ints
|
178
|
+
when Numeric; :floats
|
179
|
+
when String; :strings
|
180
|
+
when IO; :ios
|
181
|
+
when Date; :dates
|
182
|
+
else
|
183
|
+
raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
|
184
|
+
end
|
185
|
+
when nil; nil
|
186
|
+
else
|
187
|
+
raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'"
|
188
|
+
end
|
189
|
+
|
190
|
+
raise ArgumentError, ":type specification and default type don't match (default type is #{type_from_default})" if opts[:type] && type_from_default && opts[:type] != type_from_default
|
191
|
+
|
192
|
+
opts[:type] = opts[:type] || type_from_default || :flag
|
193
|
+
|
194
|
+
## fill in :long
|
195
|
+
opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
|
196
|
+
opts[:long] =
|
197
|
+
case opts[:long]
|
198
|
+
when /^--([^-].*)$/
|
199
|
+
$1
|
200
|
+
when /^[^-]/
|
201
|
+
opts[:long]
|
202
|
+
else
|
203
|
+
raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
|
204
|
+
end
|
205
|
+
raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
|
206
|
+
|
207
|
+
## fill in :short
|
208
|
+
opts[:short] = opts[:short].to_s if opts[:short] unless opts[:short] == :none
|
209
|
+
opts[:short] = case opts[:short]
|
210
|
+
when /^-(.)$/; $1
|
211
|
+
when nil, :none, /^.$/; opts[:short]
|
212
|
+
else raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'"
|
213
|
+
end
|
214
|
+
|
215
|
+
if opts[:short]
|
216
|
+
raise ArgumentError, "short option name #{opts[:short].inspect} is already taken; please specify a (different) :short" if @short[opts[:short]]
|
217
|
+
raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ INVALID_SHORT_ARG_REGEX
|
218
|
+
end
|
219
|
+
|
220
|
+
## fill in :default for flags
|
221
|
+
opts[:default] = false if opts[:type] == :flag && opts[:default].nil?
|
222
|
+
|
223
|
+
## autobox :default for :multi (multi-occurrence) arguments
|
224
|
+
opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].is_a?(Array)
|
225
|
+
|
226
|
+
## fill in :multi
|
227
|
+
opts[:multi] ||= false
|
228
|
+
|
229
|
+
opts[:desc] ||= desc
|
230
|
+
@long[opts[:long]] = name
|
231
|
+
@short[opts[:short]] = name if opts[:short] && opts[:short] != :none
|
232
|
+
@specs[name] = opts
|
233
|
+
@order << [:opt, name]
|
234
|
+
end
|
235
|
+
|
236
|
+
## Sets the version string. If set, the user can request the version
|
237
|
+
## on the commandline. Should probably be of the form "<program name>
|
238
|
+
## <version number>".
|
239
|
+
def version s=nil; @version = s if s; @version end
|
240
|
+
|
241
|
+
## Adds text to the help display. Can be interspersed with calls to
|
242
|
+
## #opt to build a multi-section help page.
|
243
|
+
def banner s; @order << [:text, s] end
|
244
|
+
alias :text :banner
|
245
|
+
|
246
|
+
## Marks two (or more!) options as requiring each other. Only handles
|
247
|
+
## undirected (i.e., mutual) dependencies. Directed dependencies are
|
248
|
+
## better modeled with Trollop::die.
|
249
|
+
def depends *syms
|
250
|
+
syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
|
251
|
+
@constraints << [:depends, syms]
|
252
|
+
end
|
253
|
+
|
254
|
+
## Marks two (or more!) options as conflicting.
|
255
|
+
def conflicts *syms
|
256
|
+
syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
|
257
|
+
@constraints << [:conflicts, syms]
|
258
|
+
end
|
259
|
+
|
260
|
+
## Defines a set of words which cause parsing to terminate when
|
261
|
+
## encountered, such that any options to the left of the word are
|
262
|
+
## parsed as usual, and options to the right of the word are left
|
263
|
+
## intact.
|
264
|
+
##
|
265
|
+
## A typical use case would be for subcommand support, where these
|
266
|
+
## would be set to the list of subcommands. A subsequent Trollop
|
267
|
+
## invocation would then be used to parse subcommand options, after
|
268
|
+
## shifting the subcommand off of ARGV.
|
269
|
+
def stop_on *words
|
270
|
+
@stop_words = [*words].flatten
|
271
|
+
end
|
272
|
+
|
273
|
+
## Similar to #stop_on, but stops on any unknown word when encountered
|
274
|
+
## (unless it is a parameter for an argument). This is useful for
|
275
|
+
## cases where you don't know the set of subcommands ahead of time,
|
276
|
+
## i.e., without first parsing the global options.
|
277
|
+
def stop_on_unknown
|
278
|
+
@stop_on_unknown = true
|
279
|
+
end
|
280
|
+
|
281
|
+
## Parses the commandline. Typically called by Trollop::options,
|
282
|
+
## but you can call it directly if you need more control.
|
283
|
+
##
|
284
|
+
## throws CommandlineError, HelpNeeded, and VersionNeeded exceptions.
|
285
|
+
def parse cmdline=ARGV
|
286
|
+
vals = {}
|
287
|
+
required = {}
|
288
|
+
|
289
|
+
opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
|
290
|
+
opt :help, "Show this message" unless @specs[:help] || @long["help"]
|
291
|
+
|
292
|
+
@specs.each do |sym, opts|
|
293
|
+
required[sym] = true if opts[:required]
|
294
|
+
vals[sym] = opts[:default]
|
295
|
+
vals[sym] = [] if opts[:multi] && !opts[:default] # multi arguments default to [], not nil
|
296
|
+
end
|
297
|
+
|
298
|
+
resolve_default_short_options
|
299
|
+
|
300
|
+
## resolve symbols
|
301
|
+
given_args = {}
|
302
|
+
@leftovers = each_arg cmdline do |arg, params|
|
303
|
+
sym = case arg
|
304
|
+
when /^-([^-])$/
|
305
|
+
@short[$1]
|
306
|
+
when /^--([^-]\S*)$/
|
307
|
+
@long[$1]
|
308
|
+
else
|
309
|
+
raise CommandlineError, "invalid argument syntax: '#{arg}'"
|
310
|
+
end
|
311
|
+
raise CommandlineError, "unknown argument '#{arg}'" unless sym
|
312
|
+
|
313
|
+
if given_args.include?(sym) && !@specs[sym][:multi]
|
314
|
+
raise CommandlineError, "option '#{arg}' specified multiple times"
|
315
|
+
end
|
316
|
+
|
317
|
+
given_args[sym] ||= {}
|
318
|
+
|
319
|
+
given_args[sym][:arg] = arg
|
320
|
+
given_args[sym][:params] ||= []
|
321
|
+
|
322
|
+
# The block returns the number of parameters taken.
|
323
|
+
num_params_taken = 0
|
324
|
+
|
325
|
+
unless params.nil?
|
326
|
+
if SINGLE_ARG_TYPES.include?(@specs[sym][:type])
|
327
|
+
given_args[sym][:params] << params[0, 1] # take the first parameter
|
328
|
+
num_params_taken = 1
|
329
|
+
elsif MULTI_ARG_TYPES.include?(@specs[sym][:type])
|
330
|
+
given_args[sym][:params] << params # take all the parameters
|
331
|
+
num_params_taken = params.size
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
num_params_taken
|
336
|
+
end
|
337
|
+
|
338
|
+
## check for version and help args
|
339
|
+
raise VersionNeeded if given_args.include? :version
|
340
|
+
raise HelpNeeded if given_args.include? :help
|
341
|
+
|
342
|
+
## check constraint satisfaction
|
343
|
+
@constraints.each do |type, syms|
|
344
|
+
constraint_sym = syms.find { |sym| given_args[sym] }
|
345
|
+
next unless constraint_sym
|
346
|
+
|
347
|
+
case type
|
348
|
+
when :depends
|
349
|
+
syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} requires --#{@specs[sym][:long]}" unless given_args.include? sym }
|
350
|
+
when :conflicts
|
351
|
+
syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} conflicts with --#{@specs[sym][:long]}" if given_args.include?(sym) && (sym != constraint_sym) }
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
required.each do |sym, val|
|
356
|
+
raise CommandlineError, "option --#{@specs[sym][:long]} must be specified" unless given_args.include? sym
|
357
|
+
end
|
358
|
+
|
359
|
+
## parse parameters
|
360
|
+
given_args.each do |sym, given_data|
|
361
|
+
arg = given_data[:arg]
|
362
|
+
params = given_data[:params]
|
363
|
+
|
364
|
+
opts = @specs[sym]
|
365
|
+
raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag
|
366
|
+
|
367
|
+
vals["#{sym}_given".intern] = true # mark argument as specified on the commandline
|
368
|
+
|
369
|
+
case opts[:type]
|
370
|
+
when :flag
|
371
|
+
vals[sym] = !opts[:default]
|
372
|
+
when :int, :ints
|
373
|
+
vals[sym] = params.map { |pg| pg.map { |p| parse_integer_parameter p, arg } }
|
374
|
+
when :float, :floats
|
375
|
+
vals[sym] = params.map { |pg| pg.map { |p| parse_float_parameter p, arg } }
|
376
|
+
when :string, :strings
|
377
|
+
vals[sym] = params.map { |pg| pg.map { |p| p.to_s } }
|
378
|
+
when :io, :ios
|
379
|
+
vals[sym] = params.map { |pg| pg.map { |p| parse_io_parameter p, arg } }
|
380
|
+
when :date, :dates
|
381
|
+
vals[sym] = params.map { |pg| pg.map { |p| parse_date_parameter p, arg } }
|
382
|
+
end
|
383
|
+
|
384
|
+
if SINGLE_ARG_TYPES.include?(opts[:type])
|
385
|
+
unless opts[:multi] # single parameter
|
386
|
+
vals[sym] = vals[sym][0][0]
|
387
|
+
else # multiple options, each with a single parameter
|
388
|
+
vals[sym] = vals[sym].map { |p| p[0] }
|
389
|
+
end
|
390
|
+
elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
|
391
|
+
vals[sym] = vals[sym][0] # single option, with multiple parameters
|
392
|
+
end
|
393
|
+
# else: multiple options, with multiple parameters
|
394
|
+
end
|
395
|
+
|
396
|
+
## modify input in place with only those
|
397
|
+
## arguments we didn't process
|
398
|
+
cmdline.clear
|
399
|
+
@leftovers.each { |l| cmdline << l }
|
400
|
+
|
401
|
+
## allow openstruct-style accessors
|
402
|
+
class << vals
|
403
|
+
def method_missing(m, *args)
|
404
|
+
self[m] || self[m.to_s]
|
405
|
+
end
|
406
|
+
end
|
407
|
+
vals
|
408
|
+
end
|
409
|
+
|
410
|
+
def parse_date_parameter param, arg #:nodoc:
|
411
|
+
begin
|
412
|
+
begin
|
413
|
+
time = Chronic.parse(param)
|
414
|
+
rescue NameError
|
415
|
+
# chronic is not available
|
416
|
+
end
|
417
|
+
time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
|
418
|
+
rescue ArgumentError => e
|
419
|
+
raise CommandlineError, "option '#{arg}' needs a date"
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
## Print the help message to +stream+.
|
424
|
+
def educate stream=$stdout
|
425
|
+
width # just calculate it now; otherwise we have to be careful not to
|
426
|
+
# call this unless the cursor's at the beginning of a line.
|
427
|
+
|
428
|
+
left = {}
|
429
|
+
@specs.each do |name, spec|
|
430
|
+
left[name] = "--#{spec[:long]}" +
|
431
|
+
(spec[:short] && spec[:short] != :none ? ", -#{spec[:short]}" : "") +
|
432
|
+
case spec[:type]
|
433
|
+
when :flag; ""
|
434
|
+
when :int; " <i>"
|
435
|
+
when :ints; " <i+>"
|
436
|
+
when :string; " <s>"
|
437
|
+
when :strings; " <s+>"
|
438
|
+
when :float; " <f>"
|
439
|
+
when :floats; " <f+>"
|
440
|
+
when :io; " <filename/uri>"
|
441
|
+
when :ios; " <filename/uri+>"
|
442
|
+
when :date; " <date>"
|
443
|
+
when :dates; " <date+>"
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
leftcol_width = left.values.map { |s| s.length }.max || 0
|
448
|
+
rightcol_start = leftcol_width + 6 # spaces
|
449
|
+
|
450
|
+
unless @order.size > 0 && @order.first.first == :text
|
451
|
+
stream.puts "#@version\n" if @version
|
452
|
+
stream.puts "Options:"
|
453
|
+
end
|
454
|
+
|
455
|
+
@order.each do |what, opt|
|
456
|
+
if what == :text
|
457
|
+
stream.puts wrap(opt)
|
458
|
+
next
|
459
|
+
end
|
460
|
+
|
461
|
+
spec = @specs[opt]
|
462
|
+
stream.printf " %#{leftcol_width}s: ", left[opt]
|
463
|
+
desc = spec[:desc] + begin
|
464
|
+
default_s = case spec[:default]
|
465
|
+
when $stdout; "<stdout>"
|
466
|
+
when $stdin; "<stdin>"
|
467
|
+
when $stderr; "<stderr>"
|
468
|
+
when Array
|
469
|
+
spec[:default].join(", ")
|
470
|
+
else
|
471
|
+
spec[:default].to_s
|
472
|
+
end
|
473
|
+
|
474
|
+
if spec[:default]
|
475
|
+
if spec[:desc] =~ /\.$/
|
476
|
+
" (Default: #{default_s})"
|
477
|
+
else
|
478
|
+
" (default: #{default_s})"
|
479
|
+
end
|
480
|
+
else
|
481
|
+
""
|
482
|
+
end
|
483
|
+
end
|
484
|
+
stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
def width #:nodoc:
|
489
|
+
@width ||= if $stdout.tty?
|
490
|
+
begin
|
491
|
+
require 'curses'
|
492
|
+
Curses::init_screen
|
493
|
+
x = Curses::cols
|
494
|
+
Curses::close_screen
|
495
|
+
x
|
496
|
+
rescue Exception
|
497
|
+
80
|
498
|
+
end
|
499
|
+
else
|
500
|
+
80
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
def wrap str, opts={} # :nodoc:
|
505
|
+
if str == ""
|
506
|
+
[""]
|
507
|
+
else
|
508
|
+
str.split("\n").map { |s| wrap_line s, opts }.flatten
|
509
|
+
end
|
510
|
+
end
|
511
|
+
|
512
|
+
## The per-parser version of Trollop::die (see that for documentation).
|
513
|
+
def die arg, msg
|
514
|
+
if msg
|
515
|
+
$stderr.puts "Error: argument --#{@specs[arg][:long]} #{msg}."
|
516
|
+
else
|
517
|
+
$stderr.puts "Error: #{arg}."
|
518
|
+
end
|
519
|
+
$stderr.puts "Try --help for help."
|
520
|
+
exit(-1)
|
521
|
+
end
|
522
|
+
|
523
|
+
private
|
524
|
+
|
525
|
+
## yield successive arg, parameter pairs
|
526
|
+
def each_arg args
|
527
|
+
remains = []
|
528
|
+
i = 0
|
529
|
+
|
530
|
+
until i >= args.length
|
531
|
+
if @stop_words.member? args[i]
|
532
|
+
remains += args[i .. -1]
|
533
|
+
return remains
|
534
|
+
end
|
535
|
+
case args[i]
|
536
|
+
when /^--$/ # arg terminator
|
537
|
+
remains += args[(i + 1) .. -1]
|
538
|
+
return remains
|
539
|
+
when /^--(\S+?)=(.*)$/ # long argument with equals
|
540
|
+
yield "--#{$1}", [$2]
|
541
|
+
i += 1
|
542
|
+
when /^--(\S+)$/ # long argument
|
543
|
+
params = collect_argument_parameters(args, i + 1)
|
544
|
+
unless params.empty?
|
545
|
+
num_params_taken = yield args[i], params
|
546
|
+
unless num_params_taken
|
547
|
+
if @stop_on_unknown
|
548
|
+
remains += args[i + 1 .. -1]
|
549
|
+
return remains
|
550
|
+
else
|
551
|
+
remains += params
|
552
|
+
end
|
553
|
+
end
|
554
|
+
i += 1 + num_params_taken
|
555
|
+
else # long argument no parameter
|
556
|
+
yield args[i], nil
|
557
|
+
i += 1
|
558
|
+
end
|
559
|
+
when /^-(\S+)$/ # one or more short arguments
|
560
|
+
shortargs = $1.split(//)
|
561
|
+
shortargs.each_with_index do |a, j|
|
562
|
+
if j == (shortargs.length - 1)
|
563
|
+
params = collect_argument_parameters(args, i + 1)
|
564
|
+
unless params.empty?
|
565
|
+
num_params_taken = yield "-#{a}", params
|
566
|
+
unless num_params_taken
|
567
|
+
if @stop_on_unknown
|
568
|
+
remains += args[i + 1 .. -1]
|
569
|
+
return remains
|
570
|
+
else
|
571
|
+
remains += params
|
572
|
+
end
|
573
|
+
end
|
574
|
+
i += 1 + num_params_taken
|
575
|
+
else # argument no parameter
|
576
|
+
yield "-#{a}", nil
|
577
|
+
i += 1
|
578
|
+
end
|
579
|
+
else
|
580
|
+
yield "-#{a}", nil
|
581
|
+
end
|
582
|
+
end
|
583
|
+
else
|
584
|
+
if @stop_on_unknown
|
585
|
+
remains += args[i .. -1]
|
586
|
+
return remains
|
587
|
+
else
|
588
|
+
remains << args[i]
|
589
|
+
i += 1
|
590
|
+
end
|
591
|
+
end
|
592
|
+
end
|
593
|
+
|
594
|
+
remains
|
595
|
+
end
|
596
|
+
|
597
|
+
def parse_integer_parameter param, arg
|
598
|
+
raise CommandlineError, "option '#{arg}' needs an integer" unless param =~ /^\d+$/
|
599
|
+
param.to_i
|
600
|
+
end
|
601
|
+
|
602
|
+
def parse_float_parameter param, arg
|
603
|
+
raise CommandlineError, "option '#{arg}' needs a floating-point number" unless param =~ FLOAT_RE
|
604
|
+
param.to_f
|
605
|
+
end
|
606
|
+
|
607
|
+
def parse_io_parameter param, arg
|
608
|
+
case param
|
609
|
+
when /^(stdin|-)$/i; $stdin
|
610
|
+
else
|
611
|
+
require 'open-uri'
|
612
|
+
begin
|
613
|
+
open param
|
614
|
+
rescue SystemCallError => e
|
615
|
+
raise CommandlineError, "file or url for option '#{arg}' cannot be opened: #{e.message}"
|
616
|
+
end
|
617
|
+
end
|
618
|
+
end
|
619
|
+
|
620
|
+
def collect_argument_parameters args, start_at
|
621
|
+
params = []
|
622
|
+
pos = start_at
|
623
|
+
while args[pos] && args[pos] !~ PARAM_RE && !@stop_words.member?(args[pos]) do
|
624
|
+
params << args[pos]
|
625
|
+
pos += 1
|
626
|
+
end
|
627
|
+
params
|
628
|
+
end
|
629
|
+
|
630
|
+
def resolve_default_short_options
|
631
|
+
@order.each do |type, name|
|
632
|
+
next unless type == :opt
|
633
|
+
opts = @specs[name]
|
634
|
+
next if opts[:short]
|
635
|
+
|
636
|
+
c = opts[:long].split(//).find { |d| d !~ INVALID_SHORT_ARG_REGEX && !@short.member?(d) }
|
637
|
+
if c # found a character to use
|
638
|
+
opts[:short] = c
|
639
|
+
@short[c] = name
|
640
|
+
end
|
641
|
+
end
|
642
|
+
end
|
643
|
+
|
644
|
+
def wrap_line str, opts={}
|
645
|
+
prefix = opts[:prefix] || 0
|
646
|
+
width = opts[:width] || (self.width - 1)
|
647
|
+
start = 0
|
648
|
+
ret = []
|
649
|
+
until start > str.length
|
650
|
+
nextt =
|
651
|
+
if start + width >= str.length
|
652
|
+
str.length
|
653
|
+
else
|
654
|
+
x = str.rindex(/\s/, start + width)
|
655
|
+
x = str.index(/\s/, start) if x && x < start
|
656
|
+
x || str.length
|
657
|
+
end
|
658
|
+
ret << (ret.empty? ? "" : " " * prefix) + str[start ... nextt]
|
659
|
+
start = nextt + 1
|
660
|
+
end
|
661
|
+
ret
|
662
|
+
end
|
663
|
+
|
664
|
+
## instance_eval but with ability to handle block arguments
|
665
|
+
## thanks to why: http://redhanded.hobix.com/inspect/aBlockCostume.html
|
666
|
+
def cloaker &b
|
667
|
+
(class << self; self; end).class_eval do
|
668
|
+
define_method :cloaker_, &b
|
669
|
+
meth = instance_method :cloaker_
|
670
|
+
remove_method :cloaker_
|
671
|
+
meth
|
672
|
+
end
|
673
|
+
end
|
674
|
+
end
|
675
|
+
|
676
|
+
## The easy, syntactic-sugary entry method into Trollop. Creates a Parser,
|
677
|
+
## passes the block to it, then parses +args+ with it, handling any errors or
|
678
|
+
## requests for help or version information appropriately (and then exiting).
|
679
|
+
## Modifies +args+ in place. Returns a hash of option values.
|
680
|
+
##
|
681
|
+
## The block passed in should contain zero or more calls to +opt+
|
682
|
+
## (Parser#opt), zero or more calls to +text+ (Parser#text), and
|
683
|
+
## probably a call to +version+ (Parser#version).
|
684
|
+
##
|
685
|
+
## The returned block contains a value for every option specified with
|
686
|
+
## +opt+. The value will be the value given on the commandline, or the
|
687
|
+
## default value if the option was not specified on the commandline. For
|
688
|
+
## every option specified on the commandline, a key "<option
|
689
|
+
## name>_given" will also be set in the hash.
|
690
|
+
##
|
691
|
+
## Example:
|
692
|
+
##
|
693
|
+
## require 'trollop'
|
694
|
+
## opts = Trollop::options do
|
695
|
+
## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
|
696
|
+
## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
|
697
|
+
## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
|
698
|
+
## opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
|
699
|
+
## end
|
700
|
+
##
|
701
|
+
## ## if called with no arguments
|
702
|
+
## p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
|
703
|
+
##
|
704
|
+
## ## if called with --monkey
|
705
|
+
## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
|
706
|
+
##
|
707
|
+
## See more examples at http://trollop.rubyforge.org.
|
708
|
+
def options args=ARGV, *a, &b
|
709
|
+
@last_parser = Parser.new(*a, &b)
|
710
|
+
with_standard_exception_handling(@last_parser) { @last_parser.parse args }
|
711
|
+
end
|
712
|
+
|
713
|
+
## If Trollop::options doesn't do quite what you want, you can create a Parser
|
714
|
+
## object and call Parser#parse on it. That method will throw CommandlineError,
|
715
|
+
## HelpNeeded and VersionNeeded exceptions when necessary; if you want to
|
716
|
+
## have these handled for you in the standard manner (e.g. show the help
|
717
|
+
## and then exit upon an HelpNeeded exception), call your code from within
|
718
|
+
## a block passed to this method.
|
719
|
+
##
|
720
|
+
## Note that this method will call System#exit after handling an exception!
|
721
|
+
##
|
722
|
+
## Usage example:
|
723
|
+
##
|
724
|
+
## require 'trollop'
|
725
|
+
## p = Trollop::Parser.new do
|
726
|
+
## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
|
727
|
+
## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
|
728
|
+
## end
|
729
|
+
##
|
730
|
+
## opts = Trollop::with_standard_exception_handling p do
|
731
|
+
## o = p.parse ARGV
|
732
|
+
## raise Trollop::HelpNeeded if ARGV.empty? # show help screen
|
733
|
+
## o
|
734
|
+
## end
|
735
|
+
##
|
736
|
+
## Requires passing in the parser object.
|
737
|
+
|
738
|
+
def with_standard_exception_handling parser
|
739
|
+
begin
|
740
|
+
yield
|
741
|
+
rescue CommandlineError => e
|
742
|
+
$stderr.puts "Error: #{e.message}."
|
743
|
+
$stderr.puts "Try --help for help."
|
744
|
+
exit(-1)
|
745
|
+
rescue HelpNeeded
|
746
|
+
parser.educate
|
747
|
+
exit
|
748
|
+
rescue VersionNeeded
|
749
|
+
puts parser.version
|
750
|
+
exit
|
751
|
+
end
|
752
|
+
end
|
753
|
+
|
754
|
+
## Informs the user that their usage of 'arg' was wrong, as detailed by
|
755
|
+
## 'msg', and dies. Example:
|
756
|
+
##
|
757
|
+
## options do
|
758
|
+
## opt :volume, :default => 0.0
|
759
|
+
## end
|
760
|
+
##
|
761
|
+
## die :volume, "too loud" if opts[:volume] > 10.0
|
762
|
+
## die :volume, "too soft" if opts[:volume] < 0.1
|
763
|
+
##
|
764
|
+
## In the one-argument case, simply print that message, a notice
|
765
|
+
## about -h, and die. Example:
|
766
|
+
##
|
767
|
+
## options do
|
768
|
+
## opt :whatever # ...
|
769
|
+
## end
|
770
|
+
##
|
771
|
+
## Trollop::die "need at least one filename" if ARGV.empty?
|
772
|
+
def die arg, msg=nil
|
773
|
+
if @last_parser
|
774
|
+
@last_parser.die arg, msg
|
775
|
+
else
|
776
|
+
raise ArgumentError, "Trollop::die can only be called after Trollop::options"
|
777
|
+
end
|
778
|
+
end
|
779
|
+
|
780
|
+
module_function :options, :die, :with_standard_exception_handling
|
781
|
+
|
782
|
+
end # module
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sweet-lang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -36,6 +36,9 @@ files:
|
|
36
36
|
- lib/runtime/context.rb
|
37
37
|
- lib/runtime/method.rb
|
38
38
|
- lib/runtime/object.rb
|
39
|
+
- lib/trollop.rb
|
40
|
+
- lib/filesystemwatcher.rb
|
41
|
+
- lib/servicestate.rb
|
39
42
|
- bin/sweet
|
40
43
|
homepage: http://joaomdmoura.github.com/sweet/
|
41
44
|
licenses: []
|