Skip to content

Multiple Selection

Krzysztof Czarnecki edited this page Sep 26, 2021 · 4 revisions

Multiple Selection

When using ask(one_or_more=some_list) with a list of values a list will be displayed where you can select more than one value using a TAB key.

Types of multiple selection

  • ask(one_or_more=some_list) - checklist-style, use TAB to select/deselect, Enter to confirm.
  • ask(one_of=some_list) - single-choice list style, use Enter to confirm selection.

Combining input from multiple selection

A common use-case would be creating a combination of comma-separated keywords, e.g. feat, fix: some commit message. If you're working with a standard list of keywords you can use the CommaSeparatedList class to mush them into a consistent commit message:

from mkcommit import CommitMessage, CommaSeparatedList, Keyword, to_stdout, ask

keywords = [
    Keyword("feat", "New feature"),
    Keyword("fix", "Bug fix")
]

selected_keywords = ask("Select keywords", one_or_more=keywords)
selected_keywords_str = CommaSeparatedList(*[k.keyword for k in selected_keywords])
short_commit_message = ask("Short commit message")

def commit():
    return CommitMessage(
        f"{selected_keywords_str}: {short_commit_message}"
    )

if __name__ == "__main__":
    to_stdout(commit())
Clone this wiki locally