Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/dargs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.0.0
Choose a base ref
...
head repository: sindresorhus/dargs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.0.0
Choose a head ref
  • 4 commits
  • 11 files changed
  • 2 contributors

Commits on Sep 10, 2019

  1. Tidelift tasks

    sindresorhus committed Sep 10, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6ef4264 View commit details

Commits on Jan 1, 2021

  1. Copy the full SHA
    88490a0 View commit details

Commits on May 3, 2021

  1. Copy the full SHA
    3c3a155 View commit details
  2. 8.0.0

    sindresorhus committed May 3, 2021
    Copy the full SHA
    b80546a View commit details
Showing with 143 additions and 120 deletions.
  1. +1 −0 .github/funding.yml
  2. +3 −0 .github/security.md
  3. +22 −0 .github/workflows/main.yml
  4. +0 −5 .travis.yml
  5. +79 −85 index.d.ts
  6. +3 −7 index.js
  7. +1 −1 index.test-d.ts
  8. +1 −1 license
  9. +9 −6 package.json
  10. +21 −12 readme.md
  11. +3 −3 test.js
1 change: 1 addition & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
tidelift: npm/dargs
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

164 changes: 79 additions & 85 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,78 @@
declare namespace dargs {
interface Options {
/**
Keys or regex of keys to exclude. Takes precedence over `includes`.
*/
excludes?: ReadonlyArray<string | RegExp>;

/**
Keys or regex of keys to include.
*/
includes?: ReadonlyArray<string | RegExp>;

/**
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
*/
aliases?: {[key: string]: string};

/**
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
@default true
@example
```
import dargs = require('dargs');
console.log(dargs({foo: 'bar'}, {useEquals: false}));
// [
// '--foo', 'bar'
// ]
```
*/
useEquals?: boolean;

/**
Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.
@default true
@example
```
import dargs = require('dargs');
console.log(dargs({a: true}));
//=> ['-a']
console.log(dargs({a: true}, {shortFlag: false}));
//=> ['--a']
```
*/
shortFlag?: boolean;

/**
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
@default false
*/
ignoreFalse?: boolean;

/**
By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.
@default false
@example
```
import dargs = require('dargs');
console.log(dargs({fooBar: 'baz'}));
//=> ['--foo-bar', 'baz']
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
//=> ['--fooBar', 'baz']
```
*/
allowCamelCase?: boolean;
}
export interface Options {
/**
Keys or regex of keys to exclude. Takes precedence over `includes`.
*/
readonly excludes?: ReadonlyArray<string | RegExp>;

/**
Keys or regex of keys to include.
*/
readonly includes?: ReadonlyArray<string | RegExp>;

/**
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
*/
readonly aliases?: Record<string, string>;

/**
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
@default true
@example
```
import dargs from 'dargs';
console.log(dargs({foo: 'bar'}, {useEquals: false}));
// [
// '--foo', 'bar'
// ]
```
*/
readonly useEquals?: boolean;

/**
Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.
@default true
@example
```
import dargs from 'dargs';
console.log(dargs({a: true}));
//=> ['-a']
console.log(dargs({a: true}, {shortFlag: false}));
//=> ['--a']
```
*/
readonly shortFlag?: boolean;

/**
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
@default false
*/
readonly ignoreFalse?: boolean;

/**
By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.
@default false
@example
```
import dargs from 'dargs';
console.log(dargs({fooBar: 'baz'}));
//=> ['--foo-bar', 'baz']
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
//=> ['--fooBar', 'baz']
```
*/
readonly allowCamelCase?: boolean;
}

/**
@@ -84,7 +82,7 @@ Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of
@example
```
import dargs = require('dargs');
import dargs from 'dargs';
const input = {
_: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list
@@ -124,7 +122,6 @@ console.log(dargs(input, {excludes, includes}));
// '--multiple=value2'
// ]
console.log(dargs(input, {includes}));
// [
// '--camel-case=5',
@@ -134,7 +131,6 @@ console.log(dargs(input, {includes}));
// '--sad=:('
// ]
console.log(dargs({
foo: 'bar',
hello: true,
@@ -147,12 +143,10 @@ console.log(dargs({
// ]
```
*/
declare function dargs(
export default function dargs(
object: {
'--'?: string[];
_?: string[];
} & {[key: string]: string | boolean | number | readonly string[]},
options?: dargs.Options
} & Record<string, string | boolean | number | readonly string[]>,
options?: Options
): string[];

export = dargs;
10 changes: 3 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const match = (array, value) =>
array.some(x => (x instanceof RegExp ? x.test(value) : x === value));
array.some(element => (element instanceof RegExp ? element.test(value) : element === value));

const dargs = (object, options) => {
export default function dargs(object, options) {
const arguments_ = [];
let extraArguments = [];
let separatedArguments = [];
@@ -115,6 +113,4 @@ const dargs = (object, options) => {
}

return arguments_;
};

module.exports = dargs;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType, expectError} from 'tsd';
import dargs = require('.');
import dargs from './index.js';

const object = {
_: ['some', 'option'],
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "dargs",
"version": "7.0.0",
"version": "8.0.0",
"description": "Reverse minimist. Convert an object of options into an array of command-line arguments.",
"license": "MIT",
"repository": "sindresorhus/dargs",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -41,8 +44,8 @@
"argv"
],
"devDependencies": {
"ava": "^2.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
}
33 changes: 21 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs)
# dargs

> Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of options into an array of command-line arguments.
Useful when spawning command-line tools.


## Install

```
$ npm install dargs
```


## Usage

```js
const dargs = require('dargs');
import dargs from 'dargs';

const object = {
_: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list
@@ -86,7 +84,6 @@ console.log(dargs({
*/
```


## API

### dargs(object, options?)
@@ -121,13 +118,13 @@ Maps keys in `object` to an aliased name. Matching keys are converted to argumen

##### useEquals

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.

```js
const dargs = require('dargs');
import dargs from 'dargs';

console.log(dargs({foo: 'bar'}, {useEquals: false}));
/*
@@ -139,13 +136,13 @@ console.log(dargs({foo: 'bar'}, {useEquals: false}));

##### shortFlag

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.

```js
const dargs = require('dargs');
import dargs from 'dargs';

console.log(dargs({a: true}));
//=> ['-a']
@@ -156,24 +153,36 @@ console.log(dargs({a: true}, {shortFlag: false}));

##### ignoreFalse

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.

##### allowCamelCase

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.

```js
const dargs = require('dargs');
import dargs from 'dargs';

console.log(dargs({fooBar: 'baz'}));
//=> ['--foo-bar', 'baz']

console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
//=> ['--fooBar', 'baz']
```

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-dargs?utm_source=npm-dargs&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import dargs from '.';
import dargs from './index.js';

const fixture = {
_: ['some', 'option'],
@@ -38,11 +38,11 @@ test('convert options to cli flags', t => {
});

test('raises a TypeError if \'_\' value is not an Array', t => {
t.throws(dargs.bind(dargs, {a: 'foo', _: 'baz'}), TypeError);
t.throws(dargs.bind(dargs, {a: 'foo', _: 'baz'}), {instanceOf: TypeError});
});

test('raises a TypeError if \'--\' value is not an Array', t => {
t.throws(dargs.bind(dargs, {a: 'foo', '--': 'baz'}), TypeError);
t.throws(dargs.bind(dargs, {a: 'foo', '--': 'baz'}), {instanceOf: TypeError});
});

test('useEquals options', t => {