Refactored for argparse, the help output dont remain the same : too much sheets so I thought that do not print cheatlist every time could be a good things. Added description + epilogue (examples) to the standart argparse output.

This commit is contained in:
roemer_j 2013-09-16 03:26:20 +02:00
parent 98b254b799
commit 526a9f595a
1 changed files with 72 additions and 34 deletions

90
cheat
View File

@ -40,7 +40,6 @@ if os.name == 'posix' and 'CHEATCOLORS' in os.environ:
except ImportError:
pass
def cheat_directories():
"Assembles a list of directories containing cheatsheets."
default_directories = [DEFAULT_CHEAT_DIR]
@ -128,8 +127,7 @@ def help(cheatsheets):
Available keyphrases:
''').strip())
print(list_cheatsheets(cheatsheets))
print list_cheatsheets(cheatsheets)
def list_cheatsheets(cheatsheets):
"Lists the cheatsheets that are currently available"
@ -137,7 +135,6 @@ def list_cheatsheets(cheatsheets):
return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
for key, value in cheatsheets.items()])))
def pretty_print(filename):
"Applies syntax highlighting to a cheatsheet and writes it to stdout"
try:
@ -156,10 +153,7 @@ def pretty_print(filename):
fmt = TerminalFormatter()
highlight(code, lexer, fmt, sys.stdout)
def main():
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
cheat_dirs = cheat_directories()
# verify that we have at least one cheat directory
@ -171,31 +165,31 @@ def main():
# list the files in the ~/.cheat directory
cheatsheets = cheat_files(cheat_dirs)
# @TODO: refactor the below into argparse
# print help
if keyphrase in ['', 'cheat', 'help', '-h', '-help', '--help']:
help(cheatsheets)
exit()
# list cheat directories and exit
if keyphrase in ['-d', '--cheat-directories']:
print("\n".join(cheat_directories()))
exit()
class ListDirectories(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print cheat_directories()
parser.exit()
# list cheatsheets and exit
if keyphrase in ['-l', '--list']:
print(list_cheatsheets(cheatsheets))
exit()
class ListCheatsheets(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print list_cheatsheets(cheatsheets)
parser.exit()
# if the user wants to edit a cheatsheet
if (sys.argv[1]) and (sys.argv[1] in ['-e', '--edit']):
edit_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
exit()
class EditSheet(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
edit_cheatsheet(values[0], cheatsheets)
parser.exit()
# print the cheatsheet if it exists
if keyphrase in cheatsheets:
filename = os.path.join(cheatsheets[keyphrase], keyphrase)
class PrintSheet(argparse.Action):
def __call__(self, parser, namespace, value, option_string=None):
if not value or value in ['help', 'cheat']:
parser.print_help()
elif value in cheatsheets:
filename = os.path.join(cheatsheets[value], value)
if USE_PYGMENTS:
pretty_print(filename)
else:
@ -205,8 +199,52 @@ def main():
# if it does not, say so
else:
print('No cheatsheet found for %s.' % keyphrase)
print >> sys.stderr, ('No cheatsheet found for %s.' % value)
parser.exit(1)
parser.exit()
desc = dedent('''
cheat allows you to create and view interactive cheatsheets on the
command-line. It was designed to help remind *nix system
administrators of options for commands that they use frequently,
but not frequently enough to remember.''').strip()
epi = dedent('''
Examples:
To look up 'tar':
cheat tar
To create or edit the cheatsheet for 'foo':
cheat -e foo
To list the directories on the CHEATPATH
cheat -d
To list the available cheatsheets:
cheat -l
''').strip()
parser = argparse.ArgumentParser(prog='cheat',
description=desc, epilog=epi,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser_group = parser.add_mutually_exclusive_group()
parser_group.add_argument('sheet', metavar='cheatsheet',
action=PrintSheet, type=str, nargs='?',
help='Look at <cheatseet>')
parser_group.add_argument('-c', '--create', metavar='cheatsheet',
action=EditSheet, type=str, nargs=1,
help='Create <cheatsheet>')
parser_group.add_argument('-e', '--edit', metavar='cheatsheet',
action=EditSheet, type=str, nargs=1,
help='Edit <cheatsheet>')
parser_group.add_argument('-l', '--list',
action=ListCheatsheets, nargs=0,
help='List all available cheatsheets')
parser_group.add_argument('-d', '--cheat-directories',
action=ListDirectories, nargs=0,
help='List all current cheat dirs')
args = parser.parse_args()
if __name__ == '__main__':
main()