How to use the fire.completion function in fire

To help you get started, we’ve selected a few fire examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / python-fire / fire / core.py View on Github external
class_attrs = completion.GetClassAttrsDict(result)
  result_visible = {
      key: value for key, value in result.items()
      if completion.MemberVisible(result, key, value,
                                  class_attrs=class_attrs, verbose=verbose)
  }

  if not result_visible:
    return '{}'

  longest_key = max(len(str(key)) for key in result_visible.keys())
  format_string = '{{key:{padding}s}} {{value}}'.format(padding=longest_key + 1)

  lines = []
  for key, value in result.items():
    if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
                                verbose=verbose):
      line = format_string.format(key=str(key) + ':',
                                  value=_OneLineResult(value))
      lines.append(line)
  return '\n'.join(lines)
github google / python-fire / fire / core.py View on Github external
def CompletionScript(name, component, shell):
  """Returns the text of the completion script for a Fire CLI."""
  return completion.Script(name, component, shell=shell)
github google / python-fire / fire / helputils.py View on Github external
else:
    command = ''

  if inspect.isroutine(component) or inspect.isclass(component):
    spec = inspectutils.GetFullArgSpec(component)
    return _UsageStringFromFullArgSpec(command, spec)

  if isinstance(component, (list, tuple)):
    length = len(component)
    if length == 0:
      return command
    if length == 1:
      return command + '[0]'
    return command + '[0..{cap}]'.format(cap=length - 1)

  completions = completion.Completions(component, verbose)
  if command:
    completions = [''] + completions
  return '\n'.join(command + end for end in completions)
github google / python-fire / fire / core.py View on Github external
"""Returns a dict as a string.

  Args:
    result: The dict to convert to a string
    verbose: Whether to include 'hidden' members, those keys starting with _.
  Returns:
    A string representing the dict
  """

  # We need to do 2 iterations over the items in the result dict
  # 1) Getting visible items and the longest key for output formatting
  # 2) Actually construct the output lines
  class_attrs = completion.GetClassAttrsDict(result)
  result_visible = {
      key: value for key, value in result.items()
      if completion.MemberVisible(result, key, value,
                                  class_attrs=class_attrs, verbose=verbose)
  }

  if not result_visible:
    return '{}'

  longest_key = max(len(str(key)) for key in result_visible.keys())
  format_string = '{{key:{padding}s}} {{value}}'.format(padding=longest_key + 1)

  lines = []
  for key, value in result.items():
    if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
                                verbose=verbose):
      line = format_string.format(key=str(key) + ':',
                                  value=_OneLineResult(value))
      lines.append(line)
github google / python-fire / fire / helptext.py View on Github external
def _GetActionsGroupedByKind(component, verbose=False):
  """Gets lists of available actions, grouped by action kind."""
  groups = ActionGroup(name='group', plural='groups')
  commands = ActionGroup(name='command', plural='commands')
  values = ActionGroup(name='value', plural='values')
  indexes = ActionGroup(name='index', plural='indexes')

  members = completion.VisibleMembers(component, verbose=verbose)
  for member_name, member in members:
    member_name = str(member_name)
    if value_types.IsGroup(member):
      groups.Add(name=member_name, member=member)
    if value_types.IsCommand(member):
      commands.Add(name=member_name, member=member)
    if value_types.IsValue(member):
      values.Add(name=member_name, member=member)

  if isinstance(component, (list, tuple)) and component:
    component_len = len(component)
    if component_len < 10:
      indexes.Add(name=', '.join(str(x) for x in range(component_len)))
    else:
      indexes.Add(name='0..{max}'.format(max=component_len-1))