@b4dnewz/object-to-argv
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

object-to-argv

Convert an object to a suitable argv array

NPM version Build Status Coverage percentage

Install

npm install @b4dnewz/object-to-argv

Usage

import objectToArgv from "@b4dnewz/object-to-argv"

objectToArgv({
  c: true,
  baz: false,
  foo: "bar",
  arr: ["a", "b"]
})

// ["-c", "--no-baz", "--foo", "bar", "--arr", "a", "b"]

You can also add positional arguments setting the value to null

objectToArgv({
  foo: null,
  bar: true
})

// ["foo", "--bar"]

Options

Here a list of options that can be used when converting objects to argv array, simply pass them as second argument to the conversion function.

normalize

When enabled it will normalize the object key names and the final argv flags to kebab-case, by default is true.

objectToArgv({ fooBar: true })
// ["--foo-bar"]

objectToArgv({ fooBar: true }, {
  normalize: false
})
// ["--fooBar"]

booleanNegation

When enabled it will add --no prefix before the boolean flags, otherwise if disabled it will simply omit the keys that have a falsy value, by default is true.

objectToArgv({ foo: false })
// ["--no-foo"]

objectToArgv({ foo: false }, {
  booleanNegation: false
})
// []

arraySeparator

This option allow to customize the separator used for array values, when false it will output each array element in it's own place, otherwise if is a string it will be used as separator.

objectToArgv({ foo: ["bar", "baz"] })
// ["--foo", "bar", "baz"]

objectToArgv({ foo: ["bar", "baz"] }, {
  arraySeparator: ","
})
// ["--foo", "bar,baz"]

License

MIT © Filippo Conti

Package Sidebar

Install

npm i @b4dnewz/object-to-argv

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

7.76 kB

Total Files

5

Last publish

Collaborators

  • b4dnewz