from __future__ import print_function
import argparse
from getpass import getpass
import logging
import os

import pyperclip

from .db import Database
from .credential import Credential


DEFAULT_DATABASE_PATH = os.path.join(
    os.path.expanduser("~"),
    ".pysswords"
)

DEFAULT_GPG_BINARY = "gpg2"


def get_args(command_args=None):
    """Return args from command line"""
    parser = argparse.ArgumentParser(prog="pysswords")
    parser.add_argument("--init", action="store_true")
    parser.add_argument("--show-password", action="store_true")
    parser.add_argument("-a", "--add", action="store_true")
    parser.add_argument("-l", "--list", action="store_true")
    parser.add_argument("-c", "--clipboard")
    parser.add_argument("-d", "--database", default=DEFAULT_DATABASE_PATH)
    parser.add_argument("--gpg", default=DEFAULT_GPG_BINARY)
    args = parser.parse_args(command_args)
    return args


def get_password(prompt="Password: "):
    for _ in range(3):
        password = getpass(prompt)
        repeat_password = getpass("Type again: ")

        if password == repeat_password:
            return password
        else:
            print("Entries don't match!")
    else:
        raise ValueError("Entries didn't match")


def check_passphrase(database, passphrase):
    sig = database.gpg.sign(
        'testing',
        default_key=database.gpg_key,
        passphrase=passphrase
    )
    if not sig:
        msg = "Wrong passphrase for database at '{}'".format(database.path)
        raise ValueError(msg)
    return sig


def list_credentials(database, show_password=False):
    if show_password:
        passphrase = getpass("Database passphrase: ")
        check_passphrase(database, passphrase)
    for idx, credential in enumerate(database.credentials):
        cred_string = "[{0}] {1}: login={2}, password={3}, {4}".format(
            idx,
            credential.name,
            credential.login,
            "..." if not show_password else database.gpg.decrypt(
                credential.password,
                passphrase=passphrase
            ),
            credential.comments
        )
        print(cred_string)


def add_credential(database):
    credential_name = input("Name: ")
    credential_login = input("Login: ")
    credential_password = input("Password: ")
    credential_comments = input("Comments [optional]: ")
    credential = Credential(
        name=credential_name,
        login=credential_login,
        password=credential_password,
        comments=credential_comments,
    )
    database.add(credential)


def copy_password_to_clipboard(database, credential_name):
    credential = database.credential(name=credential_name)
    if credential:
        passphrase = getpass("Database passphrase: ")
        check_passphrase(database, passphrase)
        pyperclip.copy(
            database.gpg.decrypt(credential.password, passphrase=passphrase)
        )
        print("Password for '{}' copied to clipboard".format(credential.name))


def run(args=None):
    args = get_args() if args is None else args

    if args.init:
        database = Database.create(
            path=args.database,
            passphrase=get_password("Database passphrase: "),
            gpg_bin=args.gpg
        )
        logging.info("Database created at '{}'".format(database.path))
    else:
        database = Database.from_path(
            path=args.database,
            gpg_bin=args.gpg
        )
        if args.add:
            add_credential(database)
        elif args.clipboard:
            copy_password_to_clipboard(
                database=database,
                credential_name=args.clipboard
            )
        else:
            list_credentials(
                database=database,
                show_password=args.show_password
            )


if __name__ == "__main__":
    run()

@@ -450,16 +450,22 @@
 rgument(
+%22-I%22, 
 %22--init%22
@@ -489,71 +489,383 @@
 rue%22
-)%0A    parser.add_argument(%22--show-password%22, action=%22store_true
+,%0A                        help=%22create a new Pysswords database%22)%0A    parser.add_argument(%22--database%22, default=DEFAULT_DATABASE_PATH,%0A                        metavar=%22%3CDATABASE PATH%3E%22,%0A                        help=%22specify path to database%22)%0A    parser.add_argument(%22--show-password%22, action=%22store_true%22,%0A                        help=%22show password as plain text when print
 %22)%0A 
@@ -913,32 +913,83 @@
 ion=%22store_true%22
+,%0A                        help=%22add new credential%22
 )%0A    parser.add
@@ -1004,40 +1004,100 @@
 t(%22-
-l
+d
 %22, %22--
-list%22, action=%22store_true
+delete%22, metavar=%22%3CCREDENTIAL NAME%3E%22,%0A                        help=%22delete credential
 %22)%0A 
@@ -1125,24 +1125,90 @@
 t(%22-
-c
+s
 %22, %22--
-clipboard
+search%22, metavar=%22%3CQUERY%3E%22,%0A                        help=%22search credential
 %22)%0A 
@@ -1236,55 +1236,250 @@
 t(%22-
-d
+l
 %22, %22--
-database%22, default=DEFAULT_DATABASE_PATH
+list%22, action=%22store_true%22,%0A                        help=%22print all credentials as a table%22)%0A    parser.add_argument(%22-c%22, %22--clipboard%22, metavar=%22%3CCREDENTIAL NAME%3E%22,%0A                        help=%22copy credential password to clipboard%22
 )%0A  
@@ -1508,16 +1508,33 @@
 %22--gpg%22,
+ metavar=%22%3Cgpg%3E%22,
  default
@@ -1552,16 +1552,64 @@
 G_BINARY
+,%0A                        help=%22gpg binary name%22
 )%0A    ar

Update args in console interface