cheat-fork-echo/cheat

34 lines
896 B
Text
Raw Normal View History

2013-07-31 04:48:07 +02:00
#!/usr/bin/env python
import os
2013-07-31 04:48:07 +02:00
import sys
from cheatsheets import cheat_dir
2013-07-31 04:48:07 +02:00
2013-07-31 05:25:36 +02:00
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
# verify that the cheat directory exists
if not os.path.isdir(cheat_dir):
print >> sys.stderr, 'The cheat directory does not exist.'
exit()
# list the files in the cheat directory
cheatsheets = [f for f in os.listdir(cheat_dir) if '.' not in f]
cheatsheets.sort()
2013-08-10 05:46:34 +02:00
# print help if requested
2013-07-31 05:57:04 +02:00
if keyphrase in ['', 'help', '--help', '-h']:
print "Usage: cheat [keyphrase]\n"
print "Available keyphrases:"
print "\n".join(cheatsheets)
2013-07-31 05:25:36 +02:00
exit()
# print the cheatsheet if it exists
if keyphrase in cheatsheets:
cheatsheet_filename = os.path.join(cheat_dir, keyphrase)
with open(cheatsheet_filename, 'r') as cheatsheet:
print cheatsheet.read()
# if it does not, say so
else:
print 'No cheatsheet found.'