Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Dhamu committed May 3, 2022
1 parent fd47c17 commit ebac8b8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,33 @@ node app.js --dry-run --mode=development --test=false --retries=100

```json
{
"dry-run": true,
"dryRun": true,
"mode": "development",
"test": false,
"retries": 100
}
```

## API

### arge(flags, options?)

Returns a key-value pairs object of flags

#### flags

Type: `string[]`

#### options

Type: `object | undefined`

##### `isArgv`

Type: `boolean`

Default: `true`

By default, the `arge` function assumes that you have passed `process.argv`. It does this because:

> The first element will be process.execPath.
Expand All @@ -47,7 +67,7 @@ https://nodejs.org/docs/latest/api/process.html#processargv

This package will omit those two items from the output.

If you wanted to pass an arbitrary array of flags that don't come from `process.argv`, you can set the second argument of `arge` to `false`.
If you wanted to pass an arbitrary array of flags that don't come from `process.argv`, you can set this option to `false`.

For example

Expand All @@ -61,16 +81,40 @@ const flags = [
'--retries=100',
]

const args = arge(flags, false)
const args = arge(flags, { isArgv: false })
```

This would then output:

```json
{
"dry-run": true,
"dryRun": true,
"mode": "development",
"test": false,
"retries": 100
}
```

##### `camelCaseKeys`

Type: `boolean`

Default: `true`

This converts hyphen separated keys into camel case. To prevent this behaviour, you can set this value to `false`.

```typescript
import { arge } from 'arge'

const flags = ['--dry-run']

const args = arge(flags, { camelCaseKeys: false })
```

This would then output:

```json
{
"dry-run": true
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arge",
"version": "1.0.0",
"version": "1.1.0",
"description": "A simple utility to parse command line arguments and flags",
"keywords": [
"argv",
Expand Down
26 changes: 17 additions & 9 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,50 @@ describe('arge', () => {
const argv = [
...baseArgs,
'-f',
'--dry-run',
'--dryRun',
'--mode=development',
'--test=false',
'--retries=100',
]

expect(arge(argv)).toEqual({
f: true,
'dry-run': true,
dryRun: true,
mode: 'development',
retries: 100,
test: false,
})
})

it('converts comma separated values to an array', () => {
const argv = [...baseArgs, '--colors=red, blue,green']
const argv = [...baseArgs, '--colours=red, blue,green']

expect(arge(argv)).toEqual({
colors: ['red', 'blue', 'green'],
colours: ['red', 'blue', 'green'],
})
})

it('converts space separated values to an array', () => {
const argv = [...baseArgs, '--colors=red blue green']
const argv = [...baseArgs, '--colours=red blue green']

expect(arge(argv)).toEqual({
colors: ['red', 'blue', 'green'],
colours: ['red', 'blue', 'green'],
})
})

it('can handle non-argv input', () => {
const argv = ['--colors=red blue green']
const argv = ['--colours=red blue green']

expect(arge(argv, false)).toEqual({
colors: ['red', 'blue', 'green'],
expect(arge(argv, { isArgv: false })).toEqual({
colours: ['red', 'blue', 'green'],
})
})

it('does not convert keys to camelCase if camelCaseKeys is false', () => {
const argv = [...baseArgs, '--my-favourite-colours=red blue green']

expect(arge(argv, { camelCaseKeys: false })).toEqual({
'my-favourite-colours': ['red', 'blue', 'green'],
})
})
})
22 changes: 19 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
interface Options {
isArgv?: boolean
camelCaseKeys?: boolean
}

const parse = (value: string | boolean | number) => {
if (['true', 'false', true, false].includes(value as string)) {
return JSON.parse(value as string)
Expand All @@ -21,12 +26,23 @@ const parse = (value: string | boolean | number) => {
return value
}

export const arge = (args: string[], isArgv = true) => {
const flags = isArgv ? args.filter((_, index) => index > 1) : args
export const arge = (
args: string[],
options: Options = {
isArgv: true,
camelCaseKeys: true,
}
) => {
const flags =
options?.isArgv !== false ? args.filter((_, index) => index > 1) : args

return flags.reduce((acc, curr) => {
const [k, v] = curr.split('=')
const key = k.replace(/^-+/, '')
let key = k.replace(/^-+/, '')

key = options?.camelCaseKeys
? key.replace(/-([a-z])/g, g => g[1].toUpperCase())
: key

return {
...acc,
Expand Down

0 comments on commit ebac8b8

Please sign in to comment.