Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 `glob` is a file and directory globbing library that supports both checking | |
| 2 whether a path matches a glob and listing all entities that match a glob. | |
| 3 | |
| 4 A "glob" is a pattern designed specifically to match files and directories. Most | |
| 5 shells support globs natively. | |
| 6 | |
| 7 ## Usage | |
| 8 | |
| 9 To construct a glob, just use `new Glob()`. As with `RegExp`s, it's a good idea | |
| 10 to keep around a glob if you'll be using it more than once so that it doesn't | |
| 11 have to be compiled over and over. You can check whether a path matches the glob | |
| 12 using `Glob.matches()`: | |
| 13 | |
| 14 ```dart | |
| 15 import 'package:glob/glob.dart'; | |
| 16 | |
| 17 final dartFile = new Glob("**.dart"); | |
| 18 | |
| 19 // Print all command-line arguments that are Dart files. | |
| 20 void main(List<String> arguments) { | |
| 21 for (var argument in arguments) { | |
| 22 if (dartFile.matches(argument)) print(argument); | |
| 23 } | |
| 24 } | |
| 25 ``` | |
| 26 | |
| 27 You can also list all files that match a glob using `Glob.list()` or | |
| 28 `Glob.listSync()`: | |
| 29 | |
| 30 ```dart | |
| 31 import 'package:glob/glob.dart'; | |
| 32 | |
| 33 final dartFile = new Glob("**.dart"); | |
| 34 | |
| 35 // Recursively list all Dart files in the current directory. | |
| 36 void main(List<String> arguments) { | |
| 37 for (var entity in dartFile.listSync()) { | |
| 38 print(entity.path); | |
| 39 } | |
| 40 } | |
| 41 ``` | |
| 42 | |
| 43 ## Syntax | |
| 44 | |
| 45 The glob syntax hews closely to the widely-known Bash glob syntax, with a few | |
| 46 exceptions that will be outlined below. | |
|
Bob Nystrom
2014/08/27 00:42:04
"will be" -> "are".
nweiz
2014/08/27 01:36:32
Done.
| |
| 47 | |
| 48 In order to be as cross-platform and as close to the Bash syntax as possible, | |
| 49 all globs use POSIX path syntax, including using `/` as a directory separator | |
| 50 regardless of which platform they're on. This is true even for Windows roots; | |
| 51 for example, a glob matching all files in the C drive would be `C:/*`. | |
|
Bob Nystrom
2014/08/27 00:42:04
This means r"C:\foo.txt" will not match the file C
nweiz
2014/08/27 01:36:32
That's correct. r"C:\foo.txt" is already a well-fo
| |
| 52 | |
| 53 ### Match any characters in a filename: `*` | |
| 54 | |
| 55 The `*` character matches any character other than `/`. This means that it can | |
|
Bob Nystrom
2014/08/27 00:42:04
"any" -> "zero or more of any"
nweiz
2014/08/27 01:36:32
Done.
| |
| 56 be used to match all files in a given directory that match a pattern without | |
| 57 also matching files in a subdirectory. For example, `lib/*.dart` will match | |
| 58 `lib/glob.dart` but not `lib/src/utils.dart`. | |
| 59 | |
| 60 ### Match any characters across directories: `**` | |
| 61 | |
| 62 `**` is like `*`, but matches `/` as well. It's useful for matching files or | |
|
Bob Nystrom
2014/08/27 00:42:03
Does this mean "a**b" will match "azzzb", "a/zzb",
nweiz
2014/08/27 01:36:32
Yes. It's useful to use ** without surrounding sep
| |
| 63 listing directories recursively. For example, `lib/**.dart` will match both | |
| 64 `lib/glob.dart` and `lib/src/utils.dart`. | |
| 65 | |
| 66 If `**` appears at the beginning of a glob, it will not match absolute paths. | |
| 67 For example, `**.dart` won't match `/foo.dart`, although `/**.dart` will. This | |
| 68 is to ensure that listing a bunch of paths and checking whether they matches a | |
| 69 glob produces the same results as listing that glob. | |
|
Bob Nystrom
2014/08/27 00:42:04
I don't understand this. Can you explain why this
nweiz
2014/08/27 01:36:32
I explain in the text: it ensures that the list be
Bob Nystrom
2014/08/27 22:16:44
Right, but you don't say *why* the behavior betwee
nweiz
2014/09/02 19:48:15
Added some more clarification.
| |
| 70 | |
| 71 This is an extension to Bash glob syntax that's widely supported by other glob | |
| 72 implementations. | |
| 73 | |
| 74 ### Match any single character: `?` | |
| 75 | |
| 76 The `?` character matches a single character other than `/`. Unlike `*`, it | |
| 77 won't match any more or fewer than one character. For example, `test?.dart` will | |
| 78 match `test1.dart` but not `test10.dart` or `test.dart`. | |
| 79 | |
| 80 ### Match a range of characters: `[...]` | |
| 81 | |
| 82 The `[...]` construction matches one of several characters. It can contain | |
| 83 individual characters, such as `[abc]`, in which case it will match any of those | |
| 84 characters; it can contain ranges, such as `[a-zA-Z]`, in which case it will | |
| 85 match any characters that fall within the range; or it can contain a mix of | |
| 86 both. It will only ever match a single character. For example, | |
| 87 `test[a-zA-Z_].dart` will match `testx.dart`, `testA.dart`, and `test_.dart`, | |
| 88 but not `test-.dart`. | |
| 89 | |
| 90 If it starts with `^` or `!`, the construction will instead match all characters | |
| 91 *not* mentioned. For example, `test[^a-z].dart` will match `test1.dart` but not | |
| 92 `testa.dart`. | |
| 93 | |
| 94 This construction never matches `/`. | |
| 95 | |
| 96 ### Match one of several possibilities: `{...,...}` | |
| 97 | |
| 98 The `{...,...}` construction matches one of several options, each of which is a | |
| 99 glob itself. For example, `lib/{*.dart,src/*}` matches `lib/glob.dart` and | |
| 100 `lib/src/data.txt`. It can contain any number of options greater than one, and | |
| 101 can even contain nested options. | |
| 102 | |
| 103 This is an extension to Bash glob syntax, although it is supported by other | |
| 104 layers of Bash and is often used in conjunction with globs. | |
| 105 | |
| 106 ### Escaping a character: `\` | |
| 107 | |
| 108 The `\` character can be used in any context to escape a character that would | |
| 109 otherwise be semantically meaningful. For example, `\*.dart` matches `*.dart` | |
| 110 but not `test.dart`. | |
| 111 | |
| 112 ### Syntax errors | |
| 113 | |
| 114 Because they're used as part of the shell, almost all strings are valid Bash | |
| 115 globs. This implementation is more picky, and performs some validation to ensure | |
| 116 that globs are meaningful. For instance, unclosed `{` and `[` are disallowed. | |
| 117 | |
| 118 ### Reserved syntax: `(...)` | |
| 119 | |
| 120 Parentheses are reserved in case this package adds support for Bash extended | |
| 121 globbing in the future. For the time being, using them will throw an error | |
| 122 unless they're escaped. | |
| OLD | NEW |