ergane 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bac11b2362d538571dc708483d4cbc83ee2a66e51983c5e9c312e5a1a98d3f3
4
- data.tar.gz: e27be5089a4034d7a67d26b17f4b5572d723a6ed38f3c29275d2089d6ead65cd
3
+ metadata.gz: 4a81aa6c11b88d07d9ca19cbf3c5b8e3e7d7eb2f1cbccdf0c80a8b5305712c1b
4
+ data.tar.gz: e12d81a94dea3cf2f2fd81281f3f65ff7df34b807e5f8902958b46a913af4745
5
5
  SHA512:
6
- metadata.gz: e136d486d292c5bac857f0cd8a950189d562aef43f76320b65c672bcc11f98af37658cac4b201a607c8ba103f73d84a94b5bfeafefa4044ccbd98a62707240ad
7
- data.tar.gz: c36af86e50cb12b0cdd270744cea2f4c3a3f88570c8bd305bcd976f8466ffa8e6d34a827df3619b317b2c86fcc30d73fbcecc4b10d7a9b4eed250d028baf9913
6
+ metadata.gz: b19044df0b9834065fc8f61929b801ed7e13b0b93decfdeafb78c79c35abf38b39a5a9bcafcd1899f77b0318d8c8db70996f52aa87b9773a08b070d7321e0073
7
+ data.tar.gz: 41b44653cca95e1b95214fe0ee9b45252168d63123b4b64649e8c5055ca93ce61a7e6f8c960276c4ba4785c8cf40c92e5e628186bd7aa0b7c74d6ef63a33913e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
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
+
5
13
  ## [0.2.0] - 2026-05-25
6
14
 
7
15
  ### Changed
@@ -6,6 +6,12 @@ module Ergane
6
6
 
7
7
  # +required+ defaults to nil, meaning "derive from the run signature";
8
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.
9
15
  def initialize(name, type = String, description: nil, required: nil, default: nil)
10
16
  @name = name.to_sym
11
17
  @type = type
@@ -26,6 +26,30 @@ module Ergane
26
26
  @subcommands ||= {}
27
27
  end
28
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
+
29
53
  # Effective required-ness of the positional argument at +index+. An
30
54
  # explicit DSL `required:` (true/false) wins; otherwise it's derived from
31
55
  # the run method's matching positional parameter: a required parameter
@@ -111,6 +135,14 @@ module Ergane
111
135
  @argv
112
136
  end
113
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
+
114
146
  def run(*run_args)
115
147
  if self.class.subcommands.any?
116
148
  $stdout.puts HelpFormatter.new(self.class).format
@@ -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!(argv)
39
- argv
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
@@ -3,6 +3,14 @@
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
15
  leftover = []
8
16
  until args.empty?
@@ -13,6 +21,7 @@ class OptionParser
13
21
  leftover.concat(e.args)
14
22
  end
15
23
  end
16
- args.replace(leftover)
24
+ # Whatever order! stopped in front of (the tail after a `--`) is still operands.
25
+ args.replace(leftover.concat(args))
17
26
  end
18
27
  end
@@ -22,10 +22,25 @@ module Ergane
22
22
  option(name, nil, short: short, description: description, default: false)
23
23
  end
24
24
 
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.
25
37
  def argument(name, type = String, description: nil, required: nil, default: nil)
26
- argument_definitions << ArgumentDefinition.new(
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)
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
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,43 +45,89 @@ module Ergane
22
45
  return
23
46
  end
24
47
 
25
- instance = command_class.new(remaining)
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
- args.include?("--help") || args.include?("-h")
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
-
43
- token = args.first
44
- return [command_class, args, path] if token.start_with?("-")
45
-
46
- sub = find_subcommand(command_class, token.to_sym)
47
- if sub
48
- args.shift
49
- resolve(sub, args, path)
50
- elsif command_class.subcommands.any?
51
- # The current command is a group, so an unmatched token is a bad
52
- # subcommand — not a positional arg for a leaf command.
53
- raise CommandNotFound.new(token, available: command_class.subcommands.keys)
54
- else
55
- [command_class, args, path]
69
+
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
56
83
  end
84
+
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)
100
+ end
101
+
102
+ [command_class, args, path]
57
103
  end
58
104
 
59
105
  def find_subcommand(command_class, token)
60
- command_class.subcommands[token] ||
61
- command_class.subcommands.each_value.find { |cmd| cmd.terms.include?(token) }
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
62
131
  end
63
132
  end
64
133
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ergane
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens