Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortFlag option #39

Merged
merged 7 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ declare namespace dargs {
*/
useEquals?: boolean;

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

@default false

@example
```
console.log(dargs({a: true}, {shortFlag: true}));
//=> ['-a']

console.log(dargs({a: true}));
//=> ['--a']
```
*/
shortFlag?: boolean;

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

Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const dargs = (input, options) => {
let separatedArgs = [];

options = Object.assign({
useEquals: true
useEquals: true,
shortFlag: false
}, options);

const makeArg = (key, value) => {
key =
'--' +
(options.allowCamelCase ?
key :
key.replace(/[A-Z]/g, '-$&').toLowerCase());
const prefix = options.shortFlag && key.length === 1 ? '-' : '--';
const theKey = (options.allowCamelCase ?
key :
key.replace(/[A-Z]/g, '-$&').toLowerCase());

key = prefix + theKey;

if (options.useEquals) {
args.push(key + (value ? `=${value}` : ''));
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ expectType<string[]>(dargs(input, {excludes}));
expectType<string[]>(dargs(input, {includes}));
expectType<string[]>(dargs(input, {aliases}));
expectType<string[]>(dargs(input, {useEquals: false}));
expectType<string[]>(dargs(input, {shortFlag: true}));
expectType<string[]>(dargs(input, {ignoreFalse: true}));
expectType<string[]>(dargs(input, {allowCamelCase: true}));

Expand Down
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ console.log(dargs({foo: 'bar'}, {useEquals: false}));
*/
```

##### shortFlag

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

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

###### Example

```js
console.log(dargs({a: true}, {shortFlag: true}));
//=> ['-a']

console.log(dargs({a: true}));
//=> ['--a']
```

##### ignoreFalse

Type: `boolean`<br>
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,14 @@ test('camelCase option', t => {
'--camelCaseOpt'
]);
});

test('shortFlag option', t => {
t.deepEqual(dargs({a: 123, b: 'foo', foo: 'bar', camelCaseCamel: true}, {
shortFlag: true
}), [
'-a=123',
'-b=foo',
'--foo=bar',
'--camel-case-camel'
]);
});