How to accept Xcode license in a automation way?

You have not agreed to the Xcode license agreements. You must agree to both license agreements below in order to use Xcode. Hit the Enter key to view the license agreements at '/opt/homebrew-cask/Caskroom/xcode/7.1/Xcode.app/Contents/Resources/English.lproj/License.rtf'

By pressing Enter , the screen will display the license and at the bottom of screen, it prompts
Software License Agreements Press 'space' for more, or 'q' to quit
By pressing q , we can quickly mark the license as read, then the final prompt shows up

By typing 'agree' you are agreeing to the terms of the software license agreements. Type 'print' to print them or anything else to cancel, [agree, print, cancel]

By pressing agree , the license was finally accepted. But the whole process need human's interaction, is there a way to accept the license automatically? I tried the following commands, none is work.

sudo echo -e "\nq\nagree\n" | sudo xcodebuild -license sudo echo -e "agree" | sudo xcodebuild -license