You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's common when writing scripts that install or configure things to need input from the user (at a minimum, something like 'Press Enter to continue'; at a maximum, to be able to prompt the user for input, with an optional default value, and return that value).
Let's use this issue to design how that would look, and I invite suggestions!
I'm quite new to the Open Source Community (read this is my first try) but I have some experience with Go.
So I sketched out a couple of the possibilities here.
You mentioned installation scripts and configuration, I also thought basic CLI menus should be included here.
So here are my thoughts, please feel totally free to change anything or everything.
Specification on Reading User input for Script
Input Types
Read from a user input
Read String (e.g. What's your name ?)
Read Number (e.g. How old are you ?)
Read Array (e.g. List car brands you like)
Protected
Read Password
Menu Items
Read from a Menu
Tell a user to pick from a certain set of options
A menu can be defined as an array of choices
Optional parameters
A message echoed to the user, on the same line as
What is your name? _______
A default value
Would you like to continue [Y/n] default(Y):
An StdErr message and end of command execution for non-opntional reads
When a user types a string instead of number, Pipe.WithError
typeOptionsstruct {
RequiredboolDefaultinterface{}
OnErrorfunc(ininterface{}) *Pipe//WithError
}
opts:=*&Options{
Required : true,
Default : nil,
}
vStr:=p.ReadString("Input your name:", opts) //stringvFlt:=p.ReadNumber("Input your age:", opts) //float64vPwd:=p.ReadPassword("Password: ", opts) //string
(p*Pipe) ReadString(messagestring, optsOptions) *Pipe {
//Execute read//Return New Pipe
}
vArr:=p.ReadArray("Input cars you like", opts)
items:= []string{"1 - Skip", "2 - Continue", "3 - Quit"}
(p*Pipe) DisplayMenu(items []string, optsOptions, ffunc(chosenstring)*Pipe) *Pipe {
//Display List//Read user input//Call f with item picked//Return new pipe created on f
})
Hey @manthantunes, great suggestions, thank you! This looks like a good starting point.
Let's see if we can put together an example program which uses the input feature for some real-world application. (This is often a great way to start designing something: use it!)
For example, a software installer which needs to get the installation directory from the user.
Thinking about use cases before modeling is a very good idea, thank you.
So, I sketched out your example on an installation script, here it is:
p:=script.Echo("Installation Script for ABCD")
welcome, err:=p.String()
fmt.Println(welcome)
dir, _:=p.GetPrompter().Input("Please provide a directory for instalation: ", script.PrompOpts{Default: "/usr/share"}).String()
//Act based on the dir variablestr, _:=script.Exec(fmt.Sprintf("touch %s/install.sh", dir)).String()
When trying to sketch it through the script api, it came to me that things could get confusing if we implement all user input methods under the Pipe struct.
I researched a bit and took the Prompter API as reference for the implementation.
Now instead of having all methods implemented under Pipe, a single method calls a prompter pointer which then implements all different kinds of prompts.
It's interesting how, once they've settled into a coherent design concept, libraries more or less design themselves. Here, for example, there's really nothing in script that doesn't make sense as part of a pipe: it either reads or creates data, and that flows through into some other pipeline stage.
It's not clear that something like PromptMenu fits that pattern, because we almost certainly need to take different code paths depending on the user's choice. I can't think of a way to fit that into the script paradigm, can you?
Although I think that not having it limits the scope of the package, a developer that needs it can easily go get specific packages for this.
Exactly. Sometimes the hardest design decision is when to leave something out.
We might try writing a short install script using one of those menu libraries, to see how it plays with script, and if there's anything we can do to improve integration.
I had imagined PromptDefault as a companion to Prompt, which in addition to the prompt string, would take a default value. But I'm not sure there's much use for a prompt method that doesn't have a default value, so perhaps it should just be Prompt, and if you don't want to set a default, you just supply an empty string for that argument:
For now, I believe the script API should only implement the Prompt method to interact with the user. If demanded, we can check out different prompt handlers, what do you think ?
Thinking of ways we can integrate, I found this really cool interactive prompt package
I am actually doing project in my university, they asked me to contribute
in any one of the project in github so I want to involve in this project so
can you give permission to do?
Activity
mathantunes commentedon Apr 26, 2020
Hi @bitfield ,
I'm quite new to the Open Source Community (read this is my first try) but I have some experience with Go.
So I sketched out a couple of the possibilities here.
You mentioned installation scripts and configuration, I also thought basic CLI menus should be included here.
So here are my thoughts, please feel totally free to change anything or everything.
Specification on Reading User input for Script
Input Types
Read from a user input
Protected
Menu Items
Tell a user to pick from a certain set of options
A menu can be defined as an array of choices
Optional parameters
A message echoed to the user, on the same line as
What is your name? _______
A default value
Would you like to continue [Y/n] default(Y):
An StdErr message and end of command execution for non-opntional reads
When a user types a string instead of number, Pipe.WithError
bitfield commentedon Apr 28, 2020
Hey @manthantunes, great suggestions, thank you! This looks like a good starting point.
Let's see if we can put together an example program which uses the input feature for some real-world application. (This is often a great way to start designing something: use it!)
For example, a software installer which needs to get the installation directory from the user.
mathantunes commentedon Apr 29, 2020
Hi @bitfield,
Thinking about use cases before modeling is a very good idea, thank you.
So, I sketched out your example on an installation script, here it is:
When trying to sketch it through the script api, it came to me that things could get confusing if we implement all user input methods under the Pipe struct.
I researched a bit and took the Prompter API as reference for the implementation.
Now instead of having all methods implemented under Pipe, a single method calls a prompter pointer which then implements all different kinds of prompts.
bitfield commentedon Apr 29, 2020
How about something like this:
mathantunes commentedon Apr 29, 2020
Makes sense to have the implementation under the package itself, since we can start a pipe and then chain from the user input.
So, on the same example, say the script verifies if its binaries have already been installed and gives you a couple of options like:
This requires the PromptMenu call from *Pipe though.
bitfield commentedon Apr 30, 2020
It's interesting how, once they've settled into a coherent design concept, libraries more or less design themselves. Here, for example, there's really nothing in
scriptthat doesn't make sense as part of a pipe: it either reads or creates data, and that flows through into some other pipeline stage.It's not clear that something like PromptMenu fits that pattern, because we almost certainly need to take different code paths depending on the user's choice. I can't think of a way to fit that into the
scriptparadigm, can you?mathantunes commentedon Apr 30, 2020
Interesting point of view. A script usually follows a straight flow to its single objective and falls at errors if anything unexpected happens.
A PromptMenu is more likely to be seen at a CLI, for example, which might not be the scope of the package.
By making a short research, I see there are many other libraries for Menu Prompting.
Although I think that not having it limits the scope of the package, a developer that needs it can easily go get specific packages for this.
So far we have a signature for:
What do you think of the PromptPassword ? (It's basically PromptDefault without Echo)
Where a script might have a predefined password for installation or even ask for a password for later use some functionality
bitfield commentedon Apr 30, 2020
Exactly. Sometimes the hardest design decision is when to leave something out.
We might try writing a short install script using one of those menu libraries, to see how it plays with
script, and if there's anything we can do to improve integration.I had imagined
PromptDefaultas a companion toPrompt, which in addition to the prompt string, would take a default value. But I'm not sure there's much use for a prompt method that doesn't have a default value, so perhaps it should just bePrompt, and if you don't want to set a default, you just supply an empty string for that argument:mathantunes commentedon May 1, 2020
It sure is!
Yes, I think the basic prompt should always have a default value so it's better named as just Prompt.
For now, I believe the
scriptAPI should only implement the Prompt method to interact with the user. If demanded, we can check out different prompt handlers, what do you think ?Thinking of ways we can integrate, I found this really cool interactive prompt package
Which I integrated by
bitfield commentedon May 1, 2020
That looks good! Maybe you'd like to put together a PR with a new
examples/program showing how this can be used?srinivasan-cloud commentedon Oct 21, 2020
hi i need to contribute my work for this issue, please accept my request
bitfield commentedon Oct 22, 2020
What is it that you want, @srinivasan-cloud?
srinivasan-cloud commentedon Oct 23, 2020
bitfield commentedon Oct 24, 2020
We actually have a PR for this in progress: #52. However, it seems to have stalled, so you'd be very welcome to contribute if you like!
srinivasan-cloud commentedon Oct 24, 2020
latiif commentedon Nov 15, 2020
I wonder whether progress with #52 has halted. If so, I'd like to take a stab at it. Just making sure so that work is not duplicated.
bitfield commentedon Nov 16, 2020
Go ahead, @latiif! I'm pretty sure if anything were happening we would have heard about it.
123vivekr commentedon Aug 12, 2021
Hi, is this issue still open?
bitfield commentedon Aug 13, 2021
It seems so!
latiif commentedon Aug 13, 2021
I still have a PR pending: #70
marco-m commentedon Sep 19, 2021
Related: #66