Skip to content

Commit 442baac

Browse files
committed
feat: add --file, --help, --version
Switch to yargs
1 parent d591b6b commit 442baac

4 files changed

Lines changed: 89 additions & 434 deletions

File tree

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ npm install --global ast-grep
2626

2727
## Usage
2828

29+
```shellsession
30+
$ ast-grep --help
31+
Options:
32+
--anonymous, -a Ignore all names in the AST [boolean]
33+
--file, -f Load pattern from a file [string]
34+
--help Show help [boolean]
35+
--version Show version number [boolean]
36+
37+
Examples:
38+
ast-grep.js -a 'fn()' file.js Find all no-arg function calls in
39+
'file.js'.
40+
ast-grep.js -f pattern.js '**/*.js' Match the pattern in 'pattern.js' across
41+
all JS files.
42+
echo 'foo' | ast-grep.js 'pattern' Match 'pattern' on standard input.
43+
```
44+
2945
On standard in:
3046

3147
```shellsession
@@ -41,24 +57,6 @@ On a set of files:
4157
$ ast-grep 'yield* foo();' '**/*.js'
4258
```
4359

44-
## Flags
45-
46-
### `--anonymous`
47-
48-
**Alias**: `-a`
49-
50-
Ignore names, this includes identifiers, types, etc.
51-
52-
## Examples
53-
54-
### Find all no-arg function calls:
55-
56-
```shellsession
57-
$ echo -e 'foo();\nbar();' | ast-grep 'fn()' -a
58-
foo();
59-
bar();
60-
```
61-
6260
## FAQ
6361

6462
### Q. But @azz, `grep` stands for Global Regular Expression Print, this tool doesn't use Regular Expressions!

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"get-stream": "^3.0.0",
2020
"globby": "^7.1.1",
2121
"mem": "^3.0.0",
22-
"mri": "^1.1.0",
23-
"omit-deep-lodash": "^1.0.0"
22+
"omit-deep-lodash": "^1.0.0",
23+
"yargs": "^10.1.1"
2424
},
2525
"scripts": {
2626
"prepublishOnly": "yarn build",

src/bin/ast-grep.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@
22
'use strict';
33

44
import fs from 'fs';
5-
import mri from 'mri';
5+
import yargs from 'yargs';
66
import globby from 'globby';
77
import getStream from 'get-stream';
88

99
import astGrep from '..';
1010

11-
const { _: [grepPattern, ...filePatterns], ...args } = mri(
12-
process.argv.slice(2),
13-
{
14-
alias: {
15-
a: 'anonymous',
16-
},
17-
boolean: ['anonymous'],
18-
}
19-
);
11+
yargs
12+
.option('anonymous', {
13+
alias: 'a',
14+
boolean: true,
15+
describe: 'Ignore all names in the AST',
16+
})
17+
.option('file', {
18+
alias: 'f',
19+
string: true,
20+
describe: 'Load pattern from a file',
21+
})
22+
.option('debug', { string: true, hidden: true })
23+
.help()
24+
.version()
25+
.example(
26+
`$0 -a 'fn()' file.js`,
27+
`Find all no-arg function calls in 'file.js'.`
28+
)
29+
.example(
30+
`$0 -f pattern.js '**/*.js'`,
31+
`Match the pattern in 'pattern.js' across all JS files.`
32+
)
33+
.example(`echo 'foo' | $0 'pattern'`, `Match 'pattern' on standard input.`);
2034

21-
async function run() {
22-
if (!grepPattern) {
23-
console.error('usage: ast-grep pattern [glob ...]');
24-
return 1;
35+
async function run({ _: [grepPattern, ...filePatterns], ...args }) {
36+
if (!grepPattern && !args.file) {
37+
yargs.showHelp();
38+
return 2;
39+
}
40+
if (args.file && grepPattern) {
41+
filePatterns.unshift(grepPattern);
42+
grepPattern = fs.readFileSync(args.file, 'utf8');
2543
}
44+
2645
if (filePatterns.length) {
2746
const files = globby.sync(filePatterns);
2847
if (!files.length) {
@@ -62,10 +81,10 @@ function displayMatches(matches, file) {
6281
}
6382
}
6483

65-
run()
84+
run(yargs.argv)
6685
.then(process.exit)
6786
.catch(error => {
68-
if (args.debug) {
87+
if (yargs.argv.debug) {
6988
console.error(error);
7089
} else {
7190
console.error(`ast-grep: ${error.toString()}`);

0 commit comments

Comments
 (0)