@plotdb/args
v0.0.4
Published
Simple argument paser
Readme
@plotdb/args
Simple argument parser. Works transparently with both node and lsc (LiveScript) invocations.
Usage:
const args = require("@plotdb/args");
const result = args({
meta: {
file: {alias: "f", type: "string", desc: "filename"},
count: {alias: "c", type: "number", desc: "loop count"},
verbose: {alias: "v", type: "boolean", desc: "verbose mode", required: false},
tags: {alias: "t", type: "array", desc: "tags (repeatable)", required: false},
},
onerror: function(e) { process.exit(1); }
});
const opt = result.options;
console.log(opt.file, opt.f); // string — both name and alias work
console.log(opt.count, opt.c); // number
console.log(opt.verbose, opt.v); // boolean
console.log(opt.tags, opt.t); // array — accumulates repeated flagsOption types
string:--file foo.txt→"foo.txt"number:--count 3→3boolean:--verbose→truearray:--tag a --tag b→["a", "b"](repeated flags accumulate)
All options are required by default. Set required: false to make them optional.
Result object
options: parsed named flags, accessible by both long name and aliascommands: positional arguments (non-flag tokens), including the script path ascommands[0]passThrough: arguments after--executables: runtime prefix —[node]under node,[node, lsc]under lsclsc:process.argv.lscset by lsc runtime (truthy array);undefinedunder noderaw: originalprocess.argv
node vs lsc
@plotdb/args detects the invocation context via process.argv.lsc, which the lsc runtime sets automatically. The correct number of prefix elements is stripped from process.argv so that options, commands, and passThrough always reflect only the user-supplied arguments — regardless of whether the script is run with node or lsc.
- node:
process.argv=[node, script.js, ...args]→ strips 1 prefix element - lsc:
process.argv=[node, lsc, script.ls, ...args]→ strips 2 prefix elements
Full options
args({
meta: { ... }, // flag definitions (required)
argv: [...], // custom argv array — defaults to process.argv
onerror: fn, // called on parse error — process.exit() when omitted
})You can also pass meta directly for brevity:
args({ file: {alias: "f", type: "string"} })License
MIT
