def validate(self): for option in self.subOptions: option.validate() for option in self.subOptions: if option.isApplied() and not self.applied: raise CommandLineUtil.ArgumentError( "%s specified without %s" % (option.getName(), self.longName)) return
def validate(self): # make sure only one of our choices is in the options applied = 0 for opt in self.choices: if opt.isApplied(): if applied: opts = ', '.join( map(lambda x: '--%s' % x.getName(), self.choices)) raise CommandLineUtil.ArgumentError( "Only one of %s allowed" % opts) applied = opt if applied: #NOTE, this allows some sub commands to sneak in applied.validate() else: #Validate them all for option in self.choices: option.validate()
def gen_usage(self, command=None): """ Generate usage info. This includes description, command line, options, and subcommands or arguments. """ command = command or self # Rebuild the command string command_string = self.script_name for cmd in self._build_command_path(command): command_string += ' ' + cmd.name lines = [self._get_version(command_string)] description = CommandLineUtil.wrap_text(command.verbose_description, CONSOLE_WIDTH - 4) lines.extend([' %s' % line for line in description ]) lines.append('\nUsage:') lines.extend(command._gen_usage(' %s ' % command_string)) lines.append('') return '\n'.join(lines)
def gen_usage(self, command=None): """ Generate usage info. This includes description, command line, options, and subcommands or arguments. """ command = command or self # Rebuild the command string command_string = self.script_name for cmd in self._build_command_path(command): command_string += ' ' + cmd.name lines = [self._get_version(command_string)] description = CommandLineUtil.wrap_text(command.verbose_description, CONSOLE_WIDTH - 4) lines.extend([' %s' % line for line in description]) lines.append('\nUsage:') lines.extend(command._gen_usage(' %s ' % command_string)) lines.append('') return '\n'.join(lines)
def gen_command_tree(self): """ Generate the command tree (a show all commands look) """ commands = self.flatten_command_tree(0) max_cmd = 0 for (level, cmd, fullName) in commands: if (len(cmd.name) + level * 2) > max_cmd: max_cmd = len(cmd.name) + level * 2 # column width = longest command + gutter col_width = max_cmd + 2 text_width = CONSOLE_WIDTH - col_width big_indent = ' ' * col_width lines = [] last_level = 0 first_level = 1 for (level, cmd, fullName) in commands: if last_level > level or level == 0: lines.append('') # last_level = level indent = ' ' * level padding = max_cmd - (level * 2) text = CommandLineUtil.wrap_text(cmd.description, text_width) lines.append('%s%-*s %s' % (indent, padding, cmd.name, text[0])) for line in text[1:]: lines.append(big_indent + line) if first_level: lines.append('') lines.append('Available Commands:') lines.append('') first_level = 0 lines.append('\nTo see help on a specific command:\n') lines.append(' %s command [subcommand]... --help\n' % self.script_name) return '\n'.join(lines)
def gen_command_tree(self): """ Generate the command tree (a show all commands look) """ commands = self.flatten_command_tree(0) max_cmd = 0 for (level, cmd,fullName) in commands: if (len(cmd.name) + level*2) > max_cmd: max_cmd = len(cmd.name) + level*2 # column width = longest command + gutter col_width = max_cmd + 2 text_width = CONSOLE_WIDTH - col_width big_indent = ' ' * col_width lines = [] last_level = 0 first_level = 1 for (level, cmd,fullName) in commands: if last_level > level or level == 0: lines.append('') # last_level = level indent = ' ' * level padding = max_cmd - (level * 2) text = CommandLineUtil.wrap_text(cmd.description, text_width) lines.append('%s%-*s %s' % (indent, padding, cmd.name, text[0])) for line in text[1:]: lines.append(big_indent + line) if first_level: lines.append('') lines.append('Available Commands:') lines.append('') first_level = 0 lines.append('\nTo see help on a specific command:\n') lines.append(' %s command [subcommand]... --help\n' % self.script_name) return '\n'.join(lines)