# Buggy code
def main(
    self, args=None, prog_name=None, complete_var=None, standalone_mode=True, **extra
):
    """This is the way to invoke a script with all the bells and
    whistles as a command line application.  This will always terminate
    the application after a call.  If this is not wanted, ``SystemExit``
    needs to be caught.

    This method is also available by directly calling the instance of
    a :class:`Command`.

    .. versionadded:: 3.0
       Added the `standalone_mode` flag to control the standalone mode.

    :param args: the arguments that should be used for parsing.  If not
                 provided, ``sys.argv[1:]`` is used.
    :param prog_name: the program name that should be used.  By default
                      the program name is constructed by taking the file
                      name from ``sys.argv[0]``.
    :param complete_var: the environment variable that controls the
                         bash completion support.  The default is
                         ``"_<prog_name>_COMPLETE"`` with prog name in
                         uppercase.
    :param standalone_mode: the default behavior is to invoke the script
                            in standalone mode.  Click will then
                            handle exceptions and convert them into
                            error messages and the function will never
                            return but shut down the interpreter.  If
                            this is set to `False` they will be
                            propagated to the caller and the return
                            value of this function is the return value
                            of :meth:`invoke`.
    :param extra: extra keyword arguments are forwarded to the context
                  constructor.  See :class:`Context` for more information.
    """
    # If we are in Python 3, we will verify that the environment is
    # sane at this point of reject further execution to avoid a
    # broken script.
    if not PY2:
        _verify_python3_env()
    else:
        _check_for_unicode_literals()

    if args is None:
        args = get_os_args()
    else:
        args = list(args)

    if prog_name is None:
        prog_name = make_str(os.path.basename(sys.argv and sys.argv[0] or __file__))

    # Hook for the Bash completion.  This only activates if the Bash
    # completion is actually enabled, otherwise this is quite a fast
    # noop.
    _bashcomplete(self, prog_name, complete_var)

    try:
        try:
            with self.make_context(prog_name, args, **extra) as ctx:
                rv = self.invoke(ctx)
                if not standalone_mode:
                    return rv
                ctx.exit()
        except (EOFError, KeyboardInterrupt):
            echo(file=sys.stderr)
            raise Abort()
        except ClickException as e:
            if not standalone_mode:
                raise
            e.show()
            sys.exit(e.exit_code)
    except Abort:
        if not standalone_mode:
            raise
        echo("Aborted!", file=sys.stderr)
        sys.exit(1)


# Traceback (IOError)
$ python ./test-click.py --name ob --count 1000 | head -1
Hello ob!
Traceback (most recent call last):
File "./test-click.py", line 13, in <module>
hello()
File "/Users/obonilla/o/click/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/Users/obonilla/o/click/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/Users/obonilla/o/click/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/obonilla/o/click/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "./test-click.py", line 10, in hello
click.echo('Hello %s!' % name)
File "/Users/obonilla/o/click/click/utils.py", line 259, in echo
file.write(message)
File "/Users/obonilla/o/click/click/_compat.py", line 63, in write
return io.TextIOWrapper.write(self, x)
IOError: [Errno 32] Broken pipe

# Fixed code
def main(
    self, args=None, prog_name=None, complete_var=None, standalone_mode=True, **extra
):
    """This is the way to invoke a script with all the bells and
    whistles as a command line application.  This will always terminate
    the application after a call.  If this is not wanted, ``SystemExit``
    needs to be caught.

    This method is also available by directly calling the instance of
    a :class:`Command`.

    .. versionadded:: 3.0
       Added the `standalone_mode` flag to control the standalone mode.

    :param args: the arguments that should be used for parsing.  If not
                 provided, ``sys.argv[1:]`` is used.
    :param prog_name: the program name that should be used.  By default
                      the program name is constructed by taking the file
                      name from ``sys.argv[0]``.
    :param complete_var: the environment variable that controls the
                         bash completion support.  The default is
                         ``"_<prog_name>_COMPLETE"`` with prog name in
                         uppercase.
    :param standalone_mode: the default behavior is to invoke the script
                            in standalone mode.  Click will then
                            handle exceptions and convert them into
                            error messages and the function will never
                            return but shut down the interpreter.  If
                            this is set to `False` they will be
                            propagated to the caller and the return
                            value of this function is the return value
                            of :meth:`invoke`.
    :param extra: extra keyword arguments are forwarded to the context
                  constructor.  See :class:`Context` for more information.
    """
    # If we are in Python 3, we will verify that the environment is
    # sane at this point of reject further execution to avoid a
    # broken script.
    if not PY2:
        _verify_python3_env()
    else:
        _check_for_unicode_literals()

    if args is None:
        args = get_os_args()
    else:
        args = list(args)

    if prog_name is None:
        prog_name = make_str(os.path.basename(sys.argv and sys.argv[0] or __file__))

    # Hook for the Bash completion.  This only activates if the Bash
    # completion is actually enabled, otherwise this is quite a fast
    # noop.
    _bashcomplete(self, prog_name, complete_var)

    try:
        try:
            with self.make_context(prog_name, args, **extra) as ctx:
                rv = self.invoke(ctx)
                if not standalone_mode:
                    return rv
                ctx.exit()
        except (EOFError, KeyboardInterrupt):
            echo(file=sys.stderr)
            raise Abort()
        except ClickException as e:
            if not standalone_mode:
                raise
            e.show()
            sys.exit(e.exit_code)
        except IOError as e:
            if e.errno == errno.EPIPE:
                sys.exit(1)
            else:
                raise
    except Abort:
        if not standalone_mode:
            raise
        echo("Aborted!", file=sys.stderr)
        sys.exit(1)