hakre / pcre.php
基于文件列表的 PCRE 模式搜索和替换的 CLI 工具(按行操作)
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^8.4
README
通过文件列表进行 PCRE 模式搜索和替换。
CLI 封装器,围绕 php preg_grep / preg_replace 等,从 stdin/file 获取文件列表进行搜索和替换(按行操作)。
使用方法
usage: pcre.php [<options>] [<search> [<replace>]]
[<options>] --print-paths
Common options
-n, --dry-run do not write changes to files
-v be verbose
--show-match show path, line number and match(es)/replacement(s)
--count-matches count distinct matches in descending order
--print-paths print paths to stdout, one by line and exit
--lines-not | --lines-only <pattern>
filter lines b/f doing search and replace
-m, --multiple search and replace multiple times on the same line
File selection options
-T, --files-from=<file>
read paths of files to operate on from <file>. The
special filename "-" or given no filename will read
files from standard input
each filename is separated by LF ("\n") in a file
--fnmatch <pattern> filter the list of path(s) by fnmatch() pattern
--fnpcre <pattern> filter the list of path(s) by pcre pattern
--only <pattern> only operate on files having a line matching pcre
pattern
--invert invert the meaning of --only pcre pattern match,
operate on files that do not have a line matching
the pcre pattern
--file-match <pattern>
only operate on files their contents (not lines)
matches the pcre pattern
--file-match-invert invert the meaning of --file-match
Operational options
-C <path> run as if pcre.php was started in <path> instead
of the current working directory
--version display version information and exit
示例
这些示例中的大部分都需要安装 git 工具。
打印 PHP 文件路径列表
$ git ls-files '*.php' | pcre.php
pcre.php
matches in 0 out of 1 files (0.0%)
搜索文件列表
$ git ls-files '*.php' | pcre.php '/getopt/'
pcre.php
matches in 1 out of 1 files (100.0%)
并实际显示每个匹配项
$ git ls-files '*.php' | pcre.php --show-match '/getopt/'
pcre.php
352: * Class getopt
354: * static helper class for parsing command-line arguments w/ php getopt()
356: * $opts - array in the form of getopt() return
359: class getopt
364: * @param array $opts getopt result
386: * (hint: maybe using `getopt::arg(...) ?? $default` is more applicable)
416: * index getopt() options and longoptions
453: * @param int $optind getopt parse stop
456: * @see getopt::erropt_msg()
458: public static function erropt(string $options, array $longopts, int $optind, $handler = ['getopt', 'erropt_msg']): bool
460: $idxopt = getopt::idxopt($options, $longopts);
466: if ($index >= $optind) break; // stop at stop (halt of getopt() option parsing)
516: * standard error message callback for @see getopt::erropt() handler
570: $opts = getopt($opt[0], $opt[1], $optind);
571: if (getopt::erropt($opt[0], $opt[1], $optind)) {
575: $opts['verbose'] = getopt::arg(getopt::args($opts, 'v'), true, false);
577: $input = getopt::arg(getopt::args($opts, 'T', 'files-from'), '-', '-');
590: $multiple = getopt::arg(getopt::args($opts, 'm', 'multiple'));
matches in 1 out of 1 files (100.0%)
替换匹配项
$ git ls-files '*.php' | pcre.php -n '/getopt/' 'replace_getopt'
...
使用 git 还原 checkout/reset。或者先使用 dry-run 预览更改(与 show each match 扩展的替换类似)
$ git ls-files *.php | pcre.php --dry-run --show-match '/getopt/' 'replace_getopt'
...
更多示例
$ pcre.php --help
显示使用信息
安装
下一步: 从 Github 安装最新的稳定版 pcre.php
系统上有一个 PHP 7.1+ PHP 可执行文件作为 /usr/bin/env
。
使 pcre.php
可执行(git 在 checkout 时已为您处理)并将其添加到您的路径中。
例如,在克隆后,假设 ~/bin
是您的 $PATH
中的一个目录
$ cp -a pcre.php ~/bin
或者创建一个符号链接(symlink)以使用源版本
$ ln -sT "$(realpath ./pcre.php)" ~/bin/pcre.php
然后调用
$ pcre.php
应仅显示光标闪烁。发送 eof
(ctrl+d / Unicode END_OF_TRANSMISSION)以获取结果
matches in 0 out of 0 files
恭喜,您成功搜索了没有文件!
这确认了安装正常。
从 Github 安装最新的稳定版 pcre.php
从 Github 获取最新稳定存档并提取到 ~/bin/pcre.php
示例操作如下命令
$ unlink ~/bin/pcre.php \
; wget -O - https://github.com/hakre/pcre.php/archive/master.tar.gz \
| tar -xzf- -C ~/bin --strip-components=1 pcre.php-master/pcre.php \
&& pcre.php --version
这将解除可能存在的 ~/bin/pcre.php
的符号链接,然后获取 master 的当前 HEAD 的 git 存档(通常被认为是功能最稳定的版本)并将 pcre.php
提取到 ~/bin
(由当前用户/组拥有,设置了可执行位)并输出版本(您应该看到类似 pcre.php v0.0.7
的标记版本)。
除了 Github 之外,也可以使用来自 git-archive
的项目的任何 tarball,无论是远程还是本地。
请注意,由于这是直接从互联网安装,如果存储库已被篡改,您可能不会获得您认为的软件 - 仅作为警告。
如果使用类似于上述示例的命令覆盖 git checkout 中的 pcre.php
,git 将显示该文件已修改,但 git-diff
可能不会显示更改,因为 .gitattributes
中的 pcre.php ident
用于区分具体版本号和源修订版($ pcre.php --version
)。
开发
这个项目只是满足我的需求,因为我需要自己开发它,所以有一个基准线
- 需要 Git。但尚不清楚具体版本要求
- 它可以从源代码运行,所以
git clone
是获取实用工具的有效方式。
- 它可以从源代码运行,所以
- 需要 Composer。它包含构建和测试
$ composer build
- 启动构建(脚本)$ composer test
- 只运行测试(不进行完整构建),当前测试是烟雾测试和Phpunit测试套件(几天前这些并不可用)。$ composer package
- 可以打包它。运行整个构建然后在当前修订版下打包。
目前pcre.php
是一个单独的PHP文件。因此,补丁、维护甚至开发都需要一些工作。好处是大多数东西可以直接访问。坏处是东西可能会突然改变。
可以通过向smoke.sh
添加测试用例并可能扩展固件(在tests/fixture
中)来轻松测试回归。
任何单元(函数、类)都可以使用Phpunit在最新版本中进行单元测试,它在存储库中预先配置,可以作为开发需求使用composer安装。
到目前为止,构建脚本已经将使用说明从pcre.php
文件移到README.md
中,以便保持更新。
通过创建一个展示功能的拉取请求,最好是提出功能请求。如果它破坏了某些其他功能,只要在拉取请求中适当突出显示,这是可以的,所以是的,这是一个你可以提交伪代码拉取请求的项目。
包
可以使用git archive
命令生成(在修订版)一个包
$ git archive -o hakre-pcre.php.tar.gz HEAD
包将在项目根目录生成,git将其突出显示为新文件。
现有文件正在被覆盖,但应该可以根据修订版重新生成文件。
可以在输出文件名中包含版本标识符的完整打包操作(首先运行测试、构建等)
$ composer package
要手动测试并检查包中的文件列表
$ git archive HEAD . | tar -t
README.md
pcre.php
我认为将README和实际工具打包是可以的,但跳过其余部分。这也经过了测试。