yargs-lite

1.1.2 • Public • Published

yargs-lite

lite-weight yargs, speed up your cli in some cases

setup

  1. go to your project directory and create bin folder

  2. create bin/index.js and put #!/usr/bin/env node on the top of the file

  3. (optional) chmod 777 bin/index.js

  4. package.json : {..."scripts":{ "myscript": "./bin/index.js" }, "bin": {"myscript": "bin/index.js"}}

  5. use npm run myscript or call ./bin/index.js directly

  6. under bin/index.js checkout example

  7. (opitonal) link you command, or put it under path variables so you can use your command directly. i.e. sudo -u root ln -s bin/index.js /usr/bin/myscript

example

#!/usr/bin/env node
const yargslite = require("yargs-lite");
let yargs = new yargslite();

yargs.addHelpDocEntry("--help", "general help page\ndisplay:\tdisplay storage");

yargs.addCommand("display", (storage) => console.log(storage));
yargs.addHelpDocEntry("display", "display the storage from yargs");

yargs.run();

./bin/index.js display

=>

{
  origin: { argv: [...]},
  argv: [ 'display' ],
  entry: 'display',
  _: [],
  args: {},
  kwargs: {}
}

./bin/index.js --help
./bin/index.js display --help

=>

general help page
display:      display storage

display the storage from yargs
./bin/index.js display 10 -a 11 -cd 12 --e --f 13

=>

{
  origin: { argv: [...]},
  argv: [ ... ],
  entry: 'display',
  _: [ '10' ],
  args: { a: [ '11' ], c: [ '12' ], d: [ '12' ] },
  kwargs: { e: [ '13' ], f: [ '13' ] }
}

Adv.

Change help doc display

let fancyLogger = (content) => console.log("---", content, "---");
yargs.help.helpDocDisplay = (entry, doc) => fancyLogger(doc[entry]);

Super fast doc loading

yargs.help.helpDoc = require("pre-defined-doc.json");

Define your own parser

yargs.addParser((storage) => {
  storage.args = { "-v": "10" };
  storage.kwargs = { "--value": "ten" };
  storage.extra = "message";
});

yargs.addCommand("test", (storage) => console.log(storage.args, storage.kwargs, storage.extra));

and run

./bin/index.js test

=>

{ '-v': '10' } { '--value': 'ten' } message

pattern and logic

let's define some parameters as

./s a b -c -d --e --f g

and ignore the entry (full command should be something like ./bin/index.js -c a --e b), and the default result contains {args:{}, kwargs:{}, _:[]}

straight-forward case example

# 1 to 1 pair
./s -c a -d b       # args:{c:[a], d:[b]}
./s --e a --f b     # kwargs:{e:[a], f:[b]}

./s a               # _:[a]

ambiguous case example

# n to 1 pair
./s -cd a           # args:{c:[a], d:[a]}
./s -c -d a         # args:{c:[a], d:[a]}
./s -c -d --f b     # args:{c:[b], d:[b]}, kwargs:{f:[b]}

# other example
./s a -cd b g       # _:[a], args:{c:[b,g], d:[b,g]}
./s a --f b g       # _:[a], kwargs:{f:[b,g]}
./s a b g           # _:[a,b,g]
./s a -c abg        # _:[a], args:{c:[a,b,g]}
./s a --ef b g      # _:[a], kwargs:{ef:[b,g]}

./s a -cd           # _:[a], args:{c:[], d:[]}
./s a -cd g -cd     # _:[a], args:{c:[], d:[]}
./s a ---ef         # _:[a], kwargs:{ef:[]}

version >= 1.1.0

./s -cd=a           # args:{c:[a], d:[a]}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.22latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.22
1.1.10
1.1.00
1.0.10
1.0.00

Package Sidebar

Install

npm i yargs-lite

Weekly Downloads

2

Version

1.1.2

License

MIT

Unpacked Size

9.99 kB

Total Files

9

Last publish

Collaborators

  • truth1984