mirror of https://github.com/cheat/cheat.git
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:
parent
98b254b799
commit
526a9f595a
106
cheat
106
cheat
|
@ -40,7 +40,6 @@ if os.name == 'posix' and 'CHEATCOLORS' in os.environ:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def cheat_directories():
|
def cheat_directories():
|
||||||
"Assembles a list of directories containing cheatsheets."
|
"Assembles a list of directories containing cheatsheets."
|
||||||
default_directories = [DEFAULT_CHEAT_DIR]
|
default_directories = [DEFAULT_CHEAT_DIR]
|
||||||
|
@ -128,8 +127,7 @@ def help(cheatsheets):
|
||||||
Available keyphrases:
|
Available keyphrases:
|
||||||
''').strip())
|
''').strip())
|
||||||
|
|
||||||
print(list_cheatsheets(cheatsheets))
|
print list_cheatsheets(cheatsheets)
|
||||||
|
|
||||||
|
|
||||||
def list_cheatsheets(cheatsheets):
|
def list_cheatsheets(cheatsheets):
|
||||||
"Lists the cheatsheets that are currently available"
|
"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)
|
return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
|
||||||
for key, value in cheatsheets.items()])))
|
for key, value in cheatsheets.items()])))
|
||||||
|
|
||||||
|
|
||||||
def pretty_print(filename):
|
def pretty_print(filename):
|
||||||
"Applies syntax highlighting to a cheatsheet and writes it to stdout"
|
"Applies syntax highlighting to a cheatsheet and writes it to stdout"
|
||||||
try:
|
try:
|
||||||
|
@ -156,10 +153,7 @@ def pretty_print(filename):
|
||||||
fmt = TerminalFormatter()
|
fmt = TerminalFormatter()
|
||||||
highlight(code, lexer, fmt, sys.stdout)
|
highlight(code, lexer, fmt, sys.stdout)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# assemble a keyphrase out of all params passed to the script
|
|
||||||
keyphrase = ' '.join(sys.argv[1:])
|
|
||||||
cheat_dirs = cheat_directories()
|
cheat_dirs = cheat_directories()
|
||||||
|
|
||||||
# verify that we have at least one cheat directory
|
# verify that we have at least one cheat directory
|
||||||
|
@ -171,42 +165,86 @@ def main():
|
||||||
# list the files in the ~/.cheat directory
|
# list the files in the ~/.cheat directory
|
||||||
cheatsheets = cheat_files(cheat_dirs)
|
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
|
# list cheat directories and exit
|
||||||
if keyphrase in ['-d', '--cheat-directories']:
|
class ListDirectories(argparse.Action):
|
||||||
print("\n".join(cheat_directories()))
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
exit()
|
print cheat_directories()
|
||||||
|
parser.exit()
|
||||||
|
|
||||||
# list cheatsheets and exit
|
# list cheatsheets and exit
|
||||||
if keyphrase in ['-l', '--list']:
|
class ListCheatsheets(argparse.Action):
|
||||||
print(list_cheatsheets(cheatsheets))
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
exit()
|
print list_cheatsheets(cheatsheets)
|
||||||
|
parser.exit()
|
||||||
|
|
||||||
# if the user wants to edit a cheatsheet
|
# if the user wants to edit a cheatsheet
|
||||||
if (sys.argv[1]) and (sys.argv[1] in ['-e', '--edit']):
|
class EditSheet(argparse.Action):
|
||||||
edit_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
exit()
|
edit_cheatsheet(values[0], cheatsheets)
|
||||||
|
parser.exit()
|
||||||
|
|
||||||
# print the cheatsheet if it exists
|
# print the cheatsheet if it exists
|
||||||
if keyphrase in cheatsheets:
|
class PrintSheet(argparse.Action):
|
||||||
filename = os.path.join(cheatsheets[keyphrase], keyphrase)
|
def __call__(self, parser, namespace, value, option_string=None):
|
||||||
if USE_PYGMENTS:
|
if not value or value in ['help', 'cheat']:
|
||||||
pretty_print(filename)
|
parser.print_help()
|
||||||
else:
|
elif value in cheatsheets:
|
||||||
with open(filename) as istream:
|
filename = os.path.join(cheatsheets[value], value)
|
||||||
for l in istream:
|
if USE_PYGMENTS:
|
||||||
sys.stdout.write(l)
|
pretty_print(filename)
|
||||||
|
else:
|
||||||
|
with open(filename) as istream:
|
||||||
|
for l in istream:
|
||||||
|
sys.stdout.write(l)
|
||||||
|
|
||||||
# if it does not, say so
|
# if it does not, say so
|
||||||
else:
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue