mirror of
https://github.com/affaan-m/ECC.git
synced 2026-09-29 13:05:18 +02:00
2.2 KiB
2.2 KiB
snippet-cli
A small in-process snippet manager. No external dependencies; Node.js standard library only.
Contract
src/cli.js is CommonJS and exports run(argv, state):
argv: array of command-line words (already split, no program name).state: any plain object, created by the caller as{}. The CLI keeps its data in it and mutates it in place; it survives across calls.- Returns synchronously:
{ code, stdout, stderr }— a number and two strings (empty string when there is nothing to print).runmust never throw, on any input. - All printed lines end with
\n.
Commands (all behavior below is contractual)
add <name> [--tags a,b] <text...>— creates a snippet from the remaining words joined by single spaces. Printscreated <name>, code 0.- Adding an existing name: code 1, stderr
error: snippet '<name>' already exists, state unchanged. addwith a missing name or missing text: code 2, stderrusage: add <name> [--tags t1,t2] <text...>.- Names must match
^[a-z0-9][a-z0-9-]*$; otherwise code 2, stderrerror: invalid snippet name '<name>'. get <name>— prints the exact text, code 0. Unknown name: code 2, stderrerror: no snippet named '<name>'.remove <name>— printsremoved <name>, code 0. Unknown name: same asget.list— every snippet name, sorted ascending, one per line. With no snippets: printsno snippets. Always code 0.list --tag <t>— only snippets whose tags includet.search <term>— case-insensitive substring match over name and text; prints matching names sorted, one per line; printsno matcheswhen empty. Code 0.export— printsJSON.stringifyof{ snippets: { <name>: { text, tags } } }with names sorted and eachtagsarray sorted. Code 0.import <json>— merges an exported document: names not already present are added, existing names are skipped. Printsimported <N>, skipped <M>, code 0. Malformed JSON: code 1, stderrerror: invalid JSON, state unchanged.- No command or an unknown command: code 2, stderr
usage: snippet <add|get|list|remove|search|export|import>.
Run the tests with npm test.