1.0.2 • Published 7 years ago

funwithflags v1.0.2

Weekly downloads
49
License
MIT
Repository
github
Last release
7 years ago

🚩 funwithflags

funwithflags

Build Status NPM version MIT License fliphub fluents

parse argument options

This module is minimist refactored as a fluent class ~125% faster.

browser support

📘 examples

var argv = require('funwithflags')(process.argv.slice(2));
console.log(argv);

📦 install

yarn add funwithflags --dev
npm install funwithflags --save-dev

⚙ options

NameTypeDescription?DefaultExample
stringstring | Array<string>names to always treat as stringsnull
booleanboolean | string | Array<string>always treat as booleans. if true will treat all double hyphenated arguments without equal signs. (e.g. affects --foo, not -f or --foo=bar)null
aliasObjectan object mapping string names to strings or arrays of string names to use as aliases{}
defaultObjectan object mapping string argument names to default values{}
['--']booleanpopulate argv._ with everything before the -- and argv['--'] with everything after the --null
stopEarlybooleanwhen true, populate argv._ with everything after the first non-optionnull
unknownFunctiona function which is invoked with a command line parameter not defined in the opts configuration object. If the function returns false, the unknown option is not added to argvnull
objbooleanwhen true, returns the object instance of FunWithFlagsnull
varsbooleanwhen true, allows args without dashes to be used as flagsnull
camelbooleanwhen true, camelCases object keys on argvnull
underscorebooleanwhen false, object is returned with no _ (for looping over object keys or values or the like)null

extending the class

🔬 tests

🏋️ benchmarks

verbose

var unknown = []

// captures the unknown args, similar to how `vars` does
function unknownFn(arg) {
  unknown.push(arg)
  return true
}

var opts = {
  'default': {
    'moose.box': 11,
    'eh': true,
    'igloo': false,
  },
  'alias': {
    'moose.box': 'mooses.boxes',
    'rain': 'british-columbia',
  },
  'boolean': ['eh', 'igloo'],
  'string': ['country', 'nan', 'noflag'],
  'vars': true,
  '--': true,
  'obj': true, // will return the instance
  'unknown': unknownFn,
}

var args = [
  '--country=canada',

  // aliased to `rain` so it will have `rain: true, 'british-columbia': true`
  '--british-columbia',

  // no value is default a true boolean
  '--igloo',

  // dot notation
  '--a.b=100',

  // using `string: 'nan'` we ensure this stays as a string
  '--nan',
  '99',

  // first flag is boolean (t: true)
  // second flag assigned to following value (f: 555)
  '-tf',
  '555',

  // mooses and globbing are parsed only because `vars: true``
  'mooses.boxes=moozes',
  'globbing',
  `"**/*"`,

  // after double dash
  '--',
  'dis-little-piggy',
  'went-to-market',
]

var obj = require('../')(args, opts)
const argv = obj.argv