From 3a0aba0835d63082f93ae0a80ccb5a41da67278d Mon Sep 17 00:00:00 2001
From: lorddavidiii <maximillian.bensch@aei.mpg.de>
Date: Tue, 26 Apr 2022 16:08:56 +0000
Subject: spack spec: add '--format' argument (#27908)

---
 lib/spack/spack/cmd/spec.py       | 12 +++++++++---
 lib/spack/spack/test/cmd/spec.py  |  5 +++++
 share/spack/spack-completion.bash |  2 +-
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/lib/spack/spack/cmd/spec.py b/lib/spack/spack/cmd/spec.py
index d908944976..8ad5f777a5 100644
--- a/lib/spack/spack/cmd/spec.py
+++ b/lib/spack/spack/cmd/spec.py
@@ -34,12 +34,16 @@ for further documentation regarding the spec syntax, see:
     arguments.add_common_arguments(
         subparser, ['long', 'very_long', 'install_status']
     )
-    subparser.add_argument(
+    format_group = subparser.add_mutually_exclusive_group()
+    format_group.add_argument(
         '-y', '--yaml', action='store_const', dest='format', default=None,
         const='yaml', help='print concrete spec as YAML')
-    subparser.add_argument(
+    format_group.add_argument(
         '-j', '--json', action='store_const', dest='format', default=None,
         const='json', help='print concrete spec as JSON')
+    format_group.add_argument(
+        '--format', action='store', default=None,
+        help='print concrete spec with the specified format string')
     subparser.add_argument(
         '-c', '--cover', action='store',
         default='nodes', choices=['nodes', 'edges', 'paths'],
@@ -98,8 +102,10 @@ def spec(parser, args):
             if args.format == 'yaml':
                 # use write because to_yaml already has a newline.
                 sys.stdout.write(output.to_yaml(hash=hash_type))
-            else:
+            elif args.format == 'json':
                 print(output.to_json(hash=hash_type))
+            else:
+                print(output.format(args.format))
             continue
 
         with tree_context():
diff --git a/lib/spack/spack/test/cmd/spec.py b/lib/spack/spack/test/cmd/spec.py
index 74be6f4ca0..3fb25a3ea8 100644
--- a/lib/spack/spack/test/cmd/spec.py
+++ b/lib/spack/spack/test/cmd/spec.py
@@ -79,6 +79,11 @@ def test_spec_json():
     assert 'mpich' in mpileaks
 
 
+def test_spec_format(database, config):
+    output = spec('--format', '{name}-{^mpi.name}', 'mpileaks^mpich')
+    assert output.rstrip('\n') == "mpileaks-mpich"
+
+
 def _parse_types(string):
     """Parse deptypes for specs from `spack spec -t` output."""
     lines = string.strip().split('\n')
diff --git a/share/spack/spack-completion.bash b/share/spack/spack-completion.bash
index cc6ca6ab56..f0c81f11db 100755
--- a/share/spack/spack-completion.bash
+++ b/share/spack/spack-completion.bash
@@ -1670,7 +1670,7 @@ _spack_solve() {
 _spack_spec() {
     if $list_options
     then
-        SPACK_COMPREPLY="-h --help -l --long -L --very-long -I --install-status -y --yaml -j --json -c --cover -N --namespaces --hash-type -t --types -U --fresh --reuse"
+        SPACK_COMPREPLY="-h --help -l --long -L --very-long -I --install-status -y --yaml -j --json --format -c --cover -N --namespaces --hash-type -t --types -U --fresh --reuse"
     else
         _all_packages
     fi
-- 
cgit v1.2.3-70-g09d2