How to take a screenshot of the front window from the command line?
The -l option allows capturing a window by ID. It is only shown in the help message and not on the man page.

I don't know any way to find the window IDs that would work with all applications though. This doesn't work with Preview, Activity Monitor, or Chrome:

screencapture -l $(osascript -e 'tell application (path to frontmost application as text) to id of window 1') /tmp/screencapture.png