Breaking logic in the main function out into smaller components.

This commit is contained in:
Chris Lane 2013-09-14 09:23:33 -04:00
parent 1894ce6297
commit be7bc488b0

54
cheat
View File

@ -88,6 +88,32 @@ def edit_cheatsheet(cheat, cheatsheets):
exit()
def help(cheatsheets):
print dedent('''
Usage: cheat [OPTIONS] <keyphrase>
Examples:
To look up 'tar':
cheat tar
To look up 'git commit' (a subcommand):
cheat git commit
To create a cheatsheet for 'foo':
cheat -c foo
To edit the cheatsheet for 'foo':
cheat -e foo
Available keyphrases:
''').strip()
max_command = max([len(x) for x in cheatsheets.keys()]) + 3
print ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
for key, value in cheatsheets.items()])))
exit()
def pretty_print(filename):
"Applies syntax highlighting to a cheatsheet and writes it to stdout"
try:
@ -123,29 +149,7 @@ def main():
# print help if requested
if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']:
print dedent('''
Usage: cheat [OPTIONS] <keyphrase>
Examples:
To look up 'tar':
cheat tar
To look up 'git commit' (a subcommand):
cheat git commit
To create a cheatsheet for 'foo':
cheat -c foo
To edit the cheatsheet for 'foo':
cheat -e foo
Available keyphrases:
''').strip()
max_command = max([len(x) for x in cheatsheets.keys()]) + 3
print ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
for key, value in cheatsheets.items()])))
exit()
help(cheatsheets)
# create/edit option
option = sys.argv[1].lower()
@ -169,8 +173,8 @@ def main():
create_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
# list cheat directories and exit
if option in ['-d', '--cheat_directories']:
print(' '.join(cheat_directories()))
if option in ['-d', '--cheat-directories']:
print("\n".join(cheat_directories()))
exit()
# print the cheatsheet if it exists