Skip to content

Commit 5cfc8ca

Browse files
authored
Refactor of option_yes_no function (#140)
1 parent ec966b9 commit 5cfc8ca

1 file changed

Lines changed: 104 additions & 80 deletions

File tree

readit/cli.py

Lines changed: 104 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,34 @@
2525

2626

2727
@click.command()
28-
@click.option('--add', '-a', nargs=0, help="Add URLs with space-separated")
29-
@click.option('--tag', '-t', nargs=2, help="Add Tag with space-separated URL")
30-
@click.option('--delete', '-d', nargs=1, help="Remove a URL of particular ID")
31-
@click.option('--clear', '-c', multiple=True, nargs=0, help="Clear bookmarks")
32-
@click.option('--update', '-u', nargs=2, help="Update a URL for specific ID")
33-
@click.option('--search', '-s', nargs=1, help="Search all bookmarks by Tag")
34-
@click.option('--view', '-v', multiple=True, nargs=0, help="Show bookmarks")
35-
@click.option('--openurl', '-o', nargs=1, help="Open URL in Browser")
36-
@click.option('--version', '-V', is_flag=True, help="Check latest version")
37-
@click.option('--export', '-e', multiple=True,
38-
nargs=0, help="Export URLs in csv file")
39-
@click.option('--taglist', '-tl', multiple=True, nargs=0, help="Show all Tags")
40-
@click.option('--urlinfo', '-ui', nargs=1, help="Check particular URL information")
41-
@click.argument('insert', nargs=-1, required=False)
42-
def main(insert, add, tag, delete, clear,
43-
update, search, view, openurl, version, export, taglist, urlinfo):
28+
@click.option("--add", "-a", nargs=0, help="Add URLs with space-separated")
29+
@click.option("--tag", "-t", nargs=2, help="Add Tag with space-separated URL")
30+
@click.option("--delete", "-d", nargs=1, help="Remove a URL of particular ID")
31+
@click.option("--clear", "-c", multiple=True, nargs=0, help="Clear bookmarks")
32+
@click.option("--update", "-u", nargs=2, help="Update a URL for specific ID")
33+
@click.option("--search", "-s", nargs=1, help="Search all bookmarks by Tag")
34+
@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks")
35+
@click.option("--openurl", "-o", nargs=1, help="Open URL in Browser")
36+
@click.option("--version", "-V", is_flag=True, help="Check latest version")
37+
@click.option("--export", "-e", multiple=True, nargs=0, help="Export URLs in csv file")
38+
@click.option("--taglist", "-tl", multiple=True, nargs=0, help="Show all Tags")
39+
@click.option("--urlinfo", "-ui", nargs=1, help="Check particular URL information")
40+
@click.argument("insert", nargs=-1, required=False)
41+
def main(
42+
insert,
43+
add,
44+
tag,
45+
delete,
46+
clear,
47+
update,
48+
search,
49+
view,
50+
openurl,
51+
version,
52+
export,
53+
taglist,
54+
urlinfo,
55+
):
4456
"""
4557
Readit - Command-line bookmark manager tool.
4658
"""
@@ -55,21 +67,29 @@ def main(insert, add, tag, delete, clear,
5567
if is_url_added:
5668
print("Bookmarked.")
5769
else:
58-
print("URL is already bookmarked. Check URL information. See help")
70+
print(
71+
"URL is already bookmarked. Check URL information. See help"
72+
)
5973
else:
6074
print("*" * 12, "\nInvalid URL\n", "*" * 11)
61-
is_url_added = option_yes_no(url)
75+
if option_yes_no():
76+
is_url_added = database_connection.add_url(url)
77+
if is_url_added:
78+
print("Bookmarked.")
79+
else:
80+
print(
81+
"URL is already bookmarked. Check URL information. See help"
82+
)
83+
except Exception as e:
84+
print("*" * 12, "\nInvalid URL\n", "*" * 11)
85+
if option_yes_no():
86+
is_url_added = database_connection.add_url(url)
6287
if is_url_added:
6388
print("Bookmarked.")
6489
else:
65-
print("URL is already bookmarked. Check URL information. See help")
66-
except Exception as e:
67-
print("*" * 12, "\nInvalid URL\n", "*" * 11)
68-
is_url_added = option_yes_no(url)
69-
if is_url_added:
70-
print("Bookmarked.")
71-
else:
72-
print("URL is already bookmarked. Check URL information. See help")
90+
print(
91+
"URL is already bookmarked. Check URL information. See help"
92+
)
7393
elif delete:
7494
is_url_deleted = database_connection.delete_url(delete)
7595
if is_url_deleted:
@@ -90,21 +110,29 @@ def main(insert, add, tag, delete, clear,
90110
if is_url_updated:
91111
print("URL of this id updated.")
92112
else:
93-
print("Provided id is not present or same URL is already in available.")
113+
print(
114+
"Provided id is not present or same URL is already in available."
115+
)
94116
else:
95117
print("*" * 12, "\nInvalid URL\n", "*" * 11)
96-
is_url_updated = update_option_yes_no(url_id, url)
118+
if option_yes_no():
119+
is_url_updated = database_connection.update_url(url_id, url)
120+
if is_url_updated:
121+
print("URL of this id updated.")
122+
else:
123+
print(
124+
"Provided id is not present or same URL is already in available."
125+
)
126+
except Exception as e:
127+
print("*" * 12, "\nInvalid URL\n", "*" * 11)
128+
if option_yes_no():
129+
is_url_updated = database_connection.update_url(url_id, url)
97130
if is_url_updated:
98131
print("URL of this id updated.")
99132
else:
100-
print("Provided id is not present or same URL is already in available.")
101-
except Exception as e:
102-
print("*" * 12, "\nInvalid URL\n", "*" * 11)
103-
is_url_updated = update_option_yes_no(url_id, url)
104-
if is_url_updated:
105-
print("URL of this id updated.")
106-
else:
107-
print("Provided id is not present or same URL is already in available.")
133+
print(
134+
"Provided id is not present or same URL is already in available."
135+
)
108136
elif view:
109137
output.print_bookmarks(database_connection.show_url())
110138
elif openurl:
@@ -131,21 +159,29 @@ def main(insert, add, tag, delete, clear,
131159
if is_url_tagged:
132160
print("Bookmarked URL with tag.")
133161
else:
134-
print("URL is already bookmarked with tag. Check URL information. See help")
162+
print(
163+
"URL is already bookmarked with tag. Check URL information. See help"
164+
)
135165
else:
136166
print("*" * 12, "\nInvalid URL\n", "*" * 11)
137-
is_url_tagged = tag_option_yes_no(tag_name, tagged_url)
167+
if option_yes_no():
168+
is_url_tagged = database_connection.tag_url(tag_name, tagged_url)
169+
if is_url_tagged:
170+
print("Bookmarked URL with tag.")
171+
else:
172+
print(
173+
"URL is already bookmarked with tag. Check URL information. See help"
174+
)
175+
except Exception as t:
176+
print("*" * 12, "\nInvalid URL\n", "*" * 11)
177+
if option_yes_no():
178+
is_url_tagged = database_connection.tag_url(tag_name, tagged_url)
138179
if is_url_tagged:
139180
print("Bookmarked URL with tag.")
140181
else:
141-
print("URL is already bookmarked with tag. Check URL information. See help")
142-
except Exception as t:
143-
print("*" * 12, "\nInvalid URL\n", "*" * 11)
144-
is_url_tagged = tag_option_yes_no(tag_name, tagged_url)
145-
if is_url_tagged:
146-
print("Bookmarked URL with tag.")
147-
else:
148-
print("URL is already bookmarked with tag. Check URL information. See help")
182+
print(
183+
"URL is already bookmarked with tag. Check URL information. See help"
184+
)
149185
elif taglist:
150186
output.print_all_tags(database_connection.list_all_tags())
151187
elif version:
@@ -171,50 +207,38 @@ def main(insert, add, tag, delete, clear,
171207
if is_url_added:
172208
print("Bookmarked.")
173209
else:
174-
print("URL is already bookmarked. Check URL information. See help")
210+
print(
211+
"URL is already bookmarked. Check URL information. See help"
212+
)
175213
else:
176214
print("*" * 12, "\nInvalid URL\n", "*" * 11)
177-
is_url_added = option_yes_no(url)
178-
if is_url_added:
179-
print("Bookmarked.")
180-
else:
181-
print("URL is already bookmarked. Check URL information. See help")
215+
if option_yes_no():
216+
is_url_added = database_connection.add_url(url)
217+
if is_url_added:
218+
print("Bookmarked.")
219+
else:
220+
print(
221+
"URL is already bookmarked. Check URL information. See help"
222+
)
182223

183224
except Exception as e:
184225
print("*" * 12, "\nInvalid URL\n", "*" * 11)
185-
is_url_added = option_yes_no(url)
186-
if is_url_added:
187-
print("Bookmarked.")
188-
else:
189-
print("URL is already bookmarked. Check URL information. See help")
226+
if option_yes_no():
227+
is_url_added = database_connection.add_url(url)
228+
if is_url_added:
229+
print("Bookmarked.")
230+
else:
231+
print(
232+
"URL is already bookmarked. Check URL information. See help"
233+
)
190234

191235

192-
def option_yes_no(url):
236+
def option_yes_no():
193237
"""
194238
Asks whether to bookmark invalid URLs or Offline URLs to database.
195239
"""
196240
option = input("Still you want to bookmark: Yes/No ? ")
197-
if option.lower() in ['yes', 'y']:
198-
return database_connection.add_url(url)
241+
if option.lower() in ["yes", "y"]:
242+
return True
199243
else:
200244
sys.exit(0)
201-
202-
203-
def tag_option_yes_no(tag_name, tagged_url):
204-
"""
205-
Asks whether to tag and bookmark invalid URLs or Offline URLs.
206-
"""
207-
option = input("Still you want to bookmark: Yes/No ? ")
208-
if option.lower() in ['yes', 'y']:
209-
return database_connection.tag_url(tag_name, tagged_url)
210-
else:
211-
sys.exit(0)
212-
213-
214-
def update_option_yes_no(url_id, url):
215-
"""
216-
Asks whether to update existing bookmark with invalid URLs or Offline URLs
217-
"""
218-
option = input("Still you want to update: Yes/No ? ")
219-
if option.lower() in ['yes', 'y']:
220-
return database_connection.update_url(url_id, url)

0 commit comments

Comments
 (0)