From d991ec90e3b5d9ec993dcde0ed99fb6539dd5e14 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Fri, 9 Dec 2022 10:07:54 -0800 Subject: new command: `spack pkg grep` to search package files (#34388) It's very common for us to tell users to grep through the existing Spack packages to find examples of what they want, and it's also very common for package developers to do it. Now, searching packages is even easier. `spack pkg grep` runs grep on all `package.py` files in repos known to Spack. It has no special options other than the search string; all options passed to it are forwarded along to `grep`. ```console > spack pkg grep --help usage: spack pkg grep [--help] ... positional arguments: grep_args arguments for grep options: --help show this help message and exit ``` ```console > spack pkg grep CMakePackage | head -3 /Users/gamblin2/src/spack/var/spack/repos/builtin/packages/3dtk/package.py:class _3dtk(CMakePackage): /Users/gamblin2/src/spack/var/spack/repos/builtin/packages/abseil-cpp/package.py:class AbseilCpp(CMakePackage): /Users/gamblin2/src/spack/var/spack/repos/builtin/packages/accfft/package.py:class Accfft(CMakePackage, CudaPackage): ``` ```console > spack pkg grep -Eho '(\S*)\(PythonPackage\)' | head -3 AwsParallelcluster(PythonPackage) Awscli(PythonPackage) Bueno(PythonPackage) ``` ## Return Value This retains the return value semantics of `grep`: * 0 for found, * 1 for not found * >1 for error ## Choosing a `grep` You can set the ``SPACK_GREP`` environment variable to choose the ``grep`` executable this command should use. --- share/spack/qa/completion-test.sh | 3 ++- share/spack/spack-completion.bash | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'share') diff --git a/share/spack/qa/completion-test.sh b/share/spack/qa/completion-test.sh index 95564e2315..e648a2ba77 100755 --- a/share/spack/qa/completion-test.sh +++ b/share/spack/qa/completion-test.sh @@ -42,7 +42,8 @@ do succeeds _spack_completions "${line[@]}" '' # Test that completion with flags works - contains '-h --help' _spack_completions "${line[@]}" - + # all commands but spack pkg grep have -h; all have --help + contains '--help' _spack_completions "${line[@]}" - done <<- EOF $(spack commands --aliases --format=subcommands) EOF diff --git a/share/spack/spack-completion.bash b/share/spack/spack-completion.bash index 5c90b1b5f3..604468aaeb 100755 --- a/share/spack/spack-completion.bash +++ b/share/spack/spack-completion.bash @@ -1450,7 +1450,7 @@ _spack_pkg() { then SPACK_COMPREPLY="-h --help" else - SPACK_COMPREPLY="add list diff added changed removed source hash" + SPACK_COMPREPLY="add list diff added changed removed grep source hash" fi } @@ -1508,6 +1508,15 @@ _spack_pkg_removed() { fi } +_spack_pkg_grep() { + if $list_options + then + SPACK_COMPREPLY="--help" + else + SPACK_COMPREPLY="" + fi +} + _spack_pkg_source() { if $list_options then -- cgit v1.2.3-70-g09d2