hiiro 0.1.220 → 0.1.221

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: 28013aa2de1c98ae4ecd1a166635b4444f0c2753fe830aeb94d796cddd607b45
4
- data.tar.gz: 17ad399a86996122313f9fb3a78aad3ea732bc8ab1a04fb735ba0b001486e3ab
3
+ metadata.gz: de890107cb43e9b6f6cf34020f4c2c2083d93e19336f30f8c78a0a14368a0b79
4
+ data.tar.gz: 18797bfa2b31b7d162a03ad1144228e320ba1ffc9e8d4377f77f51fa1efa06dd
5
5
  SHA512:
6
- metadata.gz: 7e15908fda49530377d57b400f50439a2dd9a28b24820dc28a45e20e74df150011e981624308c271ea0dcff9f86249c68ad900c6909ed5ee61d5a6e8fe195352
7
- data.tar.gz: c2ba6cb131f38b6aaa37655fc976e597e2fe1c1f0b605df431797ce4ffc2bd8f66ca086390d5691d0a1aafb2e9c4ea93ecb7a5ad581bda89f1388dfd57f8824a
6
+ metadata.gz: 133d7ca0775d50002b3f49f59fd75ed935f8eb2a0ba69747c49ada7ddb29eb52f32b319ee2aa24dccd693b6906a32c071f8128b563f5faace521f304b09c74b8
7
+ data.tar.gz: a6be7f17f45b6db61209c13b703a601044ec7a6d593ec8b8b418ce94cbda95e2aa4d5c6f1ec0f4cafcd467ff984ee7a157bba6110eb16c322f949b67f6d654ad
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'hiiro'
4
+
5
+ Hiiro.run do
6
+ add_option(:group, short: :g, desc: 'group name')
7
+ add_option(:group2, short: :g, long: :group, desc: 'group name')
8
+ add_flag(:verbose, short: :v, desc: 'verbose output')
9
+
10
+ add_subcmd(
11
+ :subcmd_name,
12
+ :alias_name,
13
+ args: [:position1_arg_name, :second_arg],
14
+ opts: [:group, :verbose],
15
+ ) do
16
+ # ...
17
+ end
18
+
19
+ add_subcmd(
20
+ :subcmd_name,
21
+ :alias_name,
22
+ args: [:position1_arg_name, :second_arg],
23
+ opts: [:group2, :verbose], # key is to differentiate between 2 options or
24
+ # flags with the same long-name but different
25
+ # description or other settings
26
+ #
27
+ # for example, one might be a flag, the other an
28
+ # option
29
+ ) do
30
+ # ...
31
+ end
32
+ end
data/lib/hiiro/options.rb CHANGED
@@ -41,18 +41,35 @@ class Hiiro
41
41
  lines.join("\n")
42
42
  end
43
43
 
44
- def flag(name, short: nil, default: false, desc: nil)
45
- defn = Definition.new(name, short: short, type: :flag, default: default, desc: desc)
44
+ def hint
45
+ @definitions
46
+ .reject { |k, _| k == :help }
47
+ .map { |_, d| d.flag? ? d.long_form : "#{d.long_form} <val>" }
48
+ .map { |s| "[#{s}]" }
49
+ .join(' ')
50
+ end
51
+
52
+ def flag(name, long: nil, short: nil, default: false, desc: nil)
53
+ defn = Definition.new(name, long: long, short: short, type: :flag, default: default, desc: desc)
46
54
  @definitions[name.to_sym] = defn
47
55
  self
48
56
  end
49
57
 
50
- def option(name, short: nil, type: :string, default: nil, desc: nil, multi: false)
51
- defn = Definition.new(name, short: short, type: type, default: default, desc: desc, multi: multi)
58
+ def option(name, long: nil, short: nil, type: :string, default: nil, desc: nil, multi: false)
59
+ defn = Definition.new(name, long: long, short: short, type: type, default: default, desc: desc, multi: multi)
52
60
  @definitions[name.to_sym] = defn
53
61
  self
54
62
  end
55
63
 
64
+ def select(names)
65
+ subset = self.class.setup {}
66
+ names.each do |name|
67
+ defn = @definitions[name.to_sym]
68
+ subset.definitions[name.to_sym] = defn if defn
69
+ end
70
+ subset
71
+ end
72
+
56
73
  def parse(args)
57
74
  Args.new(@definitions, args.flatten.compact)
58
75
  end
@@ -146,19 +163,15 @@ class Hiiro
146
163
  end
147
164
 
148
165
  def parse_long_option(arg, args)
149
- if arg.include?('=')
150
- opt, value = arg.split('=', 2)
151
- name = opt.sub(/^--/, '').tr('-', '_').to_sym
152
- else
153
- name = arg.sub(/^--/, '').tr('-', '_').to_sym
154
- value = nil
155
- end
166
+ parts = arg.split('=', 2)
167
+ flag_part = parts[0]
168
+ value = parts[1]
156
169
 
157
- defn = @definitions[name]
170
+ defn = @definitions.values.find { |d| d.long_form == flag_part }
158
171
  return unless defn
159
172
 
160
173
  if defn.flag?
161
- @values[name] = !defn.default
174
+ @values[defn.name] = !defn.default
162
175
  else
163
176
  value ||= args.shift
164
177
  store_value(defn, value)
@@ -194,11 +207,12 @@ class Hiiro
194
207
  end
195
208
 
196
209
  class Definition
197
- attr_reader :name, :short, :type, :default, :desc, :multi
210
+ attr_reader :name, :short, :long, :type, :default, :desc, :multi
198
211
 
199
- def initialize(name, short: nil, type: :string, default: nil, desc: nil, multi: false)
212
+ def initialize(name, short: nil, long: nil, type: :string, default: nil, desc: nil, multi: false)
200
213
  @name = name.to_sym
201
214
  @short = short&.to_s
215
+ @long = long&.to_sym
202
216
  @type = type
203
217
  @default = default
204
218
  @desc = desc
@@ -210,7 +224,7 @@ class Hiiro
210
224
  end
211
225
 
212
226
  def long_form
213
- "--#{name.to_s.tr('_', '-')}"
227
+ "--#{(@long || @name).to_s.tr('_', '-')}"
214
228
  end
215
229
 
216
230
  def short_form
data/lib/hiiro/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Hiiro
2
- VERSION = "0.1.220"
2
+ VERSION = "0.1.221"
3
3
  end
data/lib/hiiro.rb CHANGED
@@ -237,6 +237,36 @@ class Hiiro
237
237
  end
238
238
  alias add_subcmd add_subcommand
239
239
 
240
+ def options
241
+ @options ||= Hiiro::Options.setup {}
242
+ end
243
+
244
+ def add_option(name, **kwargs)
245
+ options.option(name, **kwargs)
246
+ end
247
+
248
+ def add_flag(name, **kwargs)
249
+ options.flag(name, **kwargs)
250
+ end
251
+
252
+ def add_cmd(*names, args: [], opts: [], &block)
253
+ cmd_opts = options.select(opts)
254
+
255
+ wrapper = lambda do |*raw_args|
256
+ @opts = cmd_opts.parse(raw_args)
257
+ instance_eval(&block)
258
+ end
259
+
260
+ names.each do |name|
261
+ runners.add_subcommand(
262
+ name, wrapper,
263
+ subcmd_args: args,
264
+ subcmd_opts: cmd_opts,
265
+ **global_values
266
+ )
267
+ end
268
+ end
269
+
240
270
  def run_subcommand(name, *args)
241
271
  runners.run_subcommand(name, *args)
242
272
  end
@@ -300,6 +330,13 @@ class Hiiro
300
330
  puts
301
331
  end
302
332
 
333
+ global = @options
334
+ if global && global.definitions.any? { |k, _| k != :help }
335
+ puts "Options:"
336
+ puts global.help_text
337
+ puts
338
+ end
339
+
303
340
  if options
304
341
  puts "Options:"
305
342
  puts options.help_text
@@ -326,7 +363,9 @@ class Hiiro
326
363
  params = r.params_string
327
364
  params_col = params ? params.ljust(max_params) : ''.ljust(max_params)
328
365
  location = shorten_location(r.location, vars)
329
- puts " #{name} #{params_col} #{type} #{location}"
366
+ hint = r.respond_to?(:opts_hint) ? r.opts_hint : nil
367
+ hint_col = hint && !hint.empty? ? " #{hint}" : ""
368
+ puts " #{name} #{params_col} #{type} #{location}#{hint_col}"
330
369
  end
331
370
  end
332
371
 
@@ -459,8 +498,13 @@ class Hiiro
459
498
  Dir.glob(pattern).map { |path| Bin.new(bin_name, path) }
460
499
  end
461
500
 
462
- def add_subcommand(name, handler, opts: nil, **values)
463
- @subcommands[name] = Subcommand.new(bin_name, name, handler, values, opts: opts)
501
+ def add_subcommand(name, handler, opts: nil, subcmd_args: [], subcmd_opts: nil, **values)
502
+ @subcommands[name] = Subcommand.new(
503
+ bin_name, name, handler, values,
504
+ opts: opts,
505
+ subcmd_args: subcmd_args,
506
+ subcmd_opts: subcmd_opts
507
+ )
464
508
  end
465
509
 
466
510
  def run_subcommand(name, *args)
@@ -545,15 +589,17 @@ class Hiiro
545
589
  end
546
590
 
547
591
  class Subcommand
548
- attr_reader :bin_name, :name, :handler, :values, :opts
592
+ attr_reader :bin_name, :name, :handler, :values, :opts, :subcmd_args, :subcmd_opts
549
593
  alias subcommand_name name
550
594
 
551
- def initialize(bin_name, name, handler, values={}, opts: nil)
595
+ def initialize(bin_name, name, handler, values={}, opts: nil, subcmd_args: [], subcmd_opts: nil)
552
596
  @bin_name = bin_name
553
597
  @name = name.to_s
554
598
  @handler = handler
555
599
  @values = values || {}
556
600
  @opts = opts
601
+ @subcmd_args = subcmd_args || []
602
+ @subcmd_opts = subcmd_opts
557
603
  end
558
604
 
559
605
  def run(*args)
@@ -586,6 +632,10 @@ class Hiiro
586
632
  end
587
633
 
588
634
  def params_string
635
+ if subcmd_args.any?
636
+ return subcmd_args.map { |a| "<#{a}>" }.join(' ')
637
+ end
638
+
589
639
  return nil unless handler.respond_to?(:parameters)
590
640
 
591
641
  params = handler.parameters
@@ -604,6 +654,10 @@ class Hiiro
604
654
  end
605
655
  }.compact.join(' ')
606
656
  end
657
+
658
+ def opts_hint
659
+ subcmd_opts&.hint
660
+ end
607
661
  end
608
662
  end
609
663
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiiro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.220
4
+ version: 0.1.221
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Toyota
@@ -128,6 +128,7 @@ files:
128
128
  - docs/h-session.md
129
129
  - docs/h-window.md
130
130
  - editboth
131
+ - examples/bin-with-proposed-options
131
132
  - examples/services.sleepers.yml
132
133
  - examples/services.yml
133
134
  - examples/sparse_groups.yml