Esempio n. 1
0
def did_you_mean(cmd, routes):
	"""Print a message along the lines of "did you mean: blah" or "blah not formatted correctly",
	by trying to match the beginning of cmd to the fixed beginnings of each command"""
	best = []
	bestlength = 0
	for fixedstr, pattern, func in routes:
		for l in range(len(fixedstr))[::-1]:
			if cmd.startswith(fixedstr[:l+1]):
				if l+1 > bestlength:
					best = [(fixedstr, func.__doc__)]
					bestlength = l+1
				elif l+1 == bestlength:
					best += [(fixedstr, func.__doc__)]
				break
	for x in best:
		if bestlength == len(x[0]): # if prefix matches the whole fixedstr
			print 'Command incorrectly formatted'
			print 'Basic usage: %s' % docs2dict(x[1])['call']
			print 'For more information, try "help %s"' % x[0]
			return
	print 'Command not found.'
	if bestlength >= MIN_CLOSENESS:
		print 'Did you mean:'
		for x in best:
			print docs2dict(x[1])['call']
Esempio n. 2
0
def man_format_routes(routes):
	"""Generate man formatting text for the routes list given.
	Routes list is as defined in routes.py"""
	findargs = re.compile(r'([A-Z]+):((?:[ \t].*\n)+)') # An re for splitting the 'args' part of the docs
	result = 'Note: For commands that produce output in quiet mode, their entries contain formatting strings. These formatting strings describe the output as per the rules used by\n' + bold('printf',suffix='(3)')
	for name, pattern, func in routes:
		docs = docs2dict(func.__doc__)
		words = docs['call'].split(' ')
		words = [r'\f%s%s' % (word.isupper() and 'I' or 'B', word) for word in words] # Italic all isupper words, else bold.
		call = ' '.join(words) + r'\fR'
		result += heading(call, level=2)
		if 'description' in docs:
			result += fix_text(docs['description'])
		result += indent()
		if 'args' in docs:
			args = docs['args'] + '\n' # Extra \n simplifies the findargs regex somewhat.
			args = findargs.findall(args) # args is now a list of (arg, description)
			for arg, info in args:
				result += heading(italic(arg), level=2)
				if info[-1] != '.':
					info += '.'
				result += fix_text(info)
		if 'output' in docs:
			result += heading(bold('Output format string for quiet mode:'), level=2)
			result += escape_text(docs['output']) + '\n'
		if 'errors' in docs:
			result += heading(bold('Error cases with special return values:'), level=2)
			info = escape_text(docs['errors'])
			info = ' ' + info.replace('\n', '\n ') # Put a space at the start of each line. This makes nroff basically print it with whitespace verbatim
			info += '\n'
			result += info
		result += deindent()
	return result
Esempio n. 3
0
def helping(cmd, api, istty):
	"""call: 'help [COMMAND]'
	description: 'Print help on the given command. If no command is given, prints a list of commands.'
	args: 'COMMAND: Optional arg. If given, it specifies a command to print detailed help on.
	      .In general, the name of a command is the part of the command before the first arg, eg: "create vm", "console".'
	errors: 'Command not found: Returns exit code 3'
	"""

	# Note: mostly not affected by istty as its assumed that if the user is asking for help, they want stuff to be output.

	if cmd == None:
		cmd = ''

	if not cmd:
		print
		print 'Command listing:'
		for name, regex, func in routes:
			print docs2dict(func.__doc__)['call']
		print
		print 'For more detailed help, run "help COMMAND"'
		print 'where COMMAND is the first part of a command, eg. "show ips" or "create vm"'
		return 0

	for name, regex, func in routes+[('help',None,helping)]:
		if name == cmd:
			info = docs2dict(func.__doc__)
			print
			print info['call']
			if 'description' in info:
				print
				print info['description']
			if 'args' in info:
				print
				print info['args']
			if 'output' in info:
				print
				print 'In quiet mode, this function will output in the following format:'
				print info['output']
			if 'errors' in info:
				print
				print 'The following errors have special return codes:'
				print info['errors']
			return 0

	if istty:
		print 'No command found: %s' % cmd
	return 3