refactor: extract sorted_subcommands helper to berhtjan-core scanner #4

Closed
opened 2026-05-08 11:47:24 +02:00 by michael · 1 comment
Owner

Goal

Extract the repeated subcommand-sorting logic into a shared helper
scanner.sorted_subcommands(cmd) in berhtjan-core/scanner.lua.

Current problem

The sort logic (by order field, fallback alphabetical) is duplicated in
three places after the refactor in DAW/daw-modul-berhtjan-core#3:

  1. berhtjan-core/scanner.lua -- inside build_index
  2. berhtjan-cli/dispatch.lua -- inside print_subcommands
  3. berhtjan-cli/dispatch.lua -- inside build_usage

Proposed change

Add to berhtjan-core/scanner.lua:

-- Returns subcommands as a sorted list of { name, ... } entries.
-- Sorted by order field (optional int), fallback: alphabetical by name.
function M.sorted_subcommands(cmd)
  local subs = {}
  for n, s in pairs(cmd.subcommands) do
    local entry = { name = n }
    for k, v in pairs(s) do entry[k] = v end
    table.insert(subs, entry)
  end
  table.sort(subs, function(a, b)
    if a.order and b.order then return a.order < b.order end
    if a.order then return true end
    if b.order then return false end
    return a.name < b.name
  end)
  return subs
end

Export via berhtjan-core/init.lua as M.sorted_subcommands.

build_index in scanner.lua and both call sites in dispatch.lua
replace their inline sort with scanner.sorted_subcommands(cmd).

Acceptance criteria

  • sorted_subcommands implemented and exported from berhtjan-core
  • build_index uses it
  • print_subcommands in dispatch.lua uses it
  • build_usage in dispatch.lua uses it
  • Existing sort tests in test_berhtjan-core-scanner.lua still pass

References

## Goal Extract the repeated subcommand-sorting logic into a shared helper `scanner.sorted_subcommands(cmd)` in `berhtjan-core/scanner.lua`. ## Current problem The sort logic (by `order` field, fallback alphabetical) is duplicated in three places after the refactor in DAW/daw-modul-berhtjan-core#3: 1. `berhtjan-core/scanner.lua` -- inside `build_index` 2. `berhtjan-cli/dispatch.lua` -- inside `print_subcommands` 3. `berhtjan-cli/dispatch.lua` -- inside `build_usage` ## Proposed change Add to `berhtjan-core/scanner.lua`: ```lua -- Returns subcommands as a sorted list of { name, ... } entries. -- Sorted by order field (optional int), fallback: alphabetical by name. function M.sorted_subcommands(cmd) local subs = {} for n, s in pairs(cmd.subcommands) do local entry = { name = n } for k, v in pairs(s) do entry[k] = v end table.insert(subs, entry) end table.sort(subs, function(a, b) if a.order and b.order then return a.order < b.order end if a.order then return true end if b.order then return false end return a.name < b.name end) return subs end ``` Export via `berhtjan-core/init.lua` as `M.sorted_subcommands`. `build_index` in `scanner.lua` and both call sites in `dispatch.lua` replace their inline sort with `scanner.sorted_subcommands(cmd)`. ## Acceptance criteria - [ ] `sorted_subcommands` implemented and exported from berhtjan-core - [ ] `build_index` uses it - [ ] `print_subcommands` in dispatch.lua uses it - [ ] `build_usage` in dispatch.lua uses it - [ ] Existing sort tests in test_berhtjan-core-scanner.lua still pass ## References - DAW/daw-modul-berhtjan-core#3 (refactor that introduced the duplication) - DAW/daw-modul-berhtjan-cli (dispatch.lua)
Author
Owner

Resolved as part of the 2.1 migration (v2.1.0).

M.sorted_subcommands(cmd) added to scanner/index.lua, exported via the
facade, and used by both scanner.build_index() and
daw-modul-berhtjan-cli's dispatch/display.lua (replacing that repo's
local sorted_subs() duplicate). Hardened beyond the original proposal
with input validation (invalid_command, invalid_subcommands), since
it's a publicly exported function used by another repo.

Refs: DAW/daw-modul-berhtjan-cli (dispatch/display.lua)

Resolved as part of the 2.1 migration (v2.1.0). M.sorted_subcommands(cmd) added to scanner/index.lua, exported via the facade, and used by both scanner.build_index() and daw-modul-berhtjan-cli's dispatch/display.lua (replacing that repo's local sorted_subs() duplicate). Hardened beyond the original proposal with input validation (invalid_command, invalid_subcommands), since it's a publicly exported function used by another repo. Refs: DAW/daw-modul-berhtjan-cli (dispatch/display.lua)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
DAW/daw-modul-berhtjan-core#4
No description provided.