Ripgrep: Cannot search for a pattern that starts with a dash

Created on 6 Oct 2017  Â·  2Comments  Â·  Source: BurntSushi/ripgrep

If I want to search for -radius in my code, I would think to type this:

rg "-radius"

but despite the quotes this seems to trip up the parser. I see:

error: The following required arguments were not provided:
    <PATTERN>

USAGE:

    rg [options] PATTERN [path ...]
    rg [options] [-e PATTERN ...] [-f FILE ...] [path ...]
    rg [options] --files [path ...]
    rg [options] --type-list

For more information try --help

I'm on macOS 10.12.6

$ rg --version
ripgrep 0.6.0
-AVX -SIMD

Most helpful comment

This is standard behaviour for almost any UNIX command-line tool.

If you run rg -radius (for example) the tool has no way of telling whether you meant the options -r -a -d -i -u -s or the literal string -radius — it just has to assume the former.

Quoting won't help you, because rg doesn't see those quotes; they're handled entirely by the shell.

There is a standard way of getting around this, though: the special -- parameter. Whenever a tool with UNIX-style argument handling sees --, it knows that it should immediately stop processing options and treat everything that comes after it as a regular operand. So:

rg -- -radius

Alternatively, you can do this:

rg -e -radius

But the -- method is conventional and works almost everywhere.

All 2 comments

This is standard behaviour for almost any UNIX command-line tool.

If you run rg -radius (for example) the tool has no way of telling whether you meant the options -r -a -d -i -u -s or the literal string -radius — it just has to assume the former.

Quoting won't help you, because rg doesn't see those quotes; they're handled entirely by the shell.

There is a standard way of getting around this, though: the special -- parameter. Whenever a tool with UNIX-style argument handling sees --, it knows that it should immediately stop processing options and treat everything that comes after it as a regular operand. So:

rg -- -radius

Alternatively, you can do this:

rg -e -radius

But the -- method is conventional and works almost everywhere.

ah, right, I see what you mean. Thanks for the detailed explanation. I will close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timotheecour picture timotheecour  Â·  3Comments

borekb picture borekb  Â·  3Comments

bastienbc picture bastienbc  Â·  3Comments

wsdjeg picture wsdjeg  Â·  3Comments

lexicalunit picture lexicalunit  Â·  3Comments