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