def pw(
    ctx,
    key_pattern,
    user_pattern,
    mode,
    strict_flag,
    user_flag,
    file,
    edit_subcommand,
    gen_subcommand,
):
    """Search for USER and KEY in GPG-encrypted password file."""

    # install silent Ctrl-C handler
    def handle_sigint(*_):
        click.echo()
        ctx.exit(1)

    signal.signal(signal.SIGINT, handle_sigint)

    # invoke a subcommand?
    if gen_subcommand:
        length = int(key_pattern) if key_pattern else None
        generate_password(mode, length)
        return
    elif edit_subcommand:
        launch_editor(ctx, file)
        return

    # verify that database file is present
    if not os.path.exists(file):
        click.echo("error: password store not found at '%s'" % file, err=True)
        ctx.exit(1)

    # load database
    store = Store.load(file)

    # if no user query provided, split key query according to right-most "@" sign (since usernames are typically email addresses)
    if not user_pattern:
        user_pattern, _, key_pattern = key_pattern.rpartition("@")

    # search database
    results = store.search(key_pattern, user_pattern)
    results = list(results)

    # if strict flag is enabled, check that precisely a single record was found
    if strict_flag and len(results) != 1:
        click.echo(
            "error: multiple or no records found (but using --strict flag)", err=True
        )
        ctx.exit(2)

    # raw mode?
    if mode == Mode.RAW:
        for entry in results:
            click.echo(entry.user if user_flag else entry.password)
        return

    # print results
    for idx, entry in enumerate(results):
        # start with key and user
        line = highlight_match(key_pattern, entry.key)
        if entry.user:
            line += ": " + highlight_match(user_pattern, entry.user)

        # add password or copy&paste sucess message
        if mode == Mode.ECHO and not user_flag:
            line += " | " + style_password(entry.password)
        elif mode == Mode.COPY and idx == 0:
            try:
                import pyperclip

                pyperclip.copy(entry.user if user_flag else entry.password)
                result = style_success(
                    "*** %s COPIED TO CLIPBOARD ***"
                    % ("USERNAME" if user_flag else "PASSWORD")
                )
            except ImportError:
                result = style_error('*** PYTHON PACKAGE "PYPERCLIP" NOT FOUND ***')
            line += " | " + result

        # add notes
        if entry.notes:
            if idx == 0:
                line += "\n"
                line += "\n".join("   " + line for line in entry.notes.splitlines())
            else:
                lines = entry.notes.splitlines()
                line += " | " + lines[0]
                if len(lines) > 1:
                    line += " (...)"
        click.echo(line)
Search for USER and KEY in GPG-encrypted password file.