ergane 0.1.0 → 0.3.0
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 +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +12 -0
- data/lib/ergane/argument_definition.rb +9 -1
- data/lib/ergane/command.rb +122 -16
- data/lib/ergane/concerns/inheritance.rb +3 -5
- data/lib/ergane/concerns/option_handling.rb +23 -2
- data/lib/ergane/core_ext/object.rb +6 -2
- data/lib/ergane/core_ext/option_parser.rb +18 -7
- data/lib/ergane/dsl/command_dsl.rb +20 -5
- data/lib/ergane/dsl/macros.rb +23 -0
- data/lib/ergane/errors.rb +18 -0
- data/lib/ergane/help_formatter.rb +23 -20
- data/lib/ergane/path_registry.rb +9 -6
- data/lib/ergane/runner.rb +92 -19
- data/lib/ergane/tool.rb +7 -22
- data/lib/ergane/version.rb +1 -1
- metadata +2 -3
- data/lib/ergane/util/debug.rb +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4a81aa6c11b88d07d9ca19cbf3c5b8e3e7d7eb2f1cbccdf0c80a8b5305712c1b
|
|
4
|
+
data.tar.gz: e12d81a94dea3cf2f2fd81281f3f65ff7df34b807e5f8902958b46a913af4745
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b19044df0b9834065fc8f61929b801ed7e13b0b93decfdeafb78c79c35abf38b39a5a9bcafcd1899f77b0318d8c8db70996f52aa87b9773a08b070d7321e0073
|
|
7
|
+
data.tar.gz: 41b44653cca95e1b95214fe0ee9b45252168d63123b4b64649e8c5055ca93ce61a7e6f8c960276c4ba4785c8cf40c92e5e628186bd7aa0b7c74d6ef63a33913e
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.3.0] - 2026-09-14
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- **`argument ..., raw: true`** — marks a positional whose value is literal text the CLI does not get to interpret (keystrokes, a message body, a shell fragment). A leading `-` there is data, not a mistyped flag. `Command.argument_raw?(index)` answers per slot, so a consumer rejecting stray flags can exempt exactly that argument and nothing around or past it. An index beyond the declarations is never raw.
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
- **Re-declaring an `argument` now replaces it in place instead of appending a second copy.** `option` was always keyed by name; `argument` pushed onto an Array, so any second evaluation of a command's class body (a Zeitwerk reload, a `load` in a spec) doubled every argument list. Because positionals are matched by index, `[name, keys]` became `[name, keys, name, keys]` and index 2 — a `required: true` argument — could not be filled by any two-word invocation, so `input sess -abc` reported `Missing required argument: <name>` with `<name>` plainly supplied. Declaration order of the first appearance is preserved.
|
|
12
|
+
|
|
13
|
+
## [0.2.0] - 2026-05-25
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
- **Unknown subcommands on a command group now raise `CommandNotFound`** (with a did-you-mean suggestion) and exit non-zero, instead of silently printing help. Scoped to command groups — leaf commands still treat unmatched tokens as positional arguments.
|
|
17
|
+
- **Positional `argument` declarations are now enforced.** Required-ness is derived from the command's `run` signature — a required parameter (`run(name)`) makes the argument required, while an optional one (`run(name = nil)`) or a splat (`run(*)`) makes it optional; an explicit `required:` on the `argument` overrides the signature. A missing required argument raises `MissingArgument`; an absent optional argument takes its `default:`; values are coerced to the declared `type:` (`String` is identity, `Integer`/`Float` via Kernel conversion raising `InvalidOption` on bad input). Previously these keywords affected only help text.
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
- `Ergane::DSL::Macros.dsl_value` — a class-level getter/setter accessor generator used by the DSL.
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
- `PathRegistry#abbreviate` now expands its input before matching (mirroring `#register`), so abbreviation is consistent across platforms (notably Windows, where un-expanded inputs failed to match drive-qualified prefixes) and accepts `~`-relative input.
|
|
24
|
+
- `OptionParser#order_recognized!` no longer drops the trailing tokens of a multi-token unknown option, and preserves argument order.
|
|
25
|
+
- `String#blank?` is guarded against external definitions (e.g. ActiveSupport) instead of unconditionally overriding them.
|
|
26
|
+
- Tool-rooted abstract intermediate commands are no longer stranded in the tool's registry when marked abstract.
|
|
27
|
+
|
|
28
|
+
### Internal
|
|
29
|
+
- Command registration unified into a single `register!` path (removed the duplicate `define_singleton_method`'d `inherited`/`inherited_command_name_set` hooks).
|
|
30
|
+
- `HelpFormatter` renders through a shared `section` helper with a per-render color cycler (no module-level mutable state).
|
|
31
|
+
- `Util::Debug` is no longer packaged in the gem (dev-only tooling).
|
|
32
|
+
|
|
5
33
|
## [0.1.0] - 2026-05-25
|
|
6
34
|
|
|
7
35
|
First release of the rewritten framework. A near-complete rewrite of the
|
data/README.md
CHANGED
|
@@ -174,6 +174,18 @@ run do |*args|
|
|
|
174
174
|
end
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
+
### Positional Arguments
|
|
178
|
+
|
|
179
|
+
An argument's required-ness is derived from the command's `run` signature — a required parameter makes it required, while an optional parameter or a splat makes it optional:
|
|
180
|
+
|
|
181
|
+
```ruby
|
|
182
|
+
def run(source, destination = nil, *rest)
|
|
183
|
+
# source is required; destination is optional
|
|
184
|
+
end
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Pass `required:` on the `argument` to override the signature, `type:` to coerce the value (a non-`String` type is converted, raising on failure), and `default:` for an absent optional argument. A missing required argument raises `Ergane::MissingArgument`.
|
|
188
|
+
|
|
177
189
|
## Loading Commands from Files
|
|
178
190
|
|
|
179
191
|
For larger CLIs, organize commands in separate files:
|
|
@@ -4,7 +4,15 @@ module Ergane
|
|
|
4
4
|
class ArgumentDefinition
|
|
5
5
|
attr_reader :name, :type, :description, :required, :default
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
# +required+ defaults to nil, meaning "derive from the run signature";
|
|
8
|
+
# pass true/false to force it.
|
|
9
|
+
#
|
|
10
|
+
# There is deliberately no way to declare an argument "literal" here. A positional whose
|
|
11
|
+
# value may begin with a dash is handled by the POSIX `--` separator at the point of USE
|
|
12
|
+
# (see Concerns::OptionHandling#parse_options), because the caller knows and a declaration
|
|
13
|
+
# only guesses -- and a declaration exempts the argument forever, including from the typos
|
|
14
|
+
# the check exists to catch.
|
|
15
|
+
def initialize(name, type = String, description: nil, required: nil, default: nil)
|
|
8
16
|
@name = name.to_sym
|
|
9
17
|
@type = type
|
|
10
18
|
@description = description
|
data/lib/ergane/command.rb
CHANGED
|
@@ -11,14 +11,7 @@ module Ergane
|
|
|
11
11
|
class << self
|
|
12
12
|
def command_name=(name)
|
|
13
13
|
@command_name = name&.to_sym
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
parent = superclass
|
|
17
|
-
if parent.respond_to?(:tool) && parent.abstract_class?
|
|
18
|
-
parent.inherited_command_name_set(self)
|
|
19
|
-
else
|
|
20
|
-
register_subcommand(self)
|
|
21
|
-
end
|
|
14
|
+
register!
|
|
22
15
|
end
|
|
23
16
|
|
|
24
17
|
def command_name
|
|
@@ -33,16 +26,60 @@ module Ergane
|
|
|
33
26
|
@subcommands ||= {}
|
|
34
27
|
end
|
|
35
28
|
|
|
29
|
+
# Mark THIS command as its parent's default subcommand. When the
|
|
30
|
+
# parent is invoked with no positional token, the runner falls
|
|
31
|
+
# through to here (recursively — the default itself may have a
|
|
32
|
+
# default). Multiple defaults under one parent raise
|
|
33
|
+
# AmbiguousDefault at lookup time.
|
|
34
|
+
def default!
|
|
35
|
+
@is_default = true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def default? = @is_default == true
|
|
39
|
+
|
|
40
|
+
# Parent-side: the child marked `default!`, or nil if none. Raises
|
|
41
|
+
# AmbiguousDefault if more than one child claims the slot — the
|
|
42
|
+
# error surfaces at first lookup (CLI startup), which is the right
|
|
43
|
+
# time to learn the registry is inconsistent.
|
|
44
|
+
def default_subcommand
|
|
45
|
+
defaults = subcommands.each_value.select(&:default?)
|
|
46
|
+
if defaults.size > 1
|
|
47
|
+
raise AmbiguousDefault, "#{command_name || self} has multiple defaults: " \
|
|
48
|
+
"#{defaults.map(&:command_name).join(', ')}"
|
|
49
|
+
end
|
|
50
|
+
defaults.first
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Effective required-ness of the positional argument at +index+. An
|
|
54
|
+
# explicit DSL `required:` (true/false) wins; otherwise it's derived from
|
|
55
|
+
# the run method's matching positional parameter: a required parameter
|
|
56
|
+
# (`run(name)`) means required, while an optional one (`run(name = nil)`)
|
|
57
|
+
# or a splat (`run(*)`) means optional.
|
|
58
|
+
def argument_required?(index)
|
|
59
|
+
defn = argument_definitions[index]
|
|
60
|
+
return false unless defn
|
|
61
|
+
return defn.required unless defn.required.nil?
|
|
62
|
+
|
|
63
|
+
param = run_positional_parameters[index]
|
|
64
|
+
param ? param.first == :req : false
|
|
65
|
+
end
|
|
66
|
+
|
|
36
67
|
def inherited(subclass)
|
|
37
68
|
super
|
|
38
69
|
subclass.instance_variable_set(:@option_definitions, option_definitions.dup)
|
|
39
70
|
subclass.instance_variable_set(:@argument_definitions, argument_definitions.dup)
|
|
40
71
|
subclass.instance_variable_set(:@subcommands, {})
|
|
41
|
-
|
|
72
|
+
subclass.send(:register!)
|
|
42
73
|
end
|
|
43
74
|
|
|
44
75
|
private
|
|
45
76
|
|
|
77
|
+
# The run method's positional parameters (required/optional), in order,
|
|
78
|
+
# which line up with declared arguments. Excludes splat/keyword params.
|
|
79
|
+
def run_positional_parameters
|
|
80
|
+
instance_method(:run).parameters.select { |kind, _| kind == :req || kind == :opt }
|
|
81
|
+
end
|
|
82
|
+
|
|
46
83
|
def derive_command_name
|
|
47
84
|
return nil if self == Command || abstract_class?
|
|
48
85
|
base = name&.demodulize
|
|
@@ -50,12 +87,36 @@ module Ergane
|
|
|
50
87
|
base.sub(/Command$/, "").underscore.to_sym
|
|
51
88
|
end
|
|
52
89
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
parent
|
|
90
|
+
# The registry this command belongs in: a tool's abstract command base
|
|
91
|
+
# registers under the tool itself; a concrete parent registers under
|
|
92
|
+
# that parent. A command rooted directly on Command, or under an
|
|
93
|
+
# abstract non-tool parent, registers nowhere.
|
|
94
|
+
def registration_target
|
|
95
|
+
parent = superclass
|
|
96
|
+
if parent.respond_to?(:tool) && parent.abstract_class?
|
|
97
|
+
parent.tool
|
|
98
|
+
elsif parent != Command && !parent.abstract_class?
|
|
99
|
+
parent
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Registers (or re-registers) this command in its target registry under
|
|
104
|
+
# its current command_name, removing any prior registration when the
|
|
105
|
+
# name changes or the command becomes abstract. Idempotent — safe to
|
|
106
|
+
# call from both .inherited and command_name=.
|
|
107
|
+
def register!
|
|
108
|
+
target = registration_target
|
|
109
|
+
return unless target
|
|
110
|
+
|
|
111
|
+
name = abstract_class? ? nil : command_name
|
|
112
|
+
|
|
113
|
+
target.subcommands.delete(@registered_as) if @registered_as && @registered_as != name
|
|
114
|
+
if name
|
|
115
|
+
target.subcommands[name] = self
|
|
116
|
+
@registered_as = name
|
|
117
|
+
else
|
|
118
|
+
@registered_as = nil
|
|
119
|
+
end
|
|
59
120
|
end
|
|
60
121
|
end
|
|
61
122
|
|
|
@@ -67,13 +128,21 @@ module Ergane
|
|
|
67
128
|
|
|
68
129
|
def initialize(argv = [])
|
|
69
130
|
@options = self.class.build_default_options
|
|
70
|
-
@argv = parse_options(argv.dup)
|
|
131
|
+
@argv = process_arguments(parse_options(argv.dup))
|
|
71
132
|
end
|
|
72
133
|
|
|
73
134
|
def args
|
|
74
135
|
@argv
|
|
75
136
|
end
|
|
76
137
|
|
|
138
|
+
# Was the positional at +index+ given after a `--` separator, marking it literal text
|
|
139
|
+
# this CLI does not get to interpret? Per INVOCATION, stated by the caller who knows --
|
|
140
|
+
# unlike a declaration, which has to guess in advance which arguments might someday
|
|
141
|
+
# receive dash-leading text and then exempts them forever, typos included.
|
|
142
|
+
def literal_argument?(index)
|
|
143
|
+
!@literal_from.nil? && index >= @literal_from
|
|
144
|
+
end
|
|
145
|
+
|
|
77
146
|
def run(*run_args)
|
|
78
147
|
if self.class.subcommands.any?
|
|
79
148
|
$stdout.puts HelpFormatter.new(self.class).format
|
|
@@ -81,5 +150,42 @@ module Ergane
|
|
|
81
150
|
raise AbstractCommand, "#{self.class.name}#run is not implemented"
|
|
82
151
|
end
|
|
83
152
|
end
|
|
153
|
+
|
|
154
|
+
private
|
|
155
|
+
|
|
156
|
+
# Validates and coerces positional args against the command's argument
|
|
157
|
+
# definitions: missing required args raise, absent optional args take
|
|
158
|
+
# their default, and present args are coerced by type. Extra positionals
|
|
159
|
+
# beyond the declared arguments pass through untouched (e.g. for run(*)).
|
|
160
|
+
def process_arguments(argv)
|
|
161
|
+
definitions = self.class.argument_definitions
|
|
162
|
+
return argv if definitions.empty?
|
|
163
|
+
|
|
164
|
+
declared = definitions.each_with_index.map do |defn, i|
|
|
165
|
+
if i < argv.length
|
|
166
|
+
coerce_argument(argv[i], defn)
|
|
167
|
+
elsif self.class.argument_required?(i)
|
|
168
|
+
raise MissingArgument, "Missing required argument: <#{defn.name}>"
|
|
169
|
+
else
|
|
170
|
+
defn.default
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
declared + argv.drop(definitions.length)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def coerce_argument(value, defn)
|
|
177
|
+
type = defn.type
|
|
178
|
+
return value if type.nil? || type == String
|
|
179
|
+
|
|
180
|
+
if type == Integer
|
|
181
|
+
Integer(value)
|
|
182
|
+
elsif type == Float
|
|
183
|
+
Float(value)
|
|
184
|
+
else
|
|
185
|
+
value
|
|
186
|
+
end
|
|
187
|
+
rescue ArgumentError
|
|
188
|
+
raise InvalidOption, "Invalid value for <#{defn.name}>: #{value.inspect} (expected #{type})"
|
|
189
|
+
end
|
|
84
190
|
end
|
|
85
191
|
end
|
|
@@ -10,11 +10,9 @@ module Ergane
|
|
|
10
10
|
module ClassMethods
|
|
11
11
|
def abstract_class=(value)
|
|
12
12
|
@abstract_class = value
|
|
13
|
-
#
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
parent.subcommands.delete(command_name) if parent.respond_to?(:subcommands)
|
|
17
|
-
end
|
|
13
|
+
# Re-run registration so the command leaves (or rejoins) its
|
|
14
|
+
# registry to match its new abstract state.
|
|
15
|
+
register! if self < Ergane::Command && respond_to?(:register!, true)
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
def abstract_class?
|
|
@@ -33,10 +33,31 @@ module Ergane
|
|
|
33
33
|
|
|
34
34
|
private
|
|
35
35
|
|
|
36
|
+
# `--` ENDS OPTION PARSING. Everything after it is literal text.
|
|
37
|
+
#
|
|
38
|
+
# This is the POSIX convention every CLI already implements, and it is the answer to
|
|
39
|
+
# "this positional may legitimately begin with a dash" -- `send <name> -- -v` means the
|
|
40
|
+
# two characters, not a flag. It is stated PER INVOCATION by the person who knows, which
|
|
41
|
+
# is why it beats any per-command or per-argument declaration: a declaration has to guess
|
|
42
|
+
# in advance which arguments might someday receive dash-leading text, and it exempts them
|
|
43
|
+
# forever, including from the typos it was supposed to catch.
|
|
44
|
+
#
|
|
45
|
+
# `order_recognized!` deliberately leaves unrecognized tokens alone, `--` among them, so
|
|
46
|
+
# it is consumed here. The index is remembered rather than the token kept: positionals are
|
|
47
|
+
# matched by index, and a `--` left in the list would occupy an argument's slot.
|
|
36
48
|
def parse_options(argv)
|
|
49
|
+
sep = argv.index('--')
|
|
50
|
+
literal = sep ? argv[(sep + 1)..] || [] : []
|
|
51
|
+
head = sep ? argv[0...sep] : argv
|
|
52
|
+
|
|
37
53
|
parser = self.class.build_option_parser(@options)
|
|
38
|
-
parser.order_recognized!(
|
|
39
|
-
|
|
54
|
+
parser.order_recognized!(head)
|
|
55
|
+
|
|
56
|
+
# Split BEFORE parsing rather than after: the separator's job is to end option
|
|
57
|
+
# parsing, so the tail must never be offered to the parser at all. Index is taken
|
|
58
|
+
# from where the parsed head ends, because option removal shifts positions.
|
|
59
|
+
@literal_from = head.length if sep
|
|
60
|
+
head.concat(literal)
|
|
40
61
|
end
|
|
41
62
|
end
|
|
42
63
|
end
|
|
@@ -32,10 +32,14 @@ class TrueClass
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
class String
|
|
35
|
-
#
|
|
35
|
+
# Catch whitespace-only strings, overriding the inherited Object#blank?.
|
|
36
|
+
# Guarded on String's OWN methods — not method_defined?, which would see the
|
|
37
|
+
# inherited Object#blank? and skip, leaving the whitespace-blind version. This
|
|
38
|
+
# still defines our version normally, but yields to any external String#blank?
|
|
39
|
+
# (e.g. ActiveSupport) rather than clobbering it.
|
|
36
40
|
def blank?
|
|
37
41
|
empty? || /\A[[:space:]]*\z/.match?(self)
|
|
38
|
-
end
|
|
42
|
+
end unless instance_methods(false).include?(:blank?)
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
class Numeric
|
|
@@ -3,14 +3,25 @@
|
|
|
3
3
|
class OptionParser
|
|
4
4
|
# Like order!, but leave any unrecognized --switches alone
|
|
5
5
|
# instead of raising InvalidOption.
|
|
6
|
+
#
|
|
7
|
+
# `order!` STOPS at a `--` separator, consuming it and leaving everything after it in
|
|
8
|
+
# +args+ as operands. The original version of this method ended in
|
|
9
|
+
# `args.replace(leftover)` where +leftover+ held only the non-options `order!` had yielded
|
|
10
|
+
# -- so that untouched remainder was thrown away, and every operand after a `--` vanished
|
|
11
|
+
# silently. `send a -- -v` parsed as `send a`.
|
|
12
|
+
#
|
|
13
|
+
# Discovered 2026-09-14 while making `--` the way a caller passes dash-leading text.
|
|
6
14
|
def order_recognized!(args)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
leftover = []
|
|
16
|
+
until args.empty?
|
|
17
|
+
begin
|
|
18
|
+
order!(args) { |nonopt| leftover << nonopt }
|
|
19
|
+
break
|
|
20
|
+
rescue OptionParser::InvalidOption => e
|
|
21
|
+
leftover.concat(e.args)
|
|
22
|
+
end
|
|
13
23
|
end
|
|
14
|
-
|
|
24
|
+
# Whatever order! stopped in front of (the tail after a `--`) is still operands.
|
|
25
|
+
args.replace(leftover.concat(args))
|
|
15
26
|
end
|
|
16
27
|
end
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
module Ergane
|
|
4
4
|
module DSL
|
|
5
5
|
module CommandDSL
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
extend Macros
|
|
7
|
+
|
|
8
|
+
dsl_value :description, default: ""
|
|
9
9
|
|
|
10
10
|
def aliases(*names)
|
|
11
11
|
names.any? ? (@aliases = names.flatten.map(&:to_sym)) : (@aliases || [])
|
|
@@ -22,10 +22,25 @@ module Ergane
|
|
|
22
22
|
option(name, nil, short: short, description: description, default: false)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
# Declaring the same argument twice REPLACES it in place rather than
|
|
26
|
+
# appending a second copy. `option` has always been keyed by name (it
|
|
27
|
+
# writes into a Hash); `argument` pushed onto an Array, so any second
|
|
28
|
+
# evaluation of a command's class body -- a Zeitwerk reload, a `load` in a
|
|
29
|
+
# spec -- silently doubled every argument list.
|
|
30
|
+
#
|
|
31
|
+
# That is not cosmetic. Positionals are matched BY INDEX, so a doubled
|
|
32
|
+
# [name, keys] becomes [name, keys, name, keys] and index 2 is a `required:
|
|
33
|
+
# true` argument that no two-word invocation can ever fill: `input sess -abc`
|
|
34
|
+
# started reporting "Missing required argument: <name>" with <name> plainly
|
|
35
|
+
# supplied. It surfaced as an unreproducible test flake (2026-09-14) because
|
|
36
|
+
# it depends on whether something reloaded a command file first.
|
|
37
|
+
def argument(name, type = String, description: nil, required: nil, default: nil)
|
|
38
|
+
defn = ArgumentDefinition.new(
|
|
27
39
|
name, type, description: description, required: required, default: default
|
|
28
40
|
)
|
|
41
|
+
existing = argument_definitions.index { |d| d.name == defn.name }
|
|
42
|
+
existing ? argument_definitions[existing] = defn : argument_definitions << defn
|
|
43
|
+
defn
|
|
29
44
|
end
|
|
30
45
|
|
|
31
46
|
def command(name, aliases: [], &block)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ergane
|
|
4
|
+
module DSL
|
|
5
|
+
# Helpers for defining DSL methods on a host class/module.
|
|
6
|
+
module Macros
|
|
7
|
+
# Defines a class-level "value" accessor with combined getter/setter
|
|
8
|
+
# semantics: called with a truthy argument it stores and returns it;
|
|
9
|
+
# called with none (or a falsy value) it returns the stored value, or
|
|
10
|
+
# +default+ if unset.
|
|
11
|
+
#
|
|
12
|
+
# dsl_value :description, default: ""
|
|
13
|
+
# description "Deploy" # => "Deploy" (and stored)
|
|
14
|
+
# description # => "Deploy"
|
|
15
|
+
def dsl_value(name, default: nil)
|
|
16
|
+
ivar = "@#{name}"
|
|
17
|
+
define_method(name) do |value = nil|
|
|
18
|
+
value ? instance_variable_set(ivar, value) : (instance_variable_get(ivar) || default)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/ergane/errors.rb
CHANGED
|
@@ -48,4 +48,22 @@ module Ergane
|
|
|
48
48
|
class MissingArgument < Error; end
|
|
49
49
|
class InvalidOption < Error; end
|
|
50
50
|
class AbstractCommand < Error; end
|
|
51
|
+
|
|
52
|
+
# Two sibling subcommands both called `default!` — registry is
|
|
53
|
+
# inconsistent. Raised at first lookup, not at registration, so command
|
|
54
|
+
# files can load in any order.
|
|
55
|
+
class AmbiguousDefault < Error; end
|
|
56
|
+
|
|
57
|
+
# A promoted token matches grandchildren under more than one
|
|
58
|
+
# `promote_subcommands!`-flagged parent — the user has to disambiguate
|
|
59
|
+
# (e.g. `claudepilot start` could mean `daemon start` or `session start`).
|
|
60
|
+
class AmbiguousCommand < Error
|
|
61
|
+
attr_reader :token, :candidates
|
|
62
|
+
|
|
63
|
+
def initialize(token, candidates:)
|
|
64
|
+
@token = token
|
|
65
|
+
@candidates = candidates # array of "parent subcommand" strings
|
|
66
|
+
super("Ambiguous command: '#{token}' could be: #{candidates.join(', ')}")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
51
69
|
end
|
|
@@ -37,8 +37,8 @@ module Ergane
|
|
|
37
37
|
usage = path.light_red
|
|
38
38
|
usage += " [options]".light_cyan if command_class.option_definitions.any?
|
|
39
39
|
usage += " [subcommand]".light_black.underline if command_class.subcommands.any?
|
|
40
|
-
command_class.argument_definitions.
|
|
41
|
-
label =
|
|
40
|
+
command_class.argument_definitions.each_with_index do |arg, i|
|
|
41
|
+
label = command_class.argument_required?(i) ? "<#{arg.name}>" : "[#{arg.name}]"
|
|
42
42
|
usage += " #{label}".light_yellow
|
|
43
43
|
end
|
|
44
44
|
"Usage:".light_cyan + " " + usage
|
|
@@ -49,21 +49,18 @@ module Ergane
|
|
|
49
49
|
return if subs.empty?
|
|
50
50
|
|
|
51
51
|
max_width = subs.keys.map { |k| k.to_s.length }.max
|
|
52
|
-
Util::Formatting.
|
|
53
|
-
|
|
54
|
-
lines = []
|
|
55
|
-
lines << "Subcommands:".light_cyan
|
|
56
|
-
header_len = lines.last.uncolorize.length
|
|
57
|
-
lines << (" \u250C" + ("\u2500" * (header_len - 2)) + "\u2518").light_black
|
|
52
|
+
colors = Util::Formatting::COLORS.cycle
|
|
53
|
+
title = "Subcommands"
|
|
58
54
|
|
|
55
|
+
lines = [(" \u250C" + ("\u2500" * (title.length - 1)) + "\u2518").light_black]
|
|
59
56
|
subs.each do |name, sub_class|
|
|
60
57
|
label = name.to_s.ljust(max_width + 2)
|
|
61
58
|
desc = sub_class.description.present? ? sub_class.description.light_black : ""
|
|
62
|
-
lines << " \u251C\u2500\u2510".light_black + " #{label.send(
|
|
59
|
+
lines << " \u251C\u2500\u2510".light_black + " #{label.send(colors.next)} #{desc}"
|
|
63
60
|
end
|
|
64
|
-
|
|
65
61
|
lines << (" \u2514" + "\u2500" * 40).light_black
|
|
66
|
-
|
|
62
|
+
|
|
63
|
+
section(title, lines)
|
|
67
64
|
end
|
|
68
65
|
|
|
69
66
|
def options_section
|
|
@@ -72,14 +69,13 @@ module Ergane
|
|
|
72
69
|
|
|
73
70
|
max_width = opts.values.map { |o| o.signature.length }.max
|
|
74
71
|
|
|
75
|
-
lines =
|
|
76
|
-
opts.each_value do |opt|
|
|
72
|
+
lines = opts.each_value.map do |opt|
|
|
77
73
|
sig = opt.signature.ljust(max_width + 2)
|
|
78
74
|
desc = opt.description || ""
|
|
79
75
|
default_note = opt.default_value ? " (default: #{opt.default_value})".light_black : ""
|
|
80
|
-
|
|
76
|
+
" #{sig.light_green} #{desc}#{default_note}"
|
|
81
77
|
end
|
|
82
|
-
|
|
78
|
+
section("Options", lines)
|
|
83
79
|
end
|
|
84
80
|
|
|
85
81
|
def arguments_section
|
|
@@ -88,14 +84,21 @@ module Ergane
|
|
|
88
84
|
|
|
89
85
|
max_width = args.map { |a| a.name.to_s.length }.max
|
|
90
86
|
|
|
91
|
-
lines =
|
|
92
|
-
args.each do |arg|
|
|
87
|
+
lines = args.each_with_index.map do |arg, i|
|
|
93
88
|
label = arg.name.to_s.ljust(max_width + 2)
|
|
94
89
|
desc = arg.description || ""
|
|
95
|
-
req =
|
|
96
|
-
|
|
90
|
+
req = command_class.argument_required?(i) ? " (required)".light_red : " (optional)".light_black
|
|
91
|
+
" #{label.light_yellow} #{desc}#{req}"
|
|
97
92
|
end
|
|
98
|
-
|
|
93
|
+
section("Arguments", lines)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Renders a titled block: a cyan "Title:" header followed by its lines,
|
|
97
|
+
# or nil when there are no lines (so #format compacts it away).
|
|
98
|
+
def section(title, lines)
|
|
99
|
+
return if lines.empty?
|
|
100
|
+
|
|
101
|
+
["#{title}:".light_cyan, *lines].join("\n")
|
|
99
102
|
end
|
|
100
103
|
end
|
|
101
104
|
end
|
data/lib/ergane/path_registry.rb
CHANGED
|
@@ -32,16 +32,19 @@ module Ergane
|
|
|
32
32
|
self
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
# Collapse the longest matching prefix in +path+ to its label,
|
|
36
|
-
#
|
|
35
|
+
# Collapse the longest matching prefix in +path+ to its label, returning
|
|
36
|
+
# the path unchanged when nothing matches. The input is expanded before
|
|
37
|
+
# matching (mirroring how prefixes are stored on #register), so matching
|
|
38
|
+
# is consistent across platforms and "~"-relative input is accepted.
|
|
37
39
|
def abbreviate(path)
|
|
38
|
-
|
|
40
|
+
original = path.to_s
|
|
41
|
+
expanded = File.expand_path(original)
|
|
39
42
|
best = @substitutions
|
|
40
|
-
.select { |sub|
|
|
43
|
+
.select { |sub| expanded == sub.prefix || expanded.start_with?("#{sub.prefix}/") }
|
|
41
44
|
.max_by { |sub| sub.prefix.length }
|
|
42
|
-
return
|
|
45
|
+
return original unless best
|
|
43
46
|
|
|
44
|
-
"#{best.label}#{
|
|
47
|
+
"#{best.label}#{expanded[best.prefix.length..]}"
|
|
45
48
|
end
|
|
46
49
|
end
|
|
47
50
|
end
|
data/lib/ergane/runner.rb
CHANGED
|
@@ -9,6 +9,29 @@ module Ergane
|
|
|
9
9
|
@argv = argv.dup
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
# ONE DEFINITION OF "IS THIS A HELP / VERSION REQUEST", because there were three.
|
|
13
|
+
#
|
|
14
|
+
# Measured on claudepilot 2026-09-14, all disagreeing:
|
|
15
|
+
#
|
|
16
|
+
# bin/claudepilot.rb %w[--help --version -h -v] (its own literal list)
|
|
17
|
+
# Runner#help_requested? --help, -h
|
|
18
|
+
# Runner#version_requested? --version, -V (no -v)
|
|
19
|
+
#
|
|
20
|
+
# So `-v` passed the consumer's list, was NOT recognised here, walked the default chain to
|
|
21
|
+
# a leaf with no version DSL, and landed in OptionParser's BUILT-IN --version officer,
|
|
22
|
+
# which aborts "version unknown". And `-V`, recognised here, was not in the consumer's list,
|
|
23
|
+
# so the consumer rewrote it to `launch -V` and the stray-flag guard rejected it. Both short
|
|
24
|
+
# forms broken, in opposite directions, from the same cause.
|
|
25
|
+
#
|
|
26
|
+
# `-v` is included because OptionParser claims it for version by default: leaving it out
|
|
27
|
+
# does not free the letter, it just means something else answers and gets it wrong. A tool
|
|
28
|
+
# that wants `-v` for verbose must declare that option, which takes precedence.
|
|
29
|
+
HELP_FLAGS = %w[--help -h].freeze
|
|
30
|
+
VERSION_FLAGS = %w[--version -v -V].freeze
|
|
31
|
+
|
|
32
|
+
def self.help_requested?(args) = args.any? { |a| HELP_FLAGS.include?(a) }
|
|
33
|
+
def self.version_requested?(args) = args.any? { |a| VERSION_FLAGS.include?(a) }
|
|
34
|
+
|
|
12
35
|
def execute
|
|
13
36
|
command_class, remaining, path = resolve(root, argv)
|
|
14
37
|
|
|
@@ -22,39 +45,89 @@ module Ergane
|
|
|
22
45
|
return
|
|
23
46
|
end
|
|
24
47
|
|
|
25
|
-
|
|
48
|
+
# MissingArgument from the constructor (a leaf was reached but the
|
|
49
|
+
# required positional wasn't supplied) → show that leaf's help instead
|
|
50
|
+
# of failing with a bare error. Matches the principle that "the user
|
|
51
|
+
# didn't tell us enough" is always answered with "here's what I need."
|
|
52
|
+
begin
|
|
53
|
+
instance = command_class.new(remaining)
|
|
54
|
+
rescue MissingArgument
|
|
55
|
+
$stdout.puts HelpFormatter.new(command_class, command_path: path).format
|
|
56
|
+
return
|
|
57
|
+
end
|
|
58
|
+
|
|
26
59
|
instance.run(*instance.args)
|
|
27
60
|
end
|
|
28
61
|
|
|
29
62
|
private
|
|
30
63
|
|
|
31
|
-
def help_requested?(args)
|
|
32
|
-
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def version_requested?(args)
|
|
36
|
-
args.include?("--version") || args.include?("-V")
|
|
37
|
-
end
|
|
64
|
+
def help_requested?(args) = Runner.help_requested?(args)
|
|
65
|
+
def version_requested?(args) = Runner.version_requested?(args)
|
|
38
66
|
|
|
39
67
|
def resolve(command_class, args, path = [])
|
|
40
68
|
path << (command_class.command_name || command_class.name || "command").to_s
|
|
41
|
-
return [command_class, args, path] if args.empty?
|
|
42
69
|
|
|
43
|
-
token
|
|
44
|
-
|
|
70
|
+
# Arg-driven descent: the leading non-flag token (if any) names a
|
|
71
|
+
# subcommand. Promotion (find_subcommand below) lets a token match
|
|
72
|
+
# a grandchild under a `promote_subcommands!`-flagged parent.
|
|
73
|
+
unless args.empty? || args.first.start_with?("-")
|
|
74
|
+
token = args.first
|
|
75
|
+
sub = find_subcommand(command_class, token.to_sym)
|
|
76
|
+
if sub
|
|
77
|
+
args.shift
|
|
78
|
+
return resolve(sub, args, path)
|
|
79
|
+
elsif command_class.subcommands.any?
|
|
80
|
+
# Group with no match — bad token, not a positional for a leaf.
|
|
81
|
+
raise CommandNotFound.new(token, available: command_class.subcommands.keys)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
45
84
|
|
|
46
|
-
|
|
47
|
-
if
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
85
|
+
# `--help` and `--version` short-circuit the default fallthrough — the
|
|
86
|
+
# user navigated to THIS level explicitly (root, if they typed only the
|
|
87
|
+
# flag), and they want THIS level's help/version. Without this, a bare
|
|
88
|
+
# `tool --help`/`tool --version` would walk the default chain (e.g.
|
|
89
|
+
# session→resume) to a leaf: `--help` would show the leaf's help instead
|
|
90
|
+
# of the root command listing, and `--version` would reach a leaf with no
|
|
91
|
+
# version DSL, skip execute's version branch, and fall into OptionParser's
|
|
92
|
+
# built-in `--version` handler (which aborts "version unknown").
|
|
93
|
+
return [command_class, args, path] if help_requested?(args) || version_requested?(args)
|
|
94
|
+
|
|
95
|
+
# Default fallthrough: no token to consume, but a child claimed the
|
|
96
|
+
# default slot — keep walking. Recursive: a default may itself have
|
|
97
|
+
# a default. Same recursion handles both descents.
|
|
98
|
+
if (default_sub = command_class.default_subcommand)
|
|
99
|
+
return resolve(default_sub, args, path)
|
|
52
100
|
end
|
|
101
|
+
|
|
102
|
+
[command_class, args, path]
|
|
53
103
|
end
|
|
54
104
|
|
|
55
105
|
def find_subcommand(command_class, token)
|
|
56
|
-
command_class.subcommands[token]
|
|
57
|
-
|
|
106
|
+
direct = command_class.subcommands[token]
|
|
107
|
+
return direct if direct
|
|
108
|
+
|
|
109
|
+
via_term = command_class.subcommands.each_value.find { |c| c.terms.include?(token) }
|
|
110
|
+
return via_term if via_term
|
|
111
|
+
|
|
112
|
+
# Promotion: search one level into any direct child marked
|
|
113
|
+
# `promote_subcommands!`. Collect all hits across promoting children;
|
|
114
|
+
# zero → nil (caller handles), one → the grandchild, multiple →
|
|
115
|
+
# AmbiguousCommand (force the user to disambiguate).
|
|
116
|
+
promoted = command_class.subcommands.each_value
|
|
117
|
+
.select { |c| c.respond_to?(:promote_subcommands?) && c.promote_subcommands? }
|
|
118
|
+
.flat_map do |c|
|
|
119
|
+
c.subcommands.each_value.select { |gc| gc.terms.include?(token) }.map { |gc| [c, gc] }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
return nil if promoted.empty?
|
|
123
|
+
|
|
124
|
+
if promoted.size > 1
|
|
125
|
+
raise AmbiguousCommand.new(token, candidates: promoted.map { |parent, gc|
|
|
126
|
+
"#{parent.command_name} #{gc.command_name}"
|
|
127
|
+
})
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
promoted.first.last
|
|
58
131
|
end
|
|
59
132
|
end
|
|
60
133
|
end
|
data/lib/ergane/tool.rb
CHANGED
|
@@ -5,6 +5,10 @@ module Ergane
|
|
|
5
5
|
self.abstract_class = true
|
|
6
6
|
|
|
7
7
|
class << self
|
|
8
|
+
extend DSL::Macros
|
|
9
|
+
|
|
10
|
+
dsl_value :version
|
|
11
|
+
|
|
8
12
|
def command_class(klass = nil)
|
|
9
13
|
if klass
|
|
10
14
|
@command_class = klass
|
|
@@ -18,10 +22,6 @@ module Ergane
|
|
|
18
22
|
name ? (self.command_name = name) : command_name
|
|
19
23
|
end
|
|
20
24
|
|
|
21
|
-
def version(ver = nil)
|
|
22
|
-
ver ? (@version = ver) : @version
|
|
23
|
-
end
|
|
24
|
-
|
|
25
25
|
def start(argv = ARGV)
|
|
26
26
|
Runner.new(self, argv.dup).execute
|
|
27
27
|
rescue Interrupt
|
|
@@ -58,27 +58,12 @@ module Ergane
|
|
|
58
58
|
tool_subclass.command_class(base)
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
+
# Gives the tool's command base (and everything below it) a reference
|
|
62
|
+
# back to the tool, so Command#registration_target can route
|
|
63
|
+
# subcommands to the tool's registry.
|
|
61
64
|
def wire_command_class(klass)
|
|
62
65
|
tool = self
|
|
63
66
|
klass.define_singleton_method(:tool) { tool }
|
|
64
|
-
|
|
65
|
-
klass.define_singleton_method(:inherited) do |subclass|
|
|
66
|
-
super(subclass)
|
|
67
|
-
cmd_name = subclass.command_name
|
|
68
|
-
if cmd_name && !subclass.abstract_class? && subclass.superclass.abstract_class?
|
|
69
|
-
subclass.instance_variable_set(:@_derived_name, cmd_name)
|
|
70
|
-
tool.subcommands[cmd_name] = subclass
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
klass.define_singleton_method(:inherited_command_name_set) do |subclass|
|
|
75
|
-
cmd_name = subclass.command_name
|
|
76
|
-
if cmd_name && !subclass.abstract_class? && subclass.superclass.abstract_class?
|
|
77
|
-
derived = subclass.instance_variable_get(:@_derived_name)
|
|
78
|
-
tool.subcommands.delete(derived) if derived && derived != cmd_name
|
|
79
|
-
tool.subcommands[cmd_name] = subclass
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
67
|
end
|
|
83
68
|
end
|
|
84
69
|
end
|
data/lib/ergane/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ergane
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dale Stevens
|
|
@@ -160,6 +160,7 @@ files:
|
|
|
160
160
|
- lib/ergane/core_ext/string.rb
|
|
161
161
|
- lib/ergane/dsl/block_dsl.rb
|
|
162
162
|
- lib/ergane/dsl/command_dsl.rb
|
|
163
|
+
- lib/ergane/dsl/macros.rb
|
|
163
164
|
- lib/ergane/errors.rb
|
|
164
165
|
- lib/ergane/formatter.rb
|
|
165
166
|
- lib/ergane/help_formatter.rb
|
|
@@ -167,7 +168,6 @@ files:
|
|
|
167
168
|
- lib/ergane/path_registry.rb
|
|
168
169
|
- lib/ergane/runner.rb
|
|
169
170
|
- lib/ergane/tool.rb
|
|
170
|
-
- lib/ergane/util/debug.rb
|
|
171
171
|
- lib/ergane/util/formatting.rb
|
|
172
172
|
- lib/ergane/version.rb
|
|
173
173
|
homepage: https://github.com/TwilightCoders/ergane
|
|
@@ -175,7 +175,6 @@ licenses:
|
|
|
175
175
|
- MIT
|
|
176
176
|
metadata:
|
|
177
177
|
rubygems_mfa_required: 'true'
|
|
178
|
-
homepage_uri: https://github.com/TwilightCoders/ergane
|
|
179
178
|
source_code_uri: https://github.com/TwilightCoders/ergane
|
|
180
179
|
changelog_uri: https://github.com/TwilightCoders/ergane/blob/main/CHANGELOG.md
|
|
181
180
|
rdoc_options: []
|
data/lib/ergane/util/debug.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Ergane
|
|
4
|
-
module Util
|
|
5
|
-
module Debug
|
|
6
|
-
def self.enabled?
|
|
7
|
-
!!$ergane_debug
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def self.enable!
|
|
11
|
-
$ergane_debug = true
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def self.disable!
|
|
15
|
-
$ergane_debug = false
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def self.log(message)
|
|
19
|
-
return unless enabled?
|
|
20
|
-
$stderr.puts "[Ergane DEBUG] #{message}"
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|