Reference
Type Aliases
columbo
uses type aliases heavily to simplify the annotations for the functions provided by the
library. The following table defines the aliases that are used.
Alias | Value |
---|---|
Answer |
Union[bool, str] |
Answers |
Mapping[str, Answer] |
Interaction |
Union[Echo, Acknowledge, Question] |
MutableAnswers |
MutableMapping[str, Answer] |
OptionList † |
List[str] |
Options |
Union[List[str], Mapping[str, str]] |
Possible * |
Union[T, Literal[_Sentinel]] |
ShouldAsk |
Callable[[Answers], bool] |
StaticOrDynamicValue |
Union[V, Callable[[Answers], V]] |
ValidationResponse |
Union[ValidationSuccess, ValidationFailure] |
Validator |
Callable[[str, Answers], ValidationResponse] |
Note
* Possible
is a special construct used in copy()
methods to indicate that a value was not
provided. Possible
, _Sentinel
, & _NOT_GIVEN
are not exposed by columbo
, but are
documented here for completeness.
† OptionList
is deprecated in favor of Options
. It will be removed in a future release.
Interactions
Acknowledge
Bases: Displayable
Display a message to the user and require the user to press ENTER to continue.
__init__(message, should_ask=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
should_ask |
Optional[ShouldAsk]
|
If |
None
|
copy(*, message=_NOT_GIVEN, should_ask=_NOT_GIVEN)
Create a new instance like this one, potentially with different values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
Possible[StaticOrDynamicValue[str]]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
should_ask |
Possible[Optional[ShouldAsk]]
|
If |
_NOT_GIVEN
|
Returns:
Type | Description |
---|---|
Acknowledge
|
A newly constructed instance with the given values in place of the values of this instance. |
display(answers, no_user_input=False)
Display a message to the user and require the user to press ENTER to continue
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
The value for |
BasicQuestion
Bases: Question[str]
A question with an arbitrary text answer.
__init__(name, message, default, cli_help=None, should_ask=None, validator=None, value_if_not_asked=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
default |
StaticOrDynamicValue[str]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
cli_help |
Optional[str]
|
Optional help message to be displayed for command line interface. |
None
|
should_ask |
Optional[ShouldAsk]
|
If |
None
|
validator |
Optional[Validator]
|
Callable that will validate the response given by the user. A ValidationSuccess object indicates success and a ValidationFailure object indicates failure. |
None
|
value_if_not_asked |
Optional[str]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
A value for |
ask(answers, no_user_input=False)
Prompt the user with this question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
str
|
The answer to the question. |
Raises:
Type | Description |
---|---|
ValueError
|
Default value did not satisfy the validator. Or the instance was misconfigured in some way. |
copy(*, name=_NOT_GIVEN, message=_NOT_GIVEN, default=_NOT_GIVEN, cli_help=_NOT_GIVEN, should_ask=_NOT_GIVEN, validator=_NOT_GIVEN, value_if_not_asked=_NOT_GIVEN)
Create a new instance like this one, potentially with different values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Possible[str]
|
The identifier that will be used as the key to access this question's answer. |
_NOT_GIVEN
|
message |
Possible[StaticOrDynamicValue[str]]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
default |
Possible[StaticOrDynamicValue[str]]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
cli_help |
Possible[Optional[str]]
|
Optional help message to be displayed for command line interface. |
_NOT_GIVEN
|
should_ask |
Possible[Optional[ShouldAsk]]
|
If |
_NOT_GIVEN
|
validator |
Possible[Optional[Validator]]
|
Callable that will validate the response given by the user. None indicates that validation was successful. Otherwise, a string containing details of the error that caused the validation failure. |
_NOT_GIVEN
|
value_if_not_asked |
Possible[Optional[str]]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
_NOT_GIVEN
|
Returns:
Type | Description |
---|---|
BasicQuestion
|
A newly constructed instance with the given values in place of the values of this instance. |
validate(value, answers)
Validate the value (a new answer).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
answers |
Answers
|
The answers that have been provided this far. |
required |
Returns:
Type | Description |
---|---|
ValidationResponse
|
A ValidationFailure or ValidationSuccess object. |
Raises:
Type | Description |
---|---|
ValueError
|
The value for |
Choice
Bases: Question[str]
A question with a set of possible answers.
__init__(name, message, options, default, cli_help=None, should_ask=None, value_if_not_asked=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
options |
StaticOrDynamicValue[Options]
|
The set of possible answers to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. If the value is a |
required |
default |
StaticOrDynamicValue[str]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
cli_help |
Optional[str]
|
Optional help message to be displayed for command line interface. |
None
|
should_ask |
Optional[ShouldAsk]
|
If |
None
|
value_if_not_asked |
Optional[str]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
A value for |
ask(answers, no_user_input=False)
Prompt the user with this question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
str
|
The answer to the question. |
Raises:
Type | Description |
---|---|
ValueError
|
The instance was misconfigured in some way. |
copy(*, name=_NOT_GIVEN, message=_NOT_GIVEN, options=_NOT_GIVEN, default=_NOT_GIVEN, cli_help=_NOT_GIVEN, should_ask=_NOT_GIVEN, value_if_not_asked=_NOT_GIVEN)
Create a new instance like this one, potentially with different values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Possible[str]
|
The identifier that will be used as the key to access this question's answer. |
_NOT_GIVEN
|
message |
Possible[StaticOrDynamicValue[str]]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
options |
Possible[StaticOrDynamicValue[Options]]
|
The set of possible answers to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
default |
Possible[StaticOrDynamicValue[str]]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
cli_help |
Possible[Optional[str]]
|
Optional help message to be displayed for command line interface. |
_NOT_GIVEN
|
should_ask |
Possible[Optional[ShouldAsk]]
|
If |
_NOT_GIVEN
|
value_if_not_asked |
Possible[Optional[str]]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
_NOT_GIVEN
|
Returns:
Type | Description |
---|---|
Choice
|
A newly constructed instance with the given values in place of the values of this instance. |
validate(value, answers)
Validate the value (a new answer).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
answers |
Answers
|
The answers that have been provided this far. |
required |
Returns:
Type | Description |
---|---|
ValidationResponse
|
A ValidationFailure or ValidationSuccess object. |
Raises:
Type | Description |
---|---|
ValueError
|
The value for |
Confirm
Bases: Question[bool]
A question with a yes or no answer.
__init__(name, message, default=False, cli_help=None, should_ask=None, value_if_not_asked=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
default |
StaticOrDynamicValue[bool]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
False
|
cli_help |
Optional[str]
|
Optional help message to be displayed for command line interface. |
None
|
should_ask |
Optional[ShouldAsk]
|
If |
None
|
value_if_not_asked |
Optional[bool]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
A value for |
ask(answers, no_user_input=False)
Prompt the user with this question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
bool
|
The answer to the question. |
Raises:
Type | Description |
---|---|
ValueError
|
The instance was misconfigured in some way. |
copy(*, name=_NOT_GIVEN, message=_NOT_GIVEN, default=_NOT_GIVEN, cli_help=_NOT_GIVEN, should_ask=_NOT_GIVEN, value_if_not_asked=_NOT_GIVEN)
Create a new instance like this one, potentially with different values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Possible[str]
|
The identifier that will be used as the key to access this question's answer. |
_NOT_GIVEN
|
message |
Possible[StaticOrDynamicValue[str]]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
default |
Possible[StaticOrDynamicValue[bool]]
|
The default answer to the question. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
cli_help |
Possible[Optional[str]]
|
Optional help message to be displayed for command line interface. |
_NOT_GIVEN
|
should_ask |
Possible[Optional[ShouldAsk]]
|
If |
_NOT_GIVEN
|
value_if_not_asked |
Possible[Optional[bool]]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
_NOT_GIVEN
|
Returns:
Type | Description |
---|---|
Confirm
|
A newly constructed instance with the given values in place of the values of this instance. |
Echo
Bases: Displayable
Display a message to the user.
__init__(message, should_ask=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
should_ask |
Optional[ShouldAsk]
|
If |
None
|
copy(*, message=_NOT_GIVEN, should_ask=_NOT_GIVEN)
Create a new instance like this one, potentially with different values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
Possible[StaticOrDynamicValue[str]]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
_NOT_GIVEN
|
should_ask |
Possible[Optional[ShouldAsk]]
|
If |
_NOT_GIVEN
|
Returns:
Type | Description |
---|---|
Echo
|
A newly constructed instance with the given values in place of the values of this instance. |
display(answers, no_user_input=False)
Display a message to the user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
Has no effect because no user input is expected. Default: |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
The value for |
Question
Bases: ABC
, Generic[QuestionValue]
Base class for a prompt to the user that produces an answer.
__init__(name, message, cli_help=None, should_ask=None, value_if_not_asked=None)
Initialize an instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The identifier that will be used as the key to access this question's answer. |
required |
message |
StaticOrDynamicValue[str]
|
The message to be displayed to the user. If the value is callable, the argument passed in will be the answers that have been provided this far. |
required |
cli_help |
Optional[str]
|
Optional help message to be displayed for command line interface. |
None
|
should_ask |
Optional[ShouldAsk]
|
If |
None
|
value_if_not_asked |
Optional[QuestionValue]
|
If provided and if should_ask is being used, this value will be recorded as an answer if should_ask evaluates to False. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
A value for |
ask(answers, no_user_input=False)
abstractmethod
Prompt the user with this question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
no_user_input |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
Answer
|
The answer to the question. |
should_ask(answers)
Should the user be asked this question.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
answers |
Answers
|
The answers that have been provided this far. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Raises:
Type | Description |
---|---|
ValueError
|
The value for |
Functions
format_cli_help(interactions, parser_name=None)
Produce CLI help text for a given set of interactions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interactions |
Collection[Interaction]
|
Interactions that should be turned into CLI arguments. |
required |
parser_name |
Optional[str]
|
Optional name to be used in help text. If omitted, the name of the process will be used. |
None
|
Raises:
Type | Description |
---|---|
DuplicateQuestionNameException
|
One of the given questions attempted to reuse a name. |
ValueError
|
One of the given |
get_answers(interactions, answers=None, no_user_input=False)
Iterates over collection of interactions, invoking interaction specific behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interactions |
Collection[Interaction]
|
Collection of interactions to present the user with. |
required |
answers |
Optional[Answers]
|
An initial dictionary of answers to start from. |
None
|
no_user_input |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
MutableAnswers
|
Dictionary of answers. |
Raises:
Type | Description |
---|---|
DuplicateQuestionNameException
|
One of the given questions attempted to reuse a name. When a value is provided for |
ValueError
|
One of the given |
parse_args(interactions, args=None, exit_on_error=True, answers=None, parser_name=None)
Parse command line argument for the given interactions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interactions |
Collection[Interaction]
|
Interactions that should be turned into CLI arguments. |
required |
args |
Optional[Sequence[str]]
|
Arguments to parse. If |
None
|
exit_on_error |
bool
|
If |
True
|
answers |
Optional[Answers]
|
An initial dictionary of answers to start from. |
None
|
parser_name |
Optional[str]
|
Optional name to be used in error text. If omitted, the name of the process will be used. |
None
|
Returns:
Type | Description |
---|---|
MutableAnswers
|
Answers based on the given arguments. |
Raises:
Type | Description |
---|---|
SystemExit
|
A value passed to CLI argument was not valid and |
CliException
|
A value passed to CLI argument was not valid and |
DuplicateQuestionNameException
|
One of the given questions attempted to reuse a name. When a value is provided for |
ValueError
|
One of the given |
Exceptions
CliException
ColumboException
Bases: Exception
Base exception for exceptions raised by Columbo
DuplicateQuestionNameException
Bases: ColumboException
Multiple questions use the same name.