# Buggy code
def setup_parser(subparser):
    subparser.add_argument(
        "--only",
        default="package,dependencies",
        dest="things_to_install",
        choices=["package", "dependencies"],
        help="""select the mode of installation.
the default is to install the package along with all its dependencies.
alternatively one can decide to install only the package or only
the dependencies""",
    )
    subparser.add_argument(
        "-j",
        "--jobs",
        action="store",
        type=int,
        help="explicitly set number of make jobs. default is #cpus",
    )
    subparser.add_argument(
        "--keep-prefix",
        action="store_true",
        dest="keep_prefix",
        help="don't remove the install prefix if installation fails",
    )
    subparser.add_argument(
        "--keep-stage",
        action="store_true",
        dest="keep_stage",
        help="don't remove the build stage if installation succeeds",
    )
    subparser.add_argument(
        "-n",
        "--no-checksum",
        action="store_true",
        dest="no_checksum",
        help="do not check packages against checksum",
    )
    subparser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        dest="verbose",
        help="display verbose build output while installing",
    )
    subparser.add_argument(
        "--fake",
        action="store_true",
        dest="fake",
        help="fake install. just remove prefix and create a fake file",
    )

    cd_group = subparser.add_mutually_exclusive_group()
    arguments.add_common_arguments(cd_group, ["clean", "dirty"])

    subparser.add_argument(
        "package", nargs=argparse.REMAINDER, help="spec of the package to install"
    )
    subparser.add_argument(
        "--run-tests",
        action="store_true",
        dest="run_tests",
        help="run package level tests during installation",
    )
    subparser.add_argument(
        "--log-format",
        default=None,
        choices=["junit"],
        help="format to be used for log files",
    )
    subparser.add_argument(
        "--log-file",
        default=None,
        help="filename for the log file. if not passed a default will be used",
    )


# Traceback (KeyError)
amklinv@edison10:~/edison/spack/bin> ./spack -vd install xsdk arch=cray-CNL-ivybridge
==> Reading config file /global/u1/a/amklinv/edison/spack/etc/spack/defaults/packages.yaml
==> Reading config file /global/homes/a/amklinv/.spack/cray/packages.yaml
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Acquiring]
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Released]
==> '/usr/bin/ibv_devices'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> Reading config file /global/homes/a/amklinv/.spack/cray/compilers.yaml
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Released]
==> Installing xsdk
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Acquiring]
==> hypre is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/hypre-develop-jwh6ty3izsk6ukytgs2i23qxgo3i4lrq
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Acquiring]
==> superlu-dist is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/superlu-dist-develop-c5jaojzxkpmqdyxz7xts4crqshkhaxd4
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Acquiring]
==> trilinos is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/trilinos-develop-qaujvdhjder4h77x3td7z7jkfi27rcop
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Released]
Traceback (most recent call last):
File "./spack", line 212, in <module>
main(sys.argv)
File "./spack", line 208, in main
_main(args, unknown)
File "./spack", line 174, in _main
return_val = command(parser, args)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/cmd/install.py", line 346, in install
package.do_install(**kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1185, in do_install
**kwargs
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1157, in do_install
rec = spack.store.db.get_record(self.spec)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 81, in converter
return function(self, spec_like, *args, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 554, in get_record
key = self._get_matching_spec_key(spec, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 549, in _get_matching_spec_key
raise KeyError("No such spec in database! %s" % spec)
KeyError: 'No such spec in database! trilinos@develop%gcc@6.1.0+boost~debug+hdf5+hypre+metis~mumps~python+shared~suite-sparse~superlu+superlu-dist+xsdkflags arch=cray-CNL-ivybridge^boost@1.63.0%gcc@6.1.0+atomic+chrono+date_time~debug+filesystem~graph~icu+iostreams+locale+log+math~mpi+multithreaded+program_options~python+random+regex+serialization+shared+signals~singlethreaded+system~taggedlayout+test+thread+timer+wave arch=cray-CNL-ivybridge^bzip2@1.0.6%gcc@6.1.0 arch=cray-CNL-ivybridge^cmake@3.7.1%gcc@6.1.0~doc+ncurses+openssl+ownlibs~qt arch=cray-CNL-ivybridge^glm@0.9.7.1%gcc@6.1.0 arch=cray-CNL-ivybridge^hdf5@1.8.12%gcc@6.1.0+cxx~debug+fortran+mpi+pic+shared~szip~threadsafe arch=cray-CNL-ivybridge^hypre@develop%gcc@6.1.0~internal-superlu+shared arch=cray-CNL-ivybridge^libsigsegv@2.10%gcc@6.1.0 arch=cray-CNL-ivybridge^m4@1.4.17%gcc@6.1.0+sigsegv arch=cray-CNL-ivybridge^matio@1.5.9%gcc@6.1.0+hdf5+zlib arch=cray-CNL-ivybridge^metis@5.1.0%gcc@6.1.0~debug~gdb~idx64~real64+shared arch=cray-CNL-ivybridge^mpich@7.4.1%gcc@6.1.0+hydra+pmi+romio~verbs arch=cray-CNL-ivybridge^ncurses@6.0%gcc@6.1.0 arch=cray-CNL-ivybridge^netcdf@4.4.1%gcc@6.1.0~cdmremote~dap~hdf4 maxdims=1024 maxvars=8192+mpi~parallel-netcdf+shared arch=cray-CNL-ivybridge^openblas@0.2.19%gcc@6.1.0~openmp+pic+shared arch=cray-CNL-ivybridge^openssl@1.0.2j%gcc@6.1.0 arch=cray-CNL-ivybridge^parmetis@4.0.3%gcc@6.1.0~debug~gdb+shared arch=cray-CNL-ivybridge^superlu-dist@develop%gcc@6.1.0 arch=cray-CNL-ivybridge^zlib@1.2.10%gcc@6.1.0+pic arch=cray-CNL-ivybridge'

# Fixed code
def setup_parser(subparser):
    subparser.add_argument(
        "--only",
        default="package,dependencies",
        dest="things_to_install",
        choices=["package", "dependencies"],
        help="""select the mode of installation.
the default is to install the package along with all its dependencies.
alternatively one can decide to install only the package or only
the dependencies""",
    )
    subparser.add_argument(
        "-j",
        "--jobs",
        action="store",
        type=int,
        help="explicitly set number of make jobs. default is #cpus",
    )
    subparser.add_argument(
        "--keep-prefix",
        action="store_true",
        dest="keep_prefix",
        help="don't remove the install prefix if installation fails",
    )
    subparser.add_argument(
        "--keep-stage",
        action="store_true",
        dest="keep_stage",
        help="don't remove the build stage if installation succeeds",
    )
    subparser.add_argument(
        "--restage",
        action="store_true",
        dest="restage",
        help="if a partial install is detected, delete prior state",
    )
    subparser.add_argument(
        "-n",
        "--no-checksum",
        action="store_true",
        dest="no_checksum",
        help="do not check packages against checksum",
    )
    subparser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        dest="verbose",
        help="display verbose build output while installing",
    )
    subparser.add_argument(
        "--fake",
        action="store_true",
        dest="fake",
        help="fake install. just remove prefix and create a fake file",
    )

    cd_group = subparser.add_mutually_exclusive_group()
    arguments.add_common_arguments(cd_group, ["clean", "dirty"])

    subparser.add_argument(
        "package", nargs=argparse.REMAINDER, help="spec of the package to install"
    )
    subparser.add_argument(
        "--run-tests",
        action="store_true",
        dest="run_tests",
        help="run package level tests during installation",
    )
    subparser.add_argument(
        "--log-format",
        default=None,
        choices=["junit"],
        help="format to be used for log files",
    )
    subparser.add_argument(
        "--log-file",
        default=None,
        help="filename for the log file. if not passed a default will be used",
    )