def FormatInlineFuncDesc(content): initial_whitespace = 37 assert MAX_COLUMN_WIDTH > initial_whitespace content = StringHelpers.Wrap( content, MAX_COLUMN_WIDTH - initial_whitespace) return StringHelpers.LeftJustify(content, initial_whitespace)
def Usage( self, error=None, error_stream=sys.stderr, verbose=False, potential_method_name=None, ): error_stream.write( textwrap.dedent("""\ {desc}{prefix} Usage: """).format( desc=StringHelpers.Wrap(self.ScriptDescription, MAX_COLUMN_WIDTH), prefix='' if not self.ScriptDescriptionPrefix else "\n\n{}".format(self.ScriptDescriptionPrefix), )) indented_stream = StreamDecorator(error_stream, line_prefix=" ") # Narrow the list down if help was requested for a single method entry_points = self.EntryPoints if len(self.EntryPoints) > 1 and len(self.Args) >= 2: potential_method_name = self.Args[1].lower() for entry_point in entry_points: if entry_point.Name.lower() == potential_method_name: entry_points = [ entry_point, ] break # Display a single item or multiple items if len(entry_points) == 1: standard, verbose_desc = self._GenerateUsageInformation( entry_points[0]) # Add the method name if necessary if len(self.EntryPoints) > 1: if '\n' in standard: standard = "\n {}{}".format( entry_points[0].Name, standard, ) else: standard = "{} {}".format( entry_points[0].Name, standard, ) if verbose: standard = "{}\n\n{}".format(standard, verbose_desc) indented_stream.write(" {} {}\n\n".format( self.ScriptName, StringHelpers.LeftJustify(standard, 4), )) else: # ---------------------------------------------------------------------- def FormatInlineFuncDesc(content): initial_whitespace = 37 assert MAX_COLUMN_WIDTH > initial_whitespace content = StringHelpers.Wrap( content, MAX_COLUMN_WIDTH - initial_whitespace) return StringHelpers.LeftJustify(content, initial_whitespace) # ---------------------------------------------------------------------- indented_stream.write( textwrap.dedent("""\ {script_name} <command> [args] Where '<command>' can be one of the following: ---------------------------------------------- """).format(script_name=self.ScriptName, )) for entry_point in entry_points: indented_stream.write(" - {name:<30} {desc}\n".format( name=entry_point.Name, desc=FormatInlineFuncDesc(entry_point.Description), )) indented_stream.write('\n') for entry_point in entry_points: intro = "When '<command>' is '{}':".format(entry_point.Name) standard, verbose_desc = self._GenerateUsageInformation( entry_point) # Insert the function name as an argument if '\n' in standard: multi_line_args = True standard = " {}{}".format( entry_point.Name, StringHelpers.LeftJustify(standard, 4)) else: multi_line_args = False standard = "{} {}".format(entry_point.Name, standard) if verbose: standard = "{}\n\n{}".format( standard, StringHelpers.LeftJustify(verbose_desc, 4, skip_first_line=False), ) indented_stream.write( textwrap.dedent("""\ {intro} {sep} {script_name}{newline}{standard} """).format( intro=intro, sep='-' * len(intro), script_name=self.ScriptName, newline='\n' if multi_line_args else ' ', standard=standard, )) if self.ScriptDescriptionSuffix: error_stream.write("\n{}\n".format( self.ScriptDescriptionSuffix.strip())) if not verbose: error_stream.write( textwrap.dedent("""\ Run "{script_name} {prefix}?" for additional information. """).format( script_name=self.ScriptName, prefix=self.CommandLineArgPrefix, )) if error: error = "\n\nERROR: {}\n".format( StringHelpers.LeftJustify(error, len("ERROR: "))) try: import colorama colorama.init(autoreset=True) error_stream = sys.stderr error_stream.write("{}{}{}".format( colorama.Fore.RED, colorama.Style.BRIGHT, error, )) except ImportError: error_stream.write(error) return -1
def _GenerateUsageInformation(self, entry_point): cols = OrderedDict([ ("Name", 30), ("Type", 15), ("Arity", 8), ("Default", 20), ("Description", 80), ]) # Calculate the verbose template and the left padding associated with verbose # descriptions. col_padding = 2 verbose_template = [] verbose_desc_offset = 0 for index, width in enumerate(six.itervalues(cols)): verbose_template.append("{{{}:<{}}}".format(index, width)) verbose_desc_offset += width + col_padding # Remove the description size from the verbose offset verbose_desc_offset -= width assert verbose_desc_offset < MAX_COLUMN_WIDTH, (verbose_desc_offset, MAX_COLUMN_WIDTH) verbose_template = (col_padding * ' ').join(verbose_template) # Gather the command line and verbose parts command_line = [] verbose = [] if entry_point.Parameters: verbose.append(verbose_template.format(*six.iterkeys(cols))) verbose.append( verbose_template.format( *['-' * col_width for col_width in six.itervalues(cols)])) is_multi_line = len(entry_point.Parameters) > 4 for index, parameter in enumerate(entry_point.Parameters): arg = parameter.Name if parameter.IsSwitch: arg = "{}{}".format( self.CommandLineArgPrefix, arg, ) elif isinstance(parameter.TypeInfo, DictTypeInfo): if not parameter.IsPositional: prefix = "{}{}{}".format( self.CommandLineArgPrefix, arg, self.CommandLineKeywordSeparator, ) else: prefix = '' arg = "{}<tag>{}<value>".format( prefix, self.CommandLineDictTagValueSeparator, ) elif not parameter.IsPositional: arg = "{}{}{}<value>".format( self.CommandLineArgPrefix, arg, self.CommandLineKeywordSeparator, ) if parameter.IsRequired: arg = "<{}>".format(arg) else: arg = "[{}]".format(arg) if parameter.DisplayArity in [ '*', '+', ]: arg += parameter.DisplayArity if is_multi_line: arg = "\n {}".format(arg) elif index: arg = " {}".format(arg) command_line.append(arg) # Verbose if parameter.DefaultValue is not EntryPointInformation.ParameterInfo.NoDefault: if parameter.IsSwitch: default_value = "on" if parameter.DefaultValue else "off" else: default_value = parameter.DefaultValue else: default_value = '' verbose.append( verbose_template.format( parameter.Name, "switch" if parameter.IsSwitch else "Dictionary" if isinstance(parameter.TypeInfo, DictTypeInfo) else parameter.TypeInfo.Desc, parameter.DisplayArity, str(default_value), StringHelpers.LeftJustify( StringHelpers.Wrap( parameter.Description, MAX_COLUMN_WIDTH - verbose_desc_offset), verbose_desc_offset, ).rstrip(), )) constraints = parameter.TypeInfo.ConstraintsDesc if constraints: verbose.append(" - {}".format(constraints)) return ''.join(command_line), '\n'.join(verbose)