Skip to content

Commit

Permalink
feat: add --no-stdin documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Jul 9, 2023
1 parent 324a542 commit ef2287e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 22 additions & 1 deletion website/guide/tooling-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Pressing `y` will accept the rewrite, `n` will skip it, `e` will open the file i

Example:

```
```bash
sg scan --interactive
```

Expand Down Expand Up @@ -125,6 +125,27 @@ You can invoke sg, the command-line interface for ast-grep, as a subprocess to s
* For the `run` command, you must specify the language of the StdIn code with `--lang` or `-l` flag. For example: `echo "print('Hello world')" | sg run --lang python`. This is because ast-grep cannot infer code language without file extension.
* Similarly, you can only `scan` StdIn code against _one single rule_, specified by `--rule` or `-r` flag. The rule must match the language of the StdIn code. For example: `echo "print('Hello world')" | sg scan --rule "python-rule.yml"`

### Disable StdIn Mode

Sometimes ast-grep is launched from another process, but you still need it to search files instead of parsing from StdIn.

You can use ast-grep's command line argument `--no-stdin` to disable StdIn mode.
The environment variable `AST_GREP_NO_STDIN` will also disable StdIn mode.


For example, this shell script will start a live ast-grep session via fzf. Note `--no-stdin` is used here to prevernt ast-grep from searching standard input from fzf.

```bash
SG_PREFIX="sg run --color=always --no-stdin -p "
INITIAL_QUERY="${*:-}"
: | fzf --ansi --disabled --query "$INITIAL_QUERY" \
--bind "start:reload:$SG_PREFIX {q}" \
--bind "change:reload:sleep 0.1; $SG_PREFIX {q} || true" \
--delimiter : \
--preview 'bat --color=always {1} --highlight-line {2}' \
--preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
--bind 'enter:become(vim {1} +{2})'
```

## Colorful Output

Expand Down
6 changes: 4 additions & 2 deletions website/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ sg run [OPTIONS] --pattern <PATTERN> [PATHS]...
| | --debug-query | Print query pattern's tree-sitter AST. Requires lang be set explicitly. |
| -l| --lang `<LANG>` | The language of the pattern query. ast-grep will infer the language based on file extension if this option is omitted. |
| -i| --interactive | Start interactive edit session. Code rewrite only happens inside a session. |
| | --accept-all | Apply all rewrite without confirmation if true. |
| -A| --accept-all | Apply all rewrite without confirmation if true. |
| | --json | Output matches in structured JSON text useful for tools like `jq`. Conflicts with interactive|
| | --heading `<HEADING>` | Print file names before each file's matches. Default is auto: print heading for tty but not for piped output [default: auto] [possible values: always, never, auto] |
| | --color `<COLOR>` | Controls output color [default: auto] |
| | --no-ignore `<NO_IGNORE>` | Do not respect hidden file system or ignore files (.gitignore, .ignore, etc.). You can suppress multiple ignore files by passing `no-ignore` multiple times [possible values: hidden, dot, exclude, global, parent, vcs] |
| | --no-stdin | Disable search code from StdIn. See [link](/guide/tooling-overview.html#disable-stdin-mode) |
|-h | --help | Print help |

## scan
Expand All @@ -54,8 +55,9 @@ sg scan [OPTIONS] [PATHS]...
| | --color `<COLOR>`|Controls output color [default: auto]|
| | --report-style `<REPORT_STYLE>` | [default: rich] [possible values: rich, medium, short]
| | --json |Output matches in structured JSON text. This is useful for tools like jq. Conflicts with color and report-style.|
| | --accept-all | Apply all rewrite without confirmation |
| -A | --accept-all | Apply all rewrite without confirmation |
| | --no-ignore `<NO_IGNORE>` | Do not respect ignore files. You can suppress multiple ignore files by passing `no-ignore` multiple times [possible values: hidden, dot, exclude, global, parent, vcs] |
| | --no-stdin | Disable search code from StdIn. See [link](/guide/tooling-overview.html#disable-stdin-mode) |
| -h| --help|Print help|

## test
Expand Down

0 comments on commit ef2287e

Please sign in to comment.