cli-dispatcher 1.2.1 → 1.2.3

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: e946454a75a033c55b82ca4814654e27e75f8635ea02b42ad5fbd95a6cc7b4c8
4
- data.tar.gz: f964e67077a8e2077a44db8b033d5a82be7fcbe10e9f2cad11504b6116e8397e
3
+ metadata.gz: 1f11805cc9ecff05fe987fe958cb4b6bf6a3a49d2a56596ff0eee761936b3ce3
4
+ data.tar.gz: aac95196261ffb6835e1ca0220c5786f260f71bd8f9e904eaa7a824943cacd04
5
5
  SHA512:
6
- metadata.gz: d93f6c815a777f40501094b84bc386178f8a45b20b6dccb4809667d3c7f855b2899a1aa48df04e9cc71eee43353b4105dd911ad1c422f187e276193aa75e8446
7
- data.tar.gz: 6d81ee52b9cac75a9446b221b7b8c8edf767b5998146a87a14bfe0e9353e80ebfbb4b57ce639c3d2d7c1b7f5fb29c1bc809197b3d07aa71a7cfd7c355295eb99
6
+ metadata.gz: d2a65276ba20ff7532ea71aff7219ade85ec908a317a8753740b42f1531283269e78df415ac6dbb607bfd009800e6e94cdd3692f01c2344b0c4653791455658b
7
+ data.tar.gz: f2ec39cb1df8e03763a8b7b1a70fc01efa696d16df14f593abd8f80acdbd1c54359d05b99104837245be76d9b665896fa24665928ef025bc1bb149110b19c92a
@@ -1,4 +1,5 @@
1
1
  require 'optparse'
2
+ require 'texttools'
2
3
 
3
4
  #
4
5
  # Constructs a program that can operate a number of user-provided commands. To
@@ -6,18 +7,23 @@ require 'optparse'
6
7
  #
7
8
  # def cmd_name(args...)
8
9
  #
9
- # Then create an instance of the class and call one of the dispatch methods.
10
+ # Then create an instance of the class and call one of the dispatch methods,
11
+ # typically:
10
12
  #
11
- # To provide help for a command, define a method:
13
+ # [DispatcherSubclass].new.dispatch_argv
12
14
  #
13
- # def help_name
15
+ # To provide help and/or a category for a command, define the methods
14
16
  #
15
- # The first line should be a short description of the command, which will be
16
- # used in a summary table describing the command.
17
+ # def help_name; [string] end
18
+ # def cat_name; [string] end
19
+ #
20
+ # For the help command, the first line should be a short description of the
21
+ # command, which will be used in a summary table describing the command. For the
22
+ # category, all commands with the same category will be grouped together in the
23
+ # help summary display.
17
24
  #
18
25
  # This class incorporates optparse, providing the commands setup_options and
19
- # add_options to pass
20
- # through options specifications.
26
+ # add_options to pass through options specifications.
21
27
  #
22
28
  class Dispatcher
23
29
 
@@ -118,11 +124,28 @@ class Dispatcher
118
124
  warn("Run 'help [command]' for further help on that command.")
119
125
  warn("")
120
126
 
121
- methods.map { |m|
127
+ grouped_methods = methods.map { |m|
122
128
  s = m.to_s
123
129
  s.start_with?("cmd_") ? s.delete_prefix("cmd_") : nil
124
- }.compact.sort.each do |cmd|
125
- warn("%-10s %s" % [ cmd, help_string(cmd, all: false) ])
130
+ }.compact.group_by { |m|
131
+ cat_m = "cat_#{m}".to_sym
132
+ respond_to?(cat_m) ? send(cat_m) : nil
133
+ }
134
+ help_cmds(grouped_methods.delete(nil)) if grouped_methods.include?(nil)
135
+
136
+ grouped_methods.sort.each do |cat, cmds|
137
+ warn("\n#{cat}\n\n")
138
+ help_cmds(cmds)
139
+ end
140
+ end
141
+
142
+ def help_cmds(cmds)
143
+ cmds.sort.each do |cmd|
144
+ warn(TextTools.line_break(
145
+ help_string(cmd, all: false),
146
+ prefix: " " * 11,
147
+ first_prefix: cmd.ljust(10) + ' ',
148
+ ))
126
149
  end
127
150
  end
128
151
 
data/lib/structured.rb CHANGED
@@ -561,7 +561,7 @@ module Structured
561
561
  # returned, and processing should also stop.
562
562
  #
563
563
  def process_nil_val(val, data)
564
- return [ val, false ] if val
564
+ return [ val, false ] unless val.nil?
565
565
  unless data[:optional]
566
566
  input_err("Required element is missing (or was deleted by a preproc)")
567
567
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-dispatcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Duan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-02 00:00:00.000000000 Z
11
+ date: 2025-01-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Library for creating command-line programs that accept commands. Also