Page 1 of 1

Writing to stderr

PostPosted: Mon Jul 06, 2009 9:56 pm
by Caligari
I understand that I can point print to a different writer, but I don't know what writer to use in order to get my output to appear on stderr.

I'm sure it is something obvious, but I don't know .NET well enough to be able to work out what to do at that end, and I can't immediately see anything in Cobra itself which gives me a stderr stream.

Re: Writing to stderr

PostPosted: Mon Jul 06, 2009 11:02 pm
by Charles
On .NET, Console.error is a reference to stderr. It's type is TextWriter.

You can use the print statement on it like so:
# simple case:
err = Console.error to !
print to err, 'x = [x]'

# redirect
print to err
print "error:"
obj.foo
obj.bar

Or you can use methods on it like: Console.error.writeLine('x = [x]')

I don't know under what circumstance Console.error may be nil/null. It's up to you if you want to check for that.

See also: http://msdn.microsoft.com/en-us/library/system.console.error.aspx