docs: def expand_var(v, env):
    """ If v is a variable reference (for example: '$myvar'), replace it using the supplied
        env dictionary.
  
    Args:
      v: the variable to replace if needed.
      env: user supplied dictionary.
  
    Raises:
      Exception if v is a variable reference but it is not found in env.
    """
    if len(v) == 0:
      return v
    # Using len() and v[0] instead of startswith makes this Unicode-safe.
    if v[0] == '$':
      v = v[1:]
      if len(v) and v[0] != '$':
        if v in env:
          v = env[v]
        else:
          raise Exception('Cannot expand variable $%s' % v)
    return v