Skip to content

plotdb/args

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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 flags

Option types

  • string: --file foo.txt"foo.txt"
  • number: --count 33
  • boolean: --verbosetrue
  • array: --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 alias
  • commands: positional arguments (non-flag tokens), including the script path as commands[0]
  • passThrough: arguments after --
  • executables: runtime prefix — [node] under node, [node, lsc] under lsc
  • lsc: process.argv.lsc set by lsc runtime (truthy array); undefined under node
  • raw: original process.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

About

argument parser

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors