Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When printing options in two or more columns, sort them so they read naturally #3211

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions awscli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import argparse
import math
import sys
from awscli.compat import six
from difflib import get_close_matches

from itertools import chain
try:
from itertools import zip_longest
except ImportError:
# Python 2
from itertools import izip_longest as zip_longest

HELP_BLURB = (
"To see help text, you can run:\n"
Expand Down Expand Up @@ -74,9 +80,13 @@ def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
msg = ['Invalid choice, valid choices are:\n']
for i in range(len(action.choices))[::self.ChoicesPerLine]:
# Sort the list so it is in order by columns instead of rows
size = int(math.ceil(len(action.choices) / float(self.ChoicesPerLine)))
chunks = [action.choices[i:i + size] for i in range(0, len(action.choices), size)]
choices = list(chain(*zip_longest(*chunks, fillvalue="")))
for i in range(len(choices))[::self.ChoicesPerLine]:
current = []
for choice in action.choices[i:i+self.ChoicesPerLine]:
for choice in choices[i:i+self.ChoicesPerLine]:
current.append('%-40s' % choice)
msg.append(' | '.join(current))
possible = get_close_matches(value, action.choices, cutoff=0.8)
Expand Down