cli-dispatcher 1.2.5 → 1.2.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1b37219d472cc73b55f56047050b442e634de6b08c649dc617c12c0e812f220
4
- data.tar.gz: 1cf08e0e3893984ef5d15c414d87b0889c3073dea6d7a6e8d7a6c6b12889d181
3
+ metadata.gz: a74c717519d1436132b56f8785fbd3b8b8038f1be9066580c8b4f961945ee4f6
4
+ data.tar.gz: c2484c21170248bba3f244a395321ee7e500b99881a89060b8da6ac2aed56b6d
5
5
  SHA512:
6
- metadata.gz: 593cce73a3bd60c69bcad822f1dcb20579f482eec9454ce86bd9164da88ff0f536cebdd67fbf55695c88622e3c391ece18bff6a12e8ad18f094d2cabee45308c
7
- data.tar.gz: da5e84b8318b15d2510ac1a04086a66bd8a53689bb2afb59a9afa4d893f3380554702ecebdc55f88805002a8e81f15377767711cc16f17bb33d386624ea08724
6
+ metadata.gz: e255a21bf5fd03c2431c51daabc933e7e18debe6f7bd6178146bf2b23c5069eb41ce681d7700b325a8cc01c46b4a5df5c1d8a9b9c3f221c14a848d6645814684
7
+ data.tar.gz: 5148804c936128ba58c545d935cefdc7f6b1c063acddd98227e18149e1ef19e993ba9ed35e5783f703d925ec4877de9dbbbfbc1f1af8b5148b674ede88202fa0
@@ -193,22 +193,58 @@ class Dispatcher
193
193
 
194
194
  #
195
195
  # Adds commands relevant when this dispatcher uses Structured data inputs.
196
+ # This method allows for generating documentation on Structured classes.
197
+ #
198
+ # The calling class should implement a method explain_classes that lists at
199
+ # least one Structured class that can be explained.
196
200
  #
197
201
  def self.add_structured_commands
198
202
  def help_explain
199
203
  return <<~EOF
200
204
  Displays an explanation of a Structured class.
201
205
 
202
- Use this to assist in generating or checking a Rubric file.
206
+ This will produce documentation on the elements permitted within the
207
+ class. If no class is given, then a listing of known Structured classes
208
+ is produced.
203
209
  EOF
204
210
  end
205
211
 
206
- def cmd_explain(class_name)
212
+ def cmd_explain(class_name = nil)
213
+ if class_name.nil?
214
+ cache = {}
215
+ puts "The following are Structured classes known to this program. Run"
216
+ puts "the command \"explain [class]\" for documentation on any of them."
217
+ puts
218
+ explain_classes.each do |c|
219
+ list_classes(c, cache)
220
+ end
221
+ return
222
+ end
223
+
207
224
  c = Object.const_get(class_name)
208
- unless c.is_a?(Class) && c.include?(Structured)
225
+ if c.is_a?(Class) && c.include?(Structured)
226
+ c.explain
227
+ else
209
228
  raise "Invalid class #{class_name}"
210
229
  end
211
- c.explain
230
+ end
231
+
232
+ unless defined? explain_classes
233
+ def explain_classes
234
+ return []
235
+ end
236
+ end
237
+
238
+ def list_classes(c, cache, level = 0)
239
+ unless c.include?(Structured)
240
+ raise "Cannot list classes of a non-Structured class"
241
+ end
242
+ puts((" " * level) + c.to_s)
243
+ return if cache.include?(c)
244
+ cache[c] = true
245
+ c.subtypes.each do |sc|
246
+ list_classes(sc, cache, level + 1)
247
+ end
212
248
  end
213
249
 
214
250
  def help_template
@@ -227,12 +263,16 @@ class Dispatcher
227
263
  end
228
264
 
229
265
 
230
- # Receives options, passing them to OptionParser. The options are processed
231
- # when dispatch_argv is called. The usage of this method is that after the
232
- # Dispatcher object is created, this method is called to instantiate the
233
- # options for the class. See #add_options for another way of doing this.
234
266
  #
235
- # The banner and -h/--help options will be added automatically.
267
+ # Creates an OptionParser object for this Dispatcher. The options for the
268
+ # OptionParser are defined in a block passed to this method. The block
269
+ # receives one argument, which is the OptionParser object being created.
270
+ #
271
+ # The banner and -h/--help options will be added automatically to the created
272
+ # OptionParser object.
273
+ #
274
+ # For a slightly simpler way to set up options for this Dispatcher object, see
275
+ # the add_options method.
236
276
  #
237
277
  def setup_options
238
278
  @option_parser = OptionParser.new do |opts|
@@ -241,10 +281,14 @@ class Dispatcher
241
281
  end
242
282
 
243
283
  #
244
- # Given an OptionParser object, add options. By default, this method does
245
- # nothing. The usage of this method, in contrast to #setup_options, is to
246
- # override this method, invoking calls to the +opts+ argument to add options.
247
- # The method will be called automatically when the Dispatcher is invoked.
284
+ # Adds command-line options for this class. By default, this method does
285
+ # nothing. Subclasses may override this method to add options. The method will
286
+ # be automatically invoked during a dispatch_argv call, thereby constructing
287
+ # an OptionParser object to handle the command-line arguments.
288
+ #
289
+ # The argument to this method is an OptionParser object, to which the desired
290
+ # options may be added. The banner and -h/--help options will be added
291
+ # automatically to the OptionParser object.
248
292
  #
249
293
  def add_options(opts)
250
294
  end
@@ -111,6 +111,10 @@ module StructuredPolymorphic
111
111
  end
112
112
  end
113
113
 
114
+ def subtypes
115
+ return @subclasses.values
116
+ end
117
+
114
118
  def template(indent: '')
115
119
  res = "#{indent}# #{name}\n"
116
120
  if @class_description
data/lib/structured.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'texttools'
2
2
  require 'yaml'
3
+ require 'date'
3
4
 
4
5
  #
5
6
  # Sets up a class to receive an initializing hash and to populate information
@@ -461,7 +462,7 @@ module Structured
461
462
  Structured.trace(key.to_s) do
462
463
  val = hash[key] || hash[key.to_s]
463
464
  cval = process_value(obj, val, data)
464
- apply_val(obj, key, cval) if cval
465
+ apply_val(obj, key, cval) unless cval.nil?
465
466
  end
466
467
  end
467
468
 
@@ -649,21 +650,27 @@ module Structured
649
650
  #
650
651
  def try_autoconvert(type, item)
651
652
 
652
- if type == String && item.is_a?(Symbol)
653
+ case { item.class => type }
654
+
655
+ when { Symbol => String }
653
656
  return item.to_s
654
- end
655
657
 
656
- if type == Symbol && item.is_a?(String)
658
+ when { String => Symbol }
657
659
  return item.to_sym
658
- end
659
660
 
660
- # Special case in which strings will be converted to Regexps
661
- if type == Regexp && item.is_a?(String)
661
+ when { String => Regexp }
662
662
  begin
663
663
  return Regexp.new(item)
664
664
  rescue RegexpError
665
665
  input_err("#{item} is not a valid regular expression")
666
666
  end
667
+
668
+ when { String => Date }
669
+ begin
670
+ return Date.parse(item)
671
+ rescue Date::Error
672
+ input_err("#{item} is not a valid date")
673
+ end
667
674
  end
668
675
 
669
676
  return item
@@ -744,6 +751,18 @@ module Structured
744
751
  end
745
752
  end
746
753
 
754
+ #
755
+ # Returns a list of all Structured types that are in elements of this class.
756
+ #
757
+ def subtypes
758
+ datas = @elements.values
759
+ datas << @default_element if @default_element
760
+ return datas.map { |data|
761
+ data[:type].is_a?(Hash) ? data[:type].first : data[:type]
762
+ }.flatten.select { |c| c.is_a?(Class) && c.include?(Structured) }.uniq
763
+ end
764
+
765
+
747
766
  #
748
767
  # Produces a template YAML file for this Structured object.
749
768
  def template(indent: '')
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-dispatcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.5
4
+ version: 1.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Duan
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 2025-10-02 00:00:00.000000000 Z
11
+ date: 2026-02-20 00:00:00.000000000 Z
11
12
  dependencies: []
12
13
  description: |
13
14
  Library for creating command-line programs that accept commands. Also
@@ -28,6 +29,7 @@ licenses:
28
29
  metadata:
29
30
  source_code_uri: https://github.com/charlesduan/cli-dispatcher
30
31
  documentation_uri: https://rubydoc.info/gems/cli-dispatcher/
32
+ post_install_message:
31
33
  rdoc_options: []
32
34
  require_paths:
33
35
  - lib
@@ -42,7 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  - !ruby/object:Gem::Version
43
45
  version: '0'
44
46
  requirements: []
45
- rubygems_version: 3.7.2
47
+ rubygems_version: 3.3.5
48
+ signing_key:
46
49
  specification_version: 4
47
50
  summary: Command-line command dispatcher
48
51
  test_files: []