Parsing commanl-line arguments in style of old good getopt with no extras.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.editorconfig .editorconfig added Jun 6, 2018
.eslintrc .eslintrc updated Aug 19, 2018
.gitignore better .gitignore Jun 6, 2018
.travis.yml node >=8 Jun 6, 2018
LICENSE License added Jun 6, 2018
README.md npm badge fixed Sep 7, 2015
index.js code linted Aug 19, 2018
package.json 1.0.1 Aug 19, 2018
test.js ava instead tape Jun 6, 2018

README.md

Oopt

Parsing commanl-line arguments in style of old good getopt with no extras. For geeks and minimalists only. Only configuration is standart optstring (something like 'ab:c') and result is an object with 'options', 'arguments' and 'operands'.

Build Status NPM version

If no bugs, it ought to be pure getopt, that is described in The Open Group Base Specifications Issue 7 (aka IEEE Std 1003.1, 2013 Edition):

The getopt() function is a command-line parser that shall follow Utility Syntax Guidelines 3, 4, 5, 6, 7, 9, and 10 in XBD Utility Syntax Guidelines.

Main intention was to make utility, which passed tests, that describe those XBD Utility Syntax Guidelines:

Guideline 3: Each option name should be a single alphanumeric character (the alnum character classification) from the portable character set. The -W (capital-W) option shall be reserved for vendor options. Multi-digit options should not be allowed.

Guideline 4: All options should be preceded by the '-' delimiter character.

Guideline 5: One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.

Guideline 6: Each option and option-argument should be a separate argument, except as noted in Utility Argument Syntax, item (2).

Guideline 7: Option-arguments should not be optional.

Guideline 9: All options should precede operands on the command line.

Guideline 10: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the '-' character.

If something in oopt works not like in Guidelines - that's a bug. Feel free to make issue or pull request.

Install

npm install oopt

Tests

$ npm test

Example

//app.js
var oopt = require('oopt');
console.log(oopt('ab:c'));

$ node app.js
{ a: false, b: '', c: false}
$ node app.js -a -b arg
{ a: true, b: 'arg', c: false }
$ node app.js --
{ a: false, b: '', c: false, _: [] }
$ node app.js -ab arg p1 p2
{ a: true, b: 'arg', c: false, _: [ 'p1', 'p2' ] }
$ node app.js -a -b arg p1 p2
{ a: true, b: 'arg', c: false, _: [ 'p1', 'p2' ] }
$ node app.js -b arg -a p1 p2
{ a: true, b: 'arg', c: false, _: [ 'p1', 'p2' ] }
$ node app.js -a -barg p1 p2
{ a: true, b: 'arg', c: false, _: [ 'p1', 'p2' ] }
$ node app.js -abarg p1 p2
{ a: true, b: 'arg', c: false, _: [ 'p1', 'p2' ] }
$ node app.js -a -b arg -- -c p1 -p2
{ a: true, b: 'arg', c: false, _: [ '-c', 'p1', '-p2' ] }
$ node app.js -a -b "a r g" "p1 p2"
{ a: true, b: 'a r g', c: false, _: [ 'p1 p2' ] }
$ node app.js -b -a
{ a: false, b: '-a', c: false }
$ node app.js "-a -b"
{ a: false, b: '', c: false, _: [ '-a -b' ] }
$ node app.js p1 p2 -a -b arg
{ a: false, b: '', c: false, _: [ 'p1', 'p2', '-a', '-b', 'arg' ] }

More examples in tests.

License

MIT