Convert an object to a suitable argv array
Convert an object to a suitable argv array
@b4dnewz/object-to-argv">
npm install @b4dnewz/object-to-argv
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"]
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"]
MIT © Filippo Conti