def _GenerateArguments(self, prefix, message):
    """Gets the arguments to add to the parser that appear in the method body.

    Args:
      prefix: str, A string to prepend to the name of the flag. This is used
        for flags representing fields of a submessage.
      message: The apitools message to generate the flags for.

    Returns:
      {str, calliope.base.Argument}, A map of field name to argument.
    """
    args = []
    field_helps = arg_utils.FieldHelpDocs(message)
    for field in message.all_fields():
      field_help = field_helps.get(field.name, None)
      name = self._GetArgName(field.name, field_help)
      if not name:
        continue
      name = prefix + name
      if field.variant == messages.Variant.MESSAGE:
        sub_args = self._GenerateArguments(name + '.', field.type)
        if sub_args:
          help_text = (name + ': ' + field_help) if field_help else ''
          group = base.ArgumentGroup(help=help_text)
          args.append(group)
          for arg in sub_args:
            group.AddArgument(arg)
      else:
        attributes = yaml_command_schema.Argument(name, name, field_help)
        arg = arg_utils.GenerateFlag(field, attributes, fix_bools=False,
                                     category='MESSAGE')
        if not arg.kwargs.get('help'):
          arg.kwargs['help'] = 'API doc needs help for field [{}].'.format(name)
        args.append(arg)
    return args
Пример #2
0
    def _GenerateResourceArg(self):
        """Gets the flags to add to the parser that appear in the method path.

    Returns:
      {str, calliope.base.Argument}, A map of field name to argument.
    """
        args = []
        field_names = (self.method.request_collection.detailed_params
                       if self.method.request_collection else None)
        if not field_names:
            return args
        field_helps = arg_utils.FieldHelpDocs(self.method.GetRequestType())
        default_help = 'For substitution into: ' + self.method.detailed_path

        # Make a dedicated positional in addition to the flags for each part of
        # the URI path.
        arg = base.Argument(AutoArgumentGenerator.FLAT_RESOURCE_ARG_NAME,
                            nargs='?',
                            help='The GRI for the resource being operated on.')
        args.append(arg)

        for field in field_names:
            arg = base.Argument(
                '--' + field,
                metavar=resource_property.ConvertToAngrySnakeCase(field),
                category='RESOURCE',
                help=field_helps.get(field, default_help))
            args.append(arg)
        return args
Пример #3
0
  def testHelpExtraction(self):
    help_texts = arg_utils.FieldHelpDocs(fm.FakeMessage.InnerMessage)
    self.assertEqual(
        help_texts,
        {'string1': 'the first string',
         'string2': 'The second string. It also happens to have a really long '
                    'description that wraps lines, which is convenient for '
                    'testing that capability.',
         'int1': 'an integer',
         'enum1': 'an enum',
        })

    help_texts = arg_utils.FieldHelpDocs(
        fm.FakeMessage.InnerMessage2.DeeperMessage)
    self.assertTrue(arg_utils.IsOutputField(help_texts['output_string']))
    self.assertTrue(arg_utils.IsOutputField(help_texts['output_string2']))
    self.assertFalse(arg_utils.IsOutputField(help_texts['deep_string']))

    help_texts = arg_utils.FieldHelpDocs(fm.FakeMessage.FakeEnum, 'Values')
    self.assertEqual(
        help_texts,
        {'THING_ONE': 'the first thing',
         'THING_TWO': 'the second thing'
        })