scout-gear 1.2.0 → 5.1.1

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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +4 -0
  3. data/.vimproject +663 -4
  4. data/Rakefile +1 -0
  5. data/VERSION +1 -1
  6. data/bin/scout +235 -0
  7. data/lib/scout/cmd.rb +344 -0
  8. data/lib/scout/concurrent_stream.rb +259 -0
  9. data/lib/scout/exceptions.rb +15 -8
  10. data/lib/scout/indiferent_hash/options.rb +8 -26
  11. data/lib/scout/indiferent_hash.rb +0 -30
  12. data/lib/scout/log/color.rb +2 -2
  13. data/lib/scout/log/fingerprint.rb +11 -1
  14. data/lib/scout/log/progress/report.rb +0 -1
  15. data/lib/scout/log/progress/util.rb +1 -1
  16. data/lib/scout/log/progress.rb +4 -4
  17. data/lib/scout/log.rb +10 -2
  18. data/lib/scout/meta_extension.rb +15 -1
  19. data/lib/scout/misc/digest.rb +56 -0
  20. data/lib/scout/misc/filesystem.rb +26 -0
  21. data/lib/scout/misc/format.rb +226 -0
  22. data/lib/scout/misc/insist.rb +56 -0
  23. data/lib/scout/misc.rb +5 -11
  24. data/lib/scout/open/lock.rb +61 -0
  25. data/lib/scout/open/remote.rb +120 -0
  26. data/lib/scout/open/stream.rb +372 -0
  27. data/lib/scout/open/util.rb +225 -0
  28. data/lib/scout/open.rb +169 -0
  29. data/lib/scout/path/find.rb +78 -26
  30. data/lib/scout/path/tmpfile.rb +8 -0
  31. data/lib/scout/path/util.rb +17 -5
  32. data/lib/scout/path.rb +13 -31
  33. data/lib/scout/persist/open.rb +17 -0
  34. data/lib/scout/persist/path.rb +15 -0
  35. data/lib/scout/persist/serialize.rb +140 -0
  36. data/lib/scout/persist.rb +54 -0
  37. data/lib/scout/resource/path.rb +15 -0
  38. data/lib/scout/resource/produce/rake.rb +69 -0
  39. data/lib/scout/resource/produce.rb +246 -0
  40. data/lib/scout/resource/scout.rb +3 -0
  41. data/lib/scout/resource.rb +37 -0
  42. data/lib/scout/simple_opt/accessor.rb +54 -0
  43. data/lib/scout/simple_opt/doc.rb +102 -0
  44. data/lib/scout/simple_opt/get.rb +57 -0
  45. data/lib/scout/simple_opt/parse.rb +67 -0
  46. data/lib/scout/simple_opt/setup.rb +26 -0
  47. data/lib/scout/simple_opt.rb +5 -0
  48. data/lib/scout/tmpfile.rb +39 -1
  49. data/lib/scout/workflow/definition.rb +72 -0
  50. data/lib/scout/workflow/documentation.rb +77 -0
  51. data/lib/scout/workflow/step/info.rb +77 -0
  52. data/lib/scout/workflow/step.rb +96 -0
  53. data/lib/scout/workflow/task/inputs.rb +112 -0
  54. data/lib/scout/workflow/task.rb +141 -0
  55. data/lib/scout/workflow/usage.rb +294 -0
  56. data/lib/scout/workflow/util.rb +11 -0
  57. data/lib/scout/workflow.rb +39 -0
  58. data/lib/scout-gear.rb +5 -0
  59. data/lib/scout.rb +1 -0
  60. data/lib/workflow-scout.rb +2 -0
  61. data/scout-gear.gemspec +78 -5
  62. data/scout_commands/alias +48 -0
  63. data/scout_commands/find +83 -0
  64. data/scout_commands/glob +0 -0
  65. data/scout_commands/rbbt +23 -0
  66. data/scout_commands/workflow/task +707 -0
  67. data/test/scout/indiferent_hash/test_case_insensitive.rb +16 -0
  68. data/test/scout/indiferent_hash/test_options.rb +11 -1
  69. data/test/scout/misc/test_digest.rb +30 -0
  70. data/test/scout/misc/test_filesystem.rb +30 -0
  71. data/test/scout/misc/test_insist.rb +13 -0
  72. data/test/scout/open/test_lock.rb +52 -0
  73. data/test/scout/open/test_remote.rb +25 -0
  74. data/test/scout/open/test_stream.rb +515 -0
  75. data/test/scout/open/test_util.rb +73 -0
  76. data/test/scout/path/test_find.rb +37 -1
  77. data/test/scout/persist/test_open.rb +37 -0
  78. data/test/scout/persist/test_path.rb +37 -0
  79. data/test/scout/persist/test_serialize.rb +114 -0
  80. data/test/scout/resource/test_path.rb +40 -0
  81. data/test/scout/resource/test_produce.rb +62 -0
  82. data/test/scout/simple_opt/test_get.rb +11 -0
  83. data/test/scout/simple_opt/test_parse.rb +10 -0
  84. data/test/scout/simple_opt/test_setup.rb +77 -0
  85. data/test/scout/test_cmd.rb +85 -0
  86. data/test/scout/test_concurrent_stream.rb +29 -0
  87. data/test/scout/test_misc.rb +0 -7
  88. data/test/scout/test_open.rb +146 -0
  89. data/test/scout/test_path.rb +3 -1
  90. data/test/scout/test_persist.rb +83 -0
  91. data/test/scout/test_resource.rb +26 -0
  92. data/test/scout/test_workflow.rb +87 -0
  93. data/test/scout/workflow/step/test_info.rb +28 -0
  94. data/test/scout/workflow/task/test_inputs.rb +182 -0
  95. data/test/scout/workflow/test_step.rb +36 -0
  96. data/test/scout/workflow/test_task.rb +178 -0
  97. data/test/scout/workflow/test_usage.rb +26 -0
  98. data/test/scout/workflow/test_util.rb +17 -0
  99. data/test/test_helper.rb +17 -0
  100. data/test/test_scout-gear.rb +0 -0
  101. metadata +76 -3
@@ -0,0 +1,69 @@
1
+ require_relative '../../misc'
2
+ require_relative '../../path'
3
+ require 'rake'
4
+
5
+ class Rake::FileTask
6
+ class << self
7
+ alias_method :old_define_task, :define_task
8
+ end
9
+
10
+ def self.define_task(file, *args, &block)
11
+ @@files ||= []
12
+ @@files << file
13
+ old_define_task(file, *args, &block)
14
+ end
15
+
16
+ def self.files
17
+ @@files
18
+ end
19
+
20
+ def self.clear_files
21
+ @@files = []
22
+ end
23
+ end
24
+
25
+ module ScoutRake
26
+ class TaskNotFound < StandardError; end
27
+ def self.run(rakefile, dir, task, &block)
28
+ old_pwd = FileUtils.pwd
29
+
30
+ Rake::Task.clear
31
+ Rake::FileTask.clear_files
32
+
33
+ t = nil
34
+ pid = Process.fork{
35
+ if block_given?
36
+ TOPLEVEL_BINDING.receiver.instance_exec &block
37
+ else
38
+ if Path.is_filename? rakefile
39
+ rakefile = rakefile.produce.find
40
+ load rakefile
41
+ else
42
+ TmpFile.with_file(rakefile) do |tmpfile|
43
+ load tmpfile
44
+ end
45
+ end
46
+ end
47
+
48
+ raise TaskNotFound if Rake::Task[task].nil?
49
+
50
+ #Misc.pre_fork
51
+ begin
52
+ Misc.in_dir(dir) do
53
+ Rake::Task[task].invoke
54
+
55
+ Rake::Task.clear
56
+ Rake::FileTask.clear_files
57
+ end
58
+ rescue Exception
59
+ Log.error "Error in rake: #{$!.message}"
60
+ Log.exception $!
61
+ Kernel.exit! -1
62
+ end
63
+ Kernel.exit! 0
64
+ }
65
+ Process.waitpid(pid)
66
+ raise "Rake failed" unless $?.success?
67
+
68
+ end
69
+ end
@@ -0,0 +1,246 @@
1
+ require_relative '../open'
2
+ require_relative '../tmpfile'
3
+ require_relative 'produce/rake'
4
+
5
+ module Resource
6
+ def claim(path, type, content = nil, &block)
7
+ if type == :rake
8
+ @rake_dirs ||= {}
9
+ @rake_dirs[path] = content || block
10
+ else
11
+ @resources ||= {}
12
+ @resources[path] = [type, content || block]
13
+ end
14
+ end
15
+
16
+ def rake_for(path)
17
+ @rake_dirs ||= {}
18
+ @rake_dirs.select{|dir, content|
19
+ Misc.path_relative_to(dir, path)
20
+ }.sort_by{|dir, content|
21
+ dir.length
22
+ }.last
23
+ end
24
+
25
+ def has_rake(path)
26
+ !! rake_for(path)
27
+ end
28
+
29
+ def run_rake(path, rakefile, rake_dir)
30
+ task = Misc.path_relative_to rake_dir, path
31
+ rakefile = rakefile.produce if rakefile.respond_to? :produce
32
+ rakefile = rakefile.find if rakefile.respond_to? :find
33
+
34
+ rake_dir = rake_dir.find(:user) if rake_dir.respond_to? :find
35
+
36
+ begin
37
+ if Proc === rakefile
38
+ ScoutRake.run(nil, rake_dir, task, &rakefile)
39
+ else
40
+ ScoutRake.run(rakefile, rake_dir, task)
41
+ end
42
+ rescue Rake::TaskNotFound
43
+ if rake_dir.nil? or rake_dir.empty? or rake_dir == "/" or rake_dir == "./"
44
+ raise $!
45
+ end
46
+ task = File.join(File.basename(rake_dir), task)
47
+ rake_dir = File.dirname(rake_dir)
48
+ retry
49
+ end
50
+ end
51
+
52
+ def produce(path, force = false)
53
+ case
54
+ when @resources.include?(path)
55
+ type, content = @resources[path]
56
+ when (Path === path && @resources.include?(path.original))
57
+ type, content = @resources[path.original]
58
+ when has_rake(path)
59
+ type = :rake
60
+ rake_dir, content = rake_for(path)
61
+ rake_dir = Path.setup(rake_dir.dup, self.pkgdir, self)
62
+ else
63
+ if path !~ /\.(gz|bgz)$/
64
+ begin
65
+ produce(path.annotate(path + '.gz'), force)
66
+ rescue ResourceNotFound
67
+ begin
68
+ produce(path.annotate(path + '.bgz'), force)
69
+ rescue ResourceNotFound
70
+ raise ResourceNotFound, "Resource is missing and does not seem to be claimed: #{ self } -- #{ path } "
71
+ end
72
+ end
73
+ else
74
+ raise ResourceNotFound, "Resource is missing and does not seem to be claimed: #{ self } -- #{ path } "
75
+ end
76
+ end
77
+
78
+ if path.respond_to?(:find)
79
+ final_path = force ? path.find(:default) : path.find
80
+ else
81
+ final_path = path
82
+ end
83
+
84
+ if type and not File.exist?(final_path) or force
85
+ Log.medium "Producing: (#{self.to_s}) #{ final_path }"
86
+ lock_filename = TmpFile.tmp_for_file(final_path, :dir => lock_dir)
87
+
88
+ Open.lock lock_filename do
89
+ FileUtils.rm_rf final_path if force and File.exist? final_path
90
+
91
+ if ! File.exist?(final_path) || force
92
+
93
+ begin
94
+ case type
95
+ when :string
96
+ Open.sensible_write(final_path, content)
97
+ when :csv
98
+ raise "TSV/CSV Not implemented yet"
99
+ #require 'rbbt/tsv/csv'
100
+ #tsv = TSV.csv Open.open(content)
101
+ #Open.sensible_write(final_path, tsv.to_s)
102
+ when :url
103
+ options = {}
104
+ options[:noz] = true if Open.gzip?(final_path) || Open.bgzip?(final_path) || Open.zip?(final_path)
105
+ Open.sensible_write(final_path, Open.open(content, options))
106
+ when :proc
107
+ data = case content.arity
108
+ when 0
109
+ content.call
110
+ when 1
111
+ content.call final_path
112
+ end
113
+ case data
114
+ when String, IO, StringIO
115
+ Open.sensible_write(final_path, data)
116
+ when Array
117
+ Open.sensible_write(final_path, data * "\n")
118
+ when TSV
119
+ Open.sensible_write(final_path, data.dumper_stream)
120
+ when TSV::Dumper
121
+ Open.sensible_write(final_path, data.stream)
122
+ when nil
123
+ else
124
+ raise "Unkown object produced: #{Log.fingerprint data}"
125
+ end
126
+ when :rake
127
+ run_rake(path, content, rake_dir)
128
+ when :install
129
+ Log.debug "Installing software: #{path}"
130
+
131
+ $set_software_env = false unless File.exist? path
132
+
133
+ software_dir = path.resource.root.software.find :user
134
+ helper_file = File.expand_path(Rbbt.share.install.software.lib.install_helpers.find(:lib, caller_lib_dir(__FILE__)))
135
+ #helper_file = File.expand_path(Rbbt.share.install.software.lib.install_helpers.find)
136
+
137
+ preamble = <<-EOF
138
+ #!/bin/bash
139
+
140
+ RBBT_SOFTWARE_DIR="#{software_dir}"
141
+
142
+ INSTALL_HELPER_FILE="#{helper_file}"
143
+ source "$INSTALL_HELPER_FILE"
144
+ EOF
145
+
146
+ content = content.call if Proc === content
147
+
148
+ content = if content =~ /git:|\.git$/
149
+ {:git => content}
150
+ else
151
+ {:src => content}
152
+ end if String === content and Open.remote?(content)
153
+
154
+ script_text = case content
155
+ when nil
156
+ raise "No way to install #{path}"
157
+ when Path
158
+ Open.read(content)
159
+ when String
160
+ if Path.is_filename?(content) and Open.exists?(content)
161
+ Open.read(content)
162
+ else
163
+ content
164
+ end
165
+ when Hash
166
+ name = content[:name] || File.basename(path)
167
+ git = content[:git]
168
+ src = content[:src]
169
+ url = content[:url]
170
+ jar = content[:jar]
171
+ extra = content[:extra]
172
+ commands = content[:commands]
173
+ if git
174
+ <<-EOF
175
+
176
+ name='#{name}'
177
+ url='#{git}'
178
+
179
+ install_git "$name" "$url" #{extra}
180
+
181
+ #{commands}
182
+ EOF
183
+ elsif src
184
+ <<-EOF
185
+
186
+ name='#{name}'
187
+ url='#{src}'
188
+
189
+ install_src "$name" "$url" #{extra}
190
+
191
+ #{commands}
192
+ EOF
193
+ elsif jar
194
+ <<-EOF
195
+
196
+ name='#{name}'
197
+ url='#{jar}'
198
+
199
+ install_jar "$name" "$url" #{extra}
200
+
201
+ #{commands}
202
+ EOF
203
+ else
204
+ <<-EOF
205
+
206
+ name='#{name}'
207
+ url='#{url}'
208
+
209
+ #{commands}
210
+ EOF
211
+ end
212
+ end
213
+
214
+ script = preamble + "\n" + script_text
215
+ Log.debug "Installing software with script:\n" << script
216
+ CMD.cmd_log('bash', :in => script)
217
+
218
+ set_software_env(software_dir) unless $set_software_env
219
+ $set_software_env = true
220
+ else
221
+ raise "Could not produce #{ resource }. (#{ type }, #{ content })"
222
+ end
223
+ rescue
224
+ FileUtils.rm_rf final_path if File.exist? final_path
225
+ raise $!
226
+ end
227
+ end
228
+ end
229
+ end
230
+
231
+ # After producing a file, make sure we recheck all locations, the file
232
+ # might have appeared with '.gz' extension for instance
233
+ path.instance_variable_set("@path", {})
234
+
235
+ path
236
+ end
237
+
238
+ end
239
+
240
+ module Path
241
+ def produce(force = false)
242
+ return self if ! force && Open.exist?(self)
243
+ self.pkgdir.produce self if Resource === self.pkgdir
244
+ return self
245
+ end
246
+ end
@@ -0,0 +1,3 @@
1
+ module Scout
2
+ extend Resource
3
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'log'
2
+ require_relative 'path'
3
+ require_relative 'resource/produce'
4
+
5
+ module Resource
6
+ extend MetaExtension
7
+ extension_attr :pkgdir, :libdir, :subdir, :resources, :rake_dirs, :path_maps, :lock_dir
8
+
9
+ def self.default_lock_dir
10
+ Path.setup('tmp/produce_locks').find
11
+ end
12
+
13
+ def subdir
14
+ @subdir ||= ""
15
+ end
16
+
17
+ def lock_dir
18
+ @lock_dir ||= Resource.default_lock_dir
19
+ end
20
+
21
+ def pkgdir
22
+ @pkgdir ||= Path.default_pkgdir
23
+ end
24
+
25
+ def root
26
+ Path.setup(subdir, self, self.libdir, @path_maps)
27
+ end
28
+
29
+ def method_missing(name, prev = nil, *args)
30
+ if prev.nil?
31
+ root.send(name, *args)
32
+ else
33
+ root.send(name, prev, *args)
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,54 @@
1
+ module SOPT
2
+ class << self
3
+ attr_writer :inputs, :input_shortcuts, :input_types, :input_descriptions, :input_defaults
4
+ end
5
+
6
+ def self.all
7
+ @all ||= {}
8
+ end
9
+
10
+ def self.shortcuts
11
+ @shortcuts ||= {}
12
+ end
13
+
14
+ def self.inputs
15
+ @inputs ||= []
16
+ end
17
+
18
+ def self.input_shortcuts
19
+ @input_shortcuts ||= {}
20
+ end
21
+
22
+ def self.input_types
23
+ @input_types ||= {}
24
+ end
25
+
26
+ def self.input_descriptions
27
+ @input_descriptions ||= {}
28
+ end
29
+
30
+ def self.input_defaults
31
+ @input_defaults ||= {}
32
+ end
33
+
34
+ def self.reset
35
+ @shortcuts = {}
36
+ @all = {}
37
+ end
38
+
39
+ def self.delete_inputs(inputs)
40
+ inputs.each do |input|
41
+ input = input.to_s
42
+ self.shortcuts.delete self.input_shortcuts.delete(input)
43
+ self.inputs.delete input
44
+ self.input_types.delete input
45
+ self.input_defaults.delete input
46
+ self.input_descriptions.delete input
47
+ end
48
+ end
49
+
50
+ def self.usage
51
+ puts SOPT.doc
52
+ exit 0
53
+ end
54
+ end
@@ -0,0 +1,102 @@
1
+ require_relative '../log'
2
+ module SOPT
3
+
4
+ class << self
5
+ attr_writer :command, :summary, :synopsys, :description
6
+ end
7
+
8
+ def self.command
9
+ @command ||= File.basename($0)
10
+ end
11
+
12
+ def self.summary
13
+ @summary ||= ""
14
+ end
15
+
16
+ def self.synopsys
17
+ @synopsys ||= begin
18
+ "#{command} " <<
19
+ inputs.collect{|name|
20
+ "[" << input_format(name, input_types[name] || :string, input_defaults[name], input_shortcuts[name]).sub(/:$/,'') << "]"
21
+ } * " "
22
+ end
23
+ end
24
+
25
+ def self.description
26
+ @description ||= "Missing"
27
+ end
28
+
29
+ def self.input_format(name, type = nil, default = nil, short = nil)
30
+ input_str = (short.nil? or short.empty?) ? "--#{name}" : "-#{short},--#{name}"
31
+ input_str = Log.color(:blue, input_str)
32
+ extra = case type
33
+ when nil
34
+ ""
35
+ when :boolean
36
+ "[=false]"
37
+ when :tsv, :text
38
+ "=<file|->"
39
+ when :array
40
+ "=<list|file|->"
41
+ else
42
+ "=<#{ type }>"
43
+ end
44
+ #extra << " (default: #{Array === default ? (default.length > 3 ? default[0..2]*", " + ', ...' : default*", " ): default})" if default != nil
45
+ extra << " (default: #{Misc.fingerprint(default)})" if default != nil
46
+ input_str << Log.color(:green, extra)
47
+ end
48
+
49
+ def self.input_doc(inputs, input_types = nil, input_descriptions = nil, input_defaults = nil, input_shortcuts = nil)
50
+ type = description = default = nil
51
+ shortcut = ""
52
+ seen = []
53
+ inputs.collect do |name|
54
+ next if seen.include? name
55
+ seen << name
56
+
57
+ type = input_types[name] unless input_types.nil?
58
+ description = input_descriptions[name] unless input_descriptions.nil?
59
+ default = input_defaults[name] unless input_defaults.nil?
60
+
61
+ name = name.to_s
62
+
63
+ case input_shortcuts
64
+ when nil, FalseClass
65
+ shortcut = nil
66
+ when Hash
67
+ shortcut = input_shortcuts[name]
68
+ when TrueClass
69
+ shortcut = fix_shortcut(name[0], name)
70
+ end
71
+
72
+ type = :string if type.nil?
73
+ register(shortcut, name, type, description) unless self.inputs.include? name
74
+
75
+ name = SOPT.input_format(name, type.to_sym, default, shortcut)
76
+ Misc.format_definition_list_item(name, description, 80, 31, nil)
77
+ end * "\n"
78
+ end
79
+
80
+ def self.doc
81
+ doc =<<-EOF
82
+ #{Log.color :magenta}#{command}(1) -- #{summary}
83
+ #{"=" * (command.length + summary.length + 7)}#{Log.color :reset}
84
+
85
+ EOF
86
+
87
+ if synopsys and not synopsys.empty?
88
+ doc << Log.color(:magenta, "## SYNOPSYS") << "\n\n"
89
+ doc << Log.color(:blue, synopsys) << "\n\n"
90
+ end
91
+
92
+ if description and not description.empty?
93
+ doc << Log.color(:magenta, "## DESCRIPTION") << "\n\n"
94
+ doc << Misc.format_paragraph(description) << "\n\n"
95
+ end
96
+
97
+ doc << Log.color(:magenta, "## OPTIONS") << "\n\n"
98
+ doc << input_doc(inputs, input_types, input_descriptions, input_defaults, input_shortcuts)
99
+
100
+ doc
101
+ end
102
+ end
@@ -0,0 +1,57 @@
1
+ module SOPT
2
+ GOT_OPTIONS= IndiferentHash.setup({})
3
+ def self.current_options=(options)
4
+ @@current_options = options
5
+ end
6
+ def self.consume(args = ARGV)
7
+ i = 0
8
+ @@current_options ||= {}
9
+ while i < args.length do
10
+ current = args[i]
11
+ break if current == "--"
12
+ if m = current.match(/--?(.+?)(?:=(.+))?$/)
13
+ key = $1
14
+ value = $2
15
+
16
+ input = inputs.include?(key)? key : shortcuts[key]
17
+
18
+ if input.nil?
19
+ i += 1
20
+ next
21
+ else
22
+ args.delete_at i
23
+ end
24
+ else
25
+ i += 1
26
+ next
27
+ end
28
+
29
+ if input_types[input] == :string
30
+ value = args.delete_at(i) if value.nil?
31
+ @@current_options[input] = value
32
+ else
33
+ if value.nil? and %w(F false FALSE no).include?(args[i])
34
+ Log.warn "Boolean values are best specified as #{current}=[true|false], not #{ current } [true|false]. Token '#{args[i]}' following '#{current}' automatically assigned as value"
35
+ value = args.delete_at(i)
36
+ end
37
+ @@current_options[input] = %w(F false FALSE no).include?(value)? false : true
38
+ end
39
+ end
40
+
41
+ IndiferentHash.setup @@current_options
42
+ GOT_OPTIONS.merge! @@current_options
43
+
44
+ @@current_options
45
+ end
46
+
47
+ def self.get(opt_str)
48
+ SOPT.parse(opt_str)
49
+ SOPT.consume(ARGV)
50
+ end
51
+
52
+ def self.require(options, *parameters)
53
+ parameters.flatten.each do |parameter|
54
+ raise ParameterException, "Parameter '#{ Log.color :blue, parameter }' not given" if options[parameter].nil?
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ module SOPT
2
+ def self.fix_shortcut(short, long)
3
+ return short unless short and shortcuts.include?(short)
4
+
5
+ current = shortcuts.select{|s,l| l == long}.collect{|s,l| s }.first
6
+ return current if current
7
+
8
+ chars = long.chars.to_a
9
+ current = [chars.shift]
10
+ short = current * ""
11
+
12
+ if (shortcuts.include?(short) and not shortcuts[short] == long)
13
+ if long.index "-" or long.index "_"
14
+ parts = long.split(/[_-]/)
15
+ acc = parts.collect{|s| s[0] } * ""
16
+ return acc unless shortcuts.include? acc
17
+ elsif m = long.match(/(\d+)/)
18
+ n = m[0]
19
+ acc = long[0] + n
20
+ return acc unless shortcuts.include? acc
21
+ end
22
+ end
23
+
24
+ while shortcuts.include?(short) && shortcuts[short] != long
25
+ next_letter = chars.shift
26
+ next_letter = chars.shift while %w(. - _).include?(next_letter)
27
+ return nil if next_letter.nil?
28
+ current << next_letter
29
+ short = current * ""
30
+ end
31
+
32
+ return nil if shortcuts.include? short
33
+
34
+ short
35
+ end
36
+
37
+ def self.register(short, long, asterisk, description)
38
+ short = fix_shortcut(short, long)
39
+ shortcuts[short] = long if short
40
+ inputs << long
41
+ input_shortcuts[long] = short
42
+ input_descriptions[long] = description
43
+ input_types[long] = asterisk ? :string : :boolean
44
+ end
45
+
46
+ def self.parse(opt_str)
47
+ inputs = []
48
+
49
+ if opt_str.include? "\n"
50
+ re = /\n+/
51
+ else
52
+ re = /:/
53
+ end
54
+
55
+ opt_str.split(re).each do |entry|
56
+ entry.strip!
57
+ next if entry.empty?
58
+ names, _sep, description = entry.partition(/\s+/)
59
+ short, long, asterisk = names.match(/\s*(?:-(.+))?(?:--(.+?))([*])?$/).values_at 1,2,3
60
+
61
+ inputs << long
62
+ register short, long, asterisk, description
63
+ end
64
+
65
+ inputs
66
+ end
67
+ end
@@ -0,0 +1,26 @@
1
+ module SOPT
2
+
3
+ def self.setup(str)
4
+ parts = str.split(/\n\n+/)
5
+
6
+ summary = parts.shift unless parts.first =~ /^\s*\$-/
7
+ synopsys = parts.shift if parts.first =~ /^\s*\$/
8
+
9
+ description = []
10
+ while parts.first and parts.first !~ /^\s*-/
11
+ description << parts.shift
12
+ end
13
+ description = description * "\n\n"
14
+
15
+ options = parts.collect{|part| part.split("\n").select{|l| l=~ /^\s*-/ } }.flatten.compact * "\n"
16
+
17
+ synopsys.sub!(/^\$\s+/,'') if synopsys
18
+
19
+ SOPT.summary = summary.strip if summary
20
+ SOPT.synopsys = synopsys.strip if synopsys
21
+ SOPT.description = description.strip if description
22
+ SOPT.parse options if options
23
+
24
+ SOPT.consume
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'simple_opt/accessor'
2
+ require_relative 'simple_opt/parse'
3
+ require_relative 'simple_opt/doc'
4
+ require_relative 'simple_opt/get'
5
+ require_relative 'simple_opt/setup'