Пример #1
0
    def __call__(self, parser, namespace, values, option_string=None):
      """Render a help document according to the style in values.

      Args:
        parser: The ArgParse object.
        namespace: The ArgParse namespace.
        values: The --document flag ArgDict() value:
          style=STYLE
            The output style. Must be specified.
          title=DOCUMENT TITLE
            The document title.
          notes=SENTENCES
            Inserts SENTENCES into the document NOTES section.
        option_string: The ArgParse flag string.

      Raises:
        parser_errors.ArgumentError: For unknown flag value attribute name.
      """
      base.LogCommand(parser.prog, namespace)
      if default_style:
        # --help
        metrics.Loaded()
      style = default_style
      notes = None
      title = None

      for attributes in values:
        for name, value in six.iteritems(attributes):
          if name == 'notes':
            notes = value
          elif name == 'style':
            style = value
          elif name == 'title':
            title = value
          else:
            raise parser_errors.ArgumentError(
                'Unknown document attribute [{0}]'.format(name))

      if title is None:
        title = command.dotted_name

      metrics.Help(command.dotted_name, style)
      # '--help' is set by the --help flag, the others by gcloud <style> ... .
      if style in ('--help', 'help', 'topic'):
        style = 'text'
      md = io.StringIO(markdown.Markdown(command))
      out = (io.StringIO() if console_io.IsInteractive(output=True)
             else None)

      if style == 'linter':
        meta_data = GetCommandMetaData(command)
      else:
        meta_data = None
      render_document.RenderDocument(style, md, out=out or log.out, notes=notes,
                                     title=title, command_metadata=meta_data)
      metrics.Ran()
      if out:
        console_io.More(out.getvalue())

      sys.exit(0)
Пример #2
0
 def testExternalPagerEnviron(self):
     encoding.SetEncodedValue(os.environ, 'PAGER', 'more')
     console_io.More(self.contents)
     self.popen.assert_called_once_with('more',
                                        stdin=subprocess.PIPE,
                                        shell=True)
     self.assertFalse(self.ran)
Пример #3
0
 def testExternalPagerDefaultWithNoLess(self):
     console_io.More(self.contents)
     self.popen.assert_called_once_with('less',
                                        stdin=subprocess.PIPE,
                                        shell=True)
     self.assertFalse(self.ran)
     self.assertEqual('-R', self.less_env)
Пример #4
0
 def testExternalPagerDefaultWithLess(self):
     encoding.SetEncodedValue(os.environ, 'LESS', '-Z')
     console_io.More(self.contents)
     self.popen.assert_called_once_with('less',
                                        stdin=subprocess.PIPE,
                                        shell=True)
     self.assertFalse(self.ran)
     self.assertEqual('-R-Z', self.less_env)
Пример #5
0
    def Finish(self):
        """Prints the results for non-streaming formats.

    Must be called via super if overridden.
    """
        if self._pager and self._out:
            try:
                console_io.More(self._out.getvalue(), out=self._pager_out)
            # Can happen if caller provided a different out that is not type StringIO.
            except AttributeError:
                pass
Пример #6
0
 def testInternalPagerKWarg(self):
     console_io.More(self.contents, check_pager=False)
     self.assertTrue(self.ran)
Пример #7
0
 def testInternalPagerEnviron(self):
     os.environ['PAGER'] = '-'
     console_io.More(self.contents)
     self.assertTrue(self.ran)
Пример #8
0
 def testInternalPagerDefault(self):
     self.SetExecutables(False)
     console_io.More(self.contents)
     self.assertTrue(self.ran)
Пример #9
0
 def testNotInteractive(self):
     self.SetInteractive(False)
     console_io.More(self.contents)
     self.AssertOutputEquals(self.contents)
     self.assertFalse(self.ran)