From be7bc488b02502837fa8b61ad612f20f17cd0312 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sat, 14 Sep 2013 09:23:33 -0400 Subject: [PATCH] Breaking logic in the main function out into smaller components. --- cheat | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/cheat b/cheat index 9698804..845fc8e 100755 --- a/cheat +++ b/cheat @@ -88,6 +88,32 @@ def edit_cheatsheet(cheat, cheatsheets): exit() +def help(cheatsheets): + print dedent(''' + Usage: cheat [OPTIONS] + 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] - 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