Skip to content

Commit 34876f9

Browse files
committed
Use util.parseArgs() to handle argv
1 parent 869054c commit 34876f9

9 files changed

Lines changed: 63 additions & 116 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
node: [12, 14, 16, 18, 20, 22, 24]
10+
node: [16, 18, 20, 22, 24]
1111

1212
name: node.js v${{ matrix.node }}
1313
runs-on: ubuntu-latest

bin/vault

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let cli = new CLI({
4242
async function main () {
4343
try {
4444
await legacy.migrate(logger, pathname, key)
45-
await cli.run(process.argv)
45+
await cli.run(process.argv.slice(2))
4646
process.exit(0)
4747
} catch (error) {
4848
logger.error(error)

lib/cli/index.js

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,65 @@
11
'use strict'
22

3-
const fs = require('fs')
4-
const path = require('path')
3+
const fs = require('fs')
4+
const path = require('path')
5+
const { parseArgs } = require('util')
56

6-
const editor = require('./editor')
7-
const escodb = require('../escodb')
8-
const OptParser = require('./optparser')
9-
const Store = require('../store')
10-
const util = require('../util')
11-
const Vault = require('../vault')
7+
const editor = require('./editor')
8+
const escodb = require('../escodb')
9+
const Store = require('../store')
10+
const util = require('../util')
11+
const Vault = require('../vault')
1212

1313
const OPTIONS = {
14-
'phrase': Boolean,
15-
'key': Boolean,
16-
'length': Number,
17-
'repeat': Number,
18-
19-
'lower': Number,
20-
'upper': Number,
21-
'number': Number,
22-
'space': Number,
23-
'dash': Number,
24-
'symbol': Number,
25-
'notes': Boolean,
26-
27-
'config': Boolean,
28-
'delete': String,
29-
'delete-globals': Boolean,
30-
'clear': Boolean,
31-
32-
'export': String,
33-
'import': String,
34-
35-
'initpath': Boolean,
36-
'cmplt': String,
37-
'help': Boolean
14+
'phrase': { type: Boolean, short: 'p' },
15+
'key': { type: Boolean, short: 'k' },
16+
'length': { type: Number, short: 'l' },
17+
'repeat': { type: Number, short: 'r' },
18+
19+
'lower': { type: Number },
20+
'upper': { type: Number },
21+
'number': { type: Number },
22+
'space': { type: Number },
23+
'dash': { type: Number },
24+
'symbol': { type: Number },
25+
'notes': { type: Boolean, short: 'n' },
26+
27+
'config': { type: Boolean, short: 'c' },
28+
'delete': { type: String, short: 'x' },
29+
'delete-globals': { type: Boolean, short: 'G' },
30+
'clear': { type: Boolean, short: 'X' },
31+
32+
'export': { type: String, short: 'e' },
33+
'import': { type: String, short: 'i' },
34+
35+
'initpath': { type: Boolean },
36+
'cmplt': { type: String },
37+
'help': { type: Boolean, short: 'h' }
3838
}
3939

40-
const SHORTS = {
41-
'c': '--config',
42-
'e': '--export',
43-
'G': '--delete-globals',
44-
'h': '--help',
45-
'i': '--import',
46-
'k': '--key',
47-
'l': '--length',
48-
'n': '--notes',
49-
'p': '--phrase',
50-
'r': '--repeat',
51-
'x': '--delete',
52-
'X': '--clear'
40+
function parseArgv (args) {
41+
let options = Object.fromEntries(
42+
Object.entries(OPTIONS).map(([key, { type, short }]) => {
43+
let opt = short ? { short } : {}
44+
opt.type = (type === Boolean) ? 'boolean' : 'string'
45+
return [key, opt]
46+
})
47+
)
48+
49+
let { values, positionals } = parseArgs({
50+
args,
51+
options,
52+
strict: true,
53+
allowPositionals: true
54+
})
55+
56+
for (let key in values) {
57+
if (OPTIONS[key].type === Number) {
58+
values[key] = parseInt(values[key], 10)
59+
}
60+
}
61+
62+
return { ...values, service: positionals[0] }
5363
}
5464

5565
class CLI {
@@ -60,8 +70,6 @@ class CLI {
6070
let adapter = escodb.createFileAdapter(pathname)
6171
this._storeParams = { adapter, password: key }
6272

63-
this._parser = new OptParser(OPTIONS, SHORTS, ['service'])
64-
6573
this._out = options.stdout
6674
this._err = options.stderr
6775
this._tty = options.tty
@@ -76,7 +84,8 @@ class CLI {
7684
let { adapter, password } = this._storeParams
7785
this._store = await Store.open(adapter, password)
7886

79-
let params = await this._parser.parse(argv)
87+
let params = parseArgv(argv)
88+
8089
await this.execute(params)
8190
}
8291

lib/cli/optparser.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

lib/cli/scripts/completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_vault_complete() {
44
COMPREPLY=()
55
local word="${COMP_WORDS[COMP_CWORD]}"
6-
local completions="$(vault --cmplt "$word")"
6+
local completions="$(vault --cmplt="$word")"
77
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
88
}
99

lib/cli/scripts/completion.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fi
77
_vault_complete() {
88
local word completions
99
word="$1"
10-
completions="$(vault --cmplt "${word}")"
10+
completions="$(vault --cmplt="${word}")"
1111
reply=( "${(ps:\n:)completions}" )
1212
}
1313

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"preferGlobal": true,
2020
"dependencies": {
2121
"@escodb/core": "github:escodb/core",
22-
"posix-argv-parser": "~1.0.0",
2322
"prompt": "~1.3.0",
2423
"sequin": "~0.1.0",
2524
"ssh-agent": "~0.2.1"

test/cli/completion_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('CLI completion', () => {
1616
})
1717

1818
it('completes option names', async () => {
19-
await helper.call('--cmplt', '--n')
19+
await helper.call('--cmplt=--n')
2020
helper.assertStdout(['--notes', '--number'].join('\n'))
2121
})
2222

test/cli/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CliHelper {
5252
}
5353

5454
call (...args) {
55-
return this.cli.run(['', '', ...args])
55+
return this.cli.run(args)
5656
}
5757

5858
assertStdout (content) {

0 commit comments

Comments
 (0)