cli-dispatcher 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cli-dispatcher.rb +33 -10
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e946454a75a033c55b82ca4814654e27e75f8635ea02b42ad5fbd95a6cc7b4c8
4
- data.tar.gz: f964e67077a8e2077a44db8b033d5a82be7fcbe10e9f2cad11504b6116e8397e
3
+ metadata.gz: 1601becbe908448760f6f02f45ce374db04a7b6926304cb381d5193748121a76
4
+ data.tar.gz: b51470fabe4535e155f8d1d7b82de56b5a5c50471aca95db3aec278fc0abd588
5
5
  SHA512:
6
- metadata.gz: d93f6c815a777f40501094b84bc386178f8a45b20b6dccb4809667d3c7f855b2899a1aa48df04e9cc71eee43353b4105dd911ad1c422f187e276193aa75e8446
7
- data.tar.gz: 6d81ee52b9cac75a9446b221b7b8c8edf767b5998146a87a14bfe0e9353e80ebfbb4b57ce639c3d2d7c1b7f5fb29c1bc809197b3d07aa71a7cfd7c355295eb99
6
+ metadata.gz: 0a7b5ad86d5decfd5486f26b0c618e5f6301399493da66731eeb745340f423194e01923261c1ae8766d0bae4ae0494195f82af301e8d769c98dffe937fc49c75
7
+ data.tar.gz: 7f565106772b1d79b7839bdedf48181eb654a2d440c83b0bbdd474feade662529dbee2615824316fc950e914ccf929a30f7c43506f9082005d080995bb536fc1
@@ -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
 
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.2
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: 2024-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Library for creating command-line programs that accept commands. Also
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  requirements: []
47
- rubygems_version: 3.5.11
47
+ rubygems_version: 3.5.23
48
48
  signing_key:
49
49
  specification_version: 4
50
50
  summary: Command-line command dispatcher