eca icon indicating copy to clipboard operation
eca copied to clipboard

Support named arguments and description in custom commands

Open joshuahamlet opened this issue 6 months ago • 1 comments

To upvote this issue, give it a thumbs up. See this list for the most upvoted issues.

It looks like custom commands currently hardcode their :description to the file path and their :arguments to auto-inferred arg1, arg2, ... names derived from $1/$ARG1 placeholders. It would be awesome if there was the option for named arguments as well.

For example, this is what I see when invoking a command

/learning-roadmap  ~/.config/eca/commands/learning-roadmap.md
  Arg: arg1  Value: ___
  Arg: arg2  Value: ___

But it would be super helpful for more involved commands if you could show additional context:

/learning-roadmap  Generate a dependency-graph learning roadmap
  Arg: domain   Value: ___
  Arg: why      Value: ___
  Arg: runtime  Value: ___
  Arg: depth    Value: ___

Since the project already uses Selmer (selmer.parser is required in eca.shared, and safe-selmer-render already exists), maybe Selmer's {{name}} syntax could also be used in custom commands as an alternative to positional $1/$ARG1 placeholders:

---
description: "Generate a dependency-graph learning roadmap"
---

You are helping me build a structured self-study roadmap...

Domain: {{domain}}
Why I want to learn it: {{why}}
Runtime/engine: {{runtime}}
Desired depth: {{depth}}

Maybe the existing parse-md function (already in eca.shared, already used by skills) could extract optional frontmatter from command files:

  • description — shown in the editor UI instead of the file path
  • arguments — optional, allows adding description fields to args for richer UI hints
---
description: "Generate a dependency-graph learning roadmap"
arguments:
  - name: domain
    description: "Domain and subfield to learn"
  - name: depth
    description: "intro | intermediate | advanced | mixed"
---

Possible implementation

1. shared/extract-args-from-content — detect {{name}} patterns

Add a branch for named variables alongside the existing positional regex:

(defn extract-args-from-content
  [content]
  (if (string/blank? content)
    []
    (let [content (str content)
          nums (keep (fn [[_ n]] (parse-long n))
                     (re-seq #"\$(?:ARG)?(\d+)" content))
          has-varargs (some #(string/includes? content %)
                            ["$ARGS" "$ARGUMENTS"])
          ;; New: detect {{name}} patterns
          named (distinct (map second (re-seq #"\{\{(\w+)\}\}" content)))
          has-positional (or (seq nums) has-varargs)
          has-named (seq named)]
      ;; Reject mixed styles
      (when (and has-positional has-named)
        (throw (ex-info "Command mixes positional ($1) and named ({{name}}) arguments — use one style"
                        {:positional (seq nums) :named named})))
      (cond
        ;; Named args: return in order of first appearance
        has-named
        (vec (for [n named]
               {:name n :required true}))
        ;; Positional: existing behavior
        :else
        (let [max-n (when (seq nums) (apply max nums))
              declared-count (cond max-n max-n has-varargs 1)]
          (if declared-count
            (vec (for [i (range 1 (inc declared-count))]
                   {:name (str "arg" i) :required true}))
            []))))))

2. commands/substitute-args — dispatch to Selmer for named args

(defn ^:private substitute-args [content args & [arg-metadata]]
  (if (and (seq arg-metadata)
           (some #(string/includes? content (str "{{" (:name %) "}}")) arg-metadata))
    ;; Named: build context map, render via Selmer
    (let [context (zipmap (map (comp keyword :name) arg-metadata) args)]
      (shared/safe-selmer-render content context "command"))
    ;; Positional: existing behavior, unchanged
    (let [args-joined (string/join " " args)
          content-with-args (-> content
                                (string/replace "$ARGS" args-joined)
                                (string/replace "$ARGUMENTS" args-joined))]
      (reduce (fn [c [i arg]]
                (-> c
                    (string/replace (str "$ARG" (inc i)) arg)
                    (string/replace (str "$" (inc i)) arg)))
              content-with-args
              (map-indexed vector args)))))

3. commands/command-file->command — parse frontmatter, thread metadata

(defn ^:private command-file->command [type file opts]
  (let [base (normalize-command-name file)
        raw (interpolation/replace-dynamic-strings (slurp (str file)) (str (fs/parent file)) nil)
        {:keys [body description arguments]} (shared/parse-md raw)
        content (or body raw)
        inferred-args (shared/extract-args-from-content content)
        ;; Merge frontmatter argument metadata (descriptions) into inferred args
        final-args (if (seq arguments)
                     (vec (map-indexed
                           (fn [i fm-arg]
                             (let [inferred (get inferred-args i {})]
                               (merge inferred
                                      (if (string? fm-arg) {:name fm-arg} fm-arg))))
                           arguments))
                     inferred-args)]
    (cond-> {:name (if-let [plugin (:plugin opts)]
                     (shared/prefixed-name plugin base)
                     base)
             :path (str (fs/canonicalize file))
             :type type
             :content content
             :arguments final-args}
      description (assoc :description description)
      (:plugin opts) (assoc :plugin (:plugin opts)))))

4. commands/all-commands — prefer frontmatter description over path

;; Change:
;;   :description (:path custom)
;; To:
;;   :description (or (:description custom) (:path custom))

5. Call sites for substitute-args — thread arg metadata

get-custom-command and the skill branch in handle-command! both call substitute-args. They would need to pass the arguments metadata as a third arg so named dispatch works:

(defn ^:private get-custom-command [command args custom-cmds]
  (when-let [cmd (first (filter #(= command (:name %)) custom-cmds))]
    (substitute-args (:content cmd) args (:arguments cmd))))
  • Commands without frontmatter and using $1/$ARG1 would behave the same.
  • Commands with frontmatter that omit description would still show the file path.
  • Commands with frontmatter that omit arguments would still use inferred args.
  • Mixing $1 and {{name}} in the same file is a parse-time error with a clear message.

joshuahamlet avatar Jun 03 '26 01:06 joshuahamlet

Done, available on next release!

ericdallo avatar Jun 03 '26 15:06 ericdallo