Skip to content

Instantly share code, notes, and snippets.

@twilson63
Created May 12, 2011 11:04
Show Gist options
  • Save twilson63/968325 to your computer and use it in GitHub Desktop.
Save twilson63/968325 to your computer and use it in GitHub Desktop.
Ask Method for your Cakefile
ask = (question, format, callback) ->
stdin = process.stdin
stdout = process.stdout
stdin.resume()
stdout.write "#{question}:"
stdin.once 'data', (data) ->
data = data.toString().trim()
if format.test(data)
callback data
else
stdout.write "It should match: #{format}\n"
ask(question, format, callback)
@twilson63
Copy link
Author

Usage

If you have coffeescript installed and you would like to create a simple command line interface to capture user input.

You can simply add this method to your cakefile, and then call it in your task. Ex:

 task 'name', 'Ask User for Name', ->
  ask 'What is your name', /.+/, (name) -> console.log name

# This task will print out the name the user entered. 

@tbeseda
Copy link

tbeseda commented Jul 20, 2011

Slick. Exactly what I needed. I really like the format feature.
(found via Forrst, btw)

@twilson63
Copy link
Author

@tbeseda

You can also install via npm

npm install ask -g

then use in your cake files

{ ask } = require('ask')

Check out https://github.com/twilson63/ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment