[file_utils] unln: improve warn() and error()

This commit is contained in:
Matteo Cypriani 2013-04-09 00:22:29 -04:00
parent d77897fd5c
commit 7879f04f15
1 changed files with 10 additions and 6 deletions

View File

@ -50,21 +50,25 @@ def verbose(*message, **args):
if options["verbose"]:
print(*message, **args)
def warn(*message, prefix="Warning:", **args):
def warn(*message, prefix="Warning!", **args):
"""Prints the message on the error output, prepended by 'prefix'.
The standard output is flushed prior to printing the error message, to
enable the messages to be displayed in the right order.
This function is a simple wrapper around print(), and you can use any
keyword argument you would use with print().
"""
sys.stdout.flush()
print(prefix, *message, file=sys.stderr, **args)
def error(error_code, *message, **args):
"""Prints the message on the error output and exits.
"""Prints the message on the error output and exits with 'error_code'.
This function is a simple wrapper around print(), and you can use any
keyword argument you would use with print().
This function is a simple wrapper around warn(), and you can use any
keyword argument you would use with warn() or print().
"""
print(*message, file=sys.stderr, **args)
warn(*message, prefix="", **args)
sys.exit(error_code)
def quote(string):
@ -81,7 +85,7 @@ options = {
progname = os.path.basename(sys.argv[0])
# Short usage string
usage_string = "Usage:\n\t{} <file1> [file2 ...]\n".format(progname)
usage_string = "Usage:\n\t{} <file1> [file2 ...]".format(progname)
# Error codes
ERR_BAD_USAGE = 1