Installing Xcode via command line
You might be better off just installing the standalone Developer Command Line Tools (which includes gcc and other standard developer toolchain items). You can get it from Apple's Developer Downloads (which requires a free developer account). Unfortunately there's no direct link, but just search for command line tools and you'll find it (note that there are different downloads for Lion and Mountain Lion).

The download is a DMG. Mount that, and you'll find a .mpkg file, which you can install via the command line: sudo installer -pkg "Command Line Tools.mpkg" -target /.