Skip to content

Commit 33775dc

Browse files
committed
sort options by class hierarchie
1 parent 3c322cc commit 33775dc

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
22

33
gemspec
44

5-
gem 'regstry', path: '../registry'
5+
# gem 'regstry', path: '../registry'
66

77
group :test do
88
gem 'memfs'

lib/cl/opts.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def define(const, *args, &block)
1212

1313
opt = Opt.new(strs, opts, block)
1414
opt.define(const)
15-
self << opt
15+
insert(opt, const)
1616
end
1717

1818
def apply(cmd, opts)
@@ -27,10 +27,11 @@ def apply(cmd, opts)
2727
opts
2828
end
2929

30-
def <<(opt)
30+
def insert(opt, const)
3131
delete(opt)
32-
# keep the --help option at the end for help output
33-
opts.empty? ? opts << opt : opts.insert(-2, opt)
32+
return opts << opt if const == Cmd
33+
ix = opts.index(const.superclass.opts.first)
34+
opts.empty? ? opts << opt : opts.insert(ix.to_i, opt)
3435
end
3536

3637
def [](key)
@@ -45,6 +46,10 @@ def delete(opt)
4546
opts.delete(opts.detect { |o| o.strs == opt.strs })
4647
end
4748

49+
def first
50+
opts.first
51+
end
52+
4853
def to_a
4954
opts
5055
end
@@ -59,6 +64,10 @@ def deprecated
5964
map(&:deprecated).flatten.compact
6065
end
6166

67+
def ==(other)
68+
strs == other.strs
69+
end
70+
6271
def dup
6372
super.tap { |obj| obj.opts = opts.dup }
6473
end

0 commit comments

Comments
 (0)