to-arg
Create a command-line argument from a string or string (key) and value.
Last updated 10 years ago by jonschlinkert .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install to-arg 
SYNC missed versions from official npm registry.

to-arg NPM version Build Status

Create a command-line argument from a string or string (key) and value.

Install with npm

npm i to-arg --save

Usage

var toArg = require('to-arg');

toArg('abc');
//=> '--abc'

toArg('abc', true);
//=> '--abc'

toArg('abc', 'xyz');
//=> '--abc=xyz'

toArg('abc', 'true');
//=> '--abc=true'

toArg('abc', 10);
//=> '--abc=10'

casing

Keys that are camelcase or contain spaces will be dash-cased:

toArg('fooBar');
//=> '--foo-bar'

toArg('a b c');
//=> '--a-b-c'

toArg('A');
//=> '--a'

Usage example

var obj = {
  foo: 'bar',
  abc: true,
  xyz: 10,
  one: false
};

var args = Object.keys(obj).map(function (key) {
  return toArg(key, obj[key]);
});
//=> ['--foo=bar', '--abc', '--xyz=10', '--no-one']

Options

invert

When the value is false an inverted flag is created by default:

toArg('a', false);
//=> '--no-a'

To disable inversion, pass false on the options:

toArg('a', false, {invert: false});
//=> '--a'

Other awesome libs

  • option-cache: Simple API for managing options in JavaScript applications.
  • config-cache: General purpose JavaScript object storage methods.

Run tests

Install dev dependencies:

npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

This was inspired by grunt.option.

License

Copyright (c) 2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb-cli on April 17, 2015.

Current Tags

  • 1.1.0                                ...           latest (10 years ago)

2 Versions

  • 1.1.0                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Total 1
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE