Skip to content

Commit 1b2bc07

Browse files
nohehfisaacs
authored andcommitted
feat: add default cli flag
PR-URL: #534 Credit: @nohehf Close: #534 Reviewed-by: @isaacs
1 parent 2a4c3ea commit 1b2bc07

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/bin.ts

100644100755
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import { foregroundChild } from 'foreground-child'
33
import { existsSync } from 'fs'
44
import { jack } from 'jackspeak'
5-
import { globStream } from './index.js'
65
import { version } from '../package.json'
6+
import { globStream } from './index.js'
77

88
const j = jack({
9-
usage: 'glob [options] [<pattern> [<pattern> ...]]'
9+
usage: 'glob [options] [<pattern> [<pattern> ...]]',
1010
})
1111
.description(
1212
`
@@ -24,6 +24,14 @@ const j = jack({
2424
matches as arguments.`,
2525
},
2626
})
27+
.opt({
28+
default: {
29+
short: 'p',
30+
hint: 'pattern',
31+
description: `If no positional arguments are provided, glob will use
32+
this pattern`,
33+
},
34+
})
2735
.flag({
2836
all: {
2937
short: 'A',
@@ -219,7 +227,10 @@ try {
219227
console.log(j.usage())
220228
process.exit(0)
221229
}
222-
if (positionals.length === 0) throw 'No patterns provided'
230+
if (positionals.length === 0 && !values.default)
231+
throw 'No patterns provided'
232+
if (positionals.length === 0 && values.default)
233+
positionals.push(values.default)
223234
const patterns = values.all
224235
? positionals
225236
: positionals.filter(p => !existsSync(p))

test/bin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,24 @@ t.test('prioritizes exact match if exists, unless --all', async t => {
8282
t.match(all.stdout, 'routes/i.tsx\n')
8383
t.match(all.stdout, 'routes/d.tsx\n')
8484
})
85+
86+
t.test('uses default pattern if none provided', async t => {
87+
const cwd = t.testdir({
88+
a: {
89+
'x.y': '',
90+
'x.a': '',
91+
b: {
92+
'z.y': '',
93+
'z.a': '',
94+
},
95+
},
96+
})
97+
98+
const def = await run(['-p', '**/*.y'], { cwd })
99+
t.match(def.stdout, `a${sep}x.y\n`)
100+
t.match(def.stdout, `a${sep}b${sep}z.y\n`)
101+
102+
const exp = await run(['-p', '**/*.y', '**/*.a'], { cwd })
103+
t.match(exp.stdout, `a${sep}x.a\n`)
104+
t.match(exp.stdout, `a${sep}b${sep}z.a\n`)
105+
})

0 commit comments

Comments
 (0)