prompt user with default value

163 views
Skip to first unread message

Jonathan Pittman

unread,
Mar 3, 2012, 8:41:24 PM3/3/12
to golang-nuts
In a cli application, I want to prompt a user for input of a value, but provide a default value on the input line.  This way a use can either simply confirm the value by pressing return/enter or backspace over the value and enter a different one.

Example...

settings:
  voltage: 110
  amperage: 15

In this case, I want to prompt the user with "  amperage:" and the also provide an editable "15" that can be deleted and changed to another value.  Anyone have any examples?

Kyle Lemons

unread,
Mar 3, 2012, 11:23:08 PM3/3/12
to Jonathan Pittman, golang-nuts

You could do this with one of the numerous terminal packages as you describe; however, I think you will find that users will be just as familiar with something like:

amperage [15]:

It's somewhat common, it's clear, its script able, and it doesn't rely on aspects of the user's terminal or the output stream to work correctly.

(sent from gmail mobile)

Archos

unread,
Mar 4, 2012, 3:38:52 AM3/4/12
to golang-nuts
I've built a package for it:

https://github.com/kless/Go-Inline

The stuff related to question is in "quest". See the test files.

// * * *
import "github.com/kless/Go-Inline/quest"

func main() {
q := quest.NewQuestionByDefault()
defer q.Restore()

q.NewPrompt(" amperage: ").ByDefault(15)
ans, err = q.ReadInt()

}


On Mar 4, 1:41 am, Jonathan Pittman <jonathan.mark.pitt...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages