Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
adhamu committed May 3, 2022
1 parent ebac8b8 commit d3b17be
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

A tiny package to parse CLI flags and arguments into an object.

## Requirements

- Node 16+

## Installation

```shell
yarn add arge

# or
npm i arge
```

## Usage

```typescript
```javascript
const { arge } = require('arge')

// or
import { arge } from 'arge'
```

Then:

```typescript
const args = arge(process.argv)
```

Expand Down Expand Up @@ -72,8 +78,6 @@ If you wanted to pass an arbitrary array of flags that don't come from `process.
For example

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

const flags = [
'--dry-run',
'--mode=development',
Expand Down Expand Up @@ -104,8 +108,6 @@ 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 })
Expand Down
2 changes: 1 addition & 1 deletion esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const options = {
external: Object.keys(require('./package.json').dependencies || {}),
logLevel: 'info',
minify: true,
target: ['esnext'],
target: [require('./tsconfig.json').compilerOptions.target],
}

build({
Expand Down
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.1.0",
"version": "1.1.1",
"description": "A simple utility to parse command line arguments and flags",
"keywords": [
"argv",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('arge', () => {
const argv = [
...baseArgs,
'-f',
'--dryRun',
'--dry-run',
'--mode=development',
'--test=false',
'--retries=100',
Expand Down
42 changes: 22 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ interface Options {
camelCaseKeys?: boolean
}

const parse = (value: string | boolean | number) => {
if (['true', 'false', true, false].includes(value as string)) {
return JSON.parse(value as string)
const toArray = (value: string, delimiter: string) =>
value
.split(delimiter)
.map(v => v.trim())
.filter(v => v)

const parse = (value: string): string | boolean | number | string[] => {
if (['true', 'false', true, false].includes(value)) {
return JSON.parse(value)
}

if (!Number.isNaN(Number(value))) {
return parseFloat(value as string)
return parseFloat(value)
}

if ((value as string).includes(',')) {
return (value as string).split(',').map(v => v.trim())
if (value.includes(',')) {
return toArray(value, ',')
}

if ((value as string).includes(' ')) {
return (value as string)
.split(' ')
.map(v => v.trim())
.filter(v => !!v)
if (value.includes(' ')) {
return toArray(value, ' ')
}

return value
Expand All @@ -32,21 +35,20 @@ export const arge = (
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('=')
) =>
(options.isArgv !== false
? args.filter((_, index) => index > 1)
: args
).reduce((acc, curr) => {
const [k, v = 'true'] = curr.split('=')
let key = k.replace(/^-+/, '')

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

return {
...acc,
[key]: parse(v || true),
[key]: parse(v),
}
}, {})
}

0 comments on commit d3b17be

Please sign in to comment.