Skip to content

Commit

Permalink
rm --force: Remove contact without confirmation (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibler committed Jul 15, 2017
1 parent 7222db9 commit fbfdf7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
34 changes: 20 additions & 14 deletions khard/khard.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,27 +953,30 @@ def modify_subcommand(selected_vcard, input_from_stdin_or_file, open_editor):
modify_existing_contact(selected_vcard)


def remove_subcommand(selected_vcard):
def remove_subcommand(selected_vcard, force):
"""Remove a contact from the addressbook.
:param selected_vcard: the contact to delete
:type selected_vcard: carddav_object.CarddavObject
:param force: delete without confirmation
:type force: bool
:returns: None
:rtype: None
"""
while True:
input_string = input(
"Deleting contact %s from address book %s. Are you sure? (y/n): "
% (selected_vcard.get_full_name(),
selected_vcard.address_book.name))
if input_string.lower() in ["", "n", "q"]:
print("Canceled")
sys.exit(0)
if input_string.lower() == "y":
break
if not force:
while True:
input_string = input(
"Deleting contact %s from address book %s. Are you sure? (y/n): "
% (selected_vcard.get_full_name(),
selected_vcard.address_book.name))
if input_string.lower() in ["", "n", "q"]:
print("Canceled")
sys.exit(0)
if input_string.lower() == "y":
break
selected_vcard.delete_vcard_file()
print("Contact deleted successfully")
print("Contact %s deleted successfully" % selected_vcard.get_full_name())


def source_subcommand(selected_vcard, editor):
Expand Down Expand Up @@ -1409,13 +1412,16 @@ def parse_args():
sort_parser],
description="move a contact to a different addressbook",
help="move a contact to a different addressbook")
subparsers.add_parser(
remove_parser = subparsers.add_parser(
"remove",
aliases=Actions.get_alias_list_for_action("remove"),
parents=[default_addressbook_parser, default_search_parser,
sort_parser],
description="remove a contact",
help="remove a contact")
remove_parser.add_argument(
"--force", action="store_true",
help="Remove contact without confirmation")
subparsers.add_parser(
"addressbooks",
aliases=Actions.get_alias_list_for_action("addressbooks"),
Expand Down Expand Up @@ -1693,7 +1699,7 @@ def main():
modify_subcommand(selected_vcard, input_from_stdin_or_file,
args.open_editor)
elif args.action == "remove":
remove_subcommand(selected_vcard)
remove_subcommand(selected_vcard, args.force)
elif args.action == "source":
source_subcommand(selected_vcard, config.editor)
elif args.action == "merge":
Expand Down
7 changes: 6 additions & 1 deletion misc/zsh/_khard
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ case $state in
case $words[1] in
addressbooks|abooks)
options+=();;
details|show|source|src|remove|delete|del|rm)
details|show|source|src)
options+=(
$default_addressbook_options $default_search_options $sort_options
);;
Expand Down Expand Up @@ -180,6 +180,11 @@ case $state in
options+=(
$merge_addressbook_options $merge_search_options $sort_options
);;
remove|delete|del|rm)
options+=(
$default_addressbook_options $default_search_options $sort_options
'--force[Remove contact without confirmation]'
);;
esac
# Complete the subcommand options.
_arguments -S $options && ret=0
Expand Down

0 comments on commit fbfdf7e

Please sign in to comment.