diff --git a/README.md b/README.md index 3761358..2724e20 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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 +} +``` diff --git a/package.json b/package.json index 4e72c99..8585fda 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 0904377..7aa06ea 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -7,7 +7,7 @@ describe('arge', () => { const argv = [ ...baseArgs, '-f', - '--dry-run', + '--dryRun', '--mode=development', '--test=false', '--retries=100', @@ -15,7 +15,7 @@ describe('arge', () => { expect(arge(argv)).toEqual({ f: true, - 'dry-run': true, + dryRun: true, mode: 'development', retries: 100, test: false, @@ -23,26 +23,34 @@ describe('arge', () => { }) 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'], }) }) }) diff --git a/src/index.ts b/src/index.ts index fb78f18..db2911f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) @@ -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,