rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 552 forks source link

Shell "select" method #457

Open maxlinc opened 9 years ago

maxlinc commented 9 years ago

I think it would be useful to have a Thor shell command for selecting from a list of options. This is fairly common in CLI applications, e.g.:

$ gem uninstall rspec

Select gem to uninstall:
 1. rspec-2.14.1
 2. rspec-3.0.0
 3. rspec-3.1.0
 4. All versions
>

The closest Thor currently has to this behavior is:

gems = ['rspec-2.14.1', 'rspec-3.0.0', 'rspec-3.1.0', 'All versions']
ask 'Select gem to uninstall: ', { :limited_by => gems }
# Select gem to uninstall:  [rspec-2.14.1, rspec-3.0.0, rspec-3.1.0, All versions]

I do like the autocomplete support to select by value via Thor ask, but it isn't a good solution for long lists or long names since it displays an inline list rather than the question, list, then prompt format in the original example above.

In fact, to support very long lists my ideal implementation would:

Basically something that behaves like this, where both 4 or Auto Scale - List scaling groups are equivalent selections:

Which API would you like to test?
1.  Cloud Identity - List Users
2.  Cloud Identity - Reset user API key
3.  Cloud Identity - List Credentials
4.  Auto Scale - List scaling groups
5.  Cloud Big Data - View profile information
(Press return for more options)
>
6.   Cloud Queues - Get home document
7.   Cloud Databases - List versions
8.   Cloud DNS - Update PTR records
9.   Cloud Identity - Get version details
10.  Cloud Images - List images
(Press return for more options)
>
11.  Cloud Images - Get images schema
12.  Cloud Networks - List Routers
13.  Cloud Databases - List All Compute Hosts
14.  Cloud Networks - List Networks
15.  Cloud Networks - List Networks
(Press return for more options)
>  Auto Scale - List scaling groups
ezekg commented 9 years ago

+1. Please? Prompting for a multiple choice feels messy how it is now. Having an up/down arrow select like Yeoman, in addition to selecting by index, would be beyond perfect.

piotrmurach commented 8 years ago

@maxlinc @ezekg Though your requirement for list menu is quite often necessary in building CLI apps, I think it maybe slightly out of scope for thor. I would prefer for thor to be 'mighty hammer' rather than 'swiss army-knife' for CLI development. I'm not a thor maintainer, therefore I cannot express the views for the future of the library but my own. If you are still interested in this functionality, I've been recently working on library that helps building user prompts tty-prompt.

maxlinc commented 8 years ago

That looks pretty awesome @peter-murach, thanks for sharing.