ask_lib package#

Submodules#

ask_lib.ask module#

ask_lib.ask.ask(message, default_option=AskResult.YES, flag: AskFlag | None = None) bool#

Ask user’s confirmation about an action.

Parameters:
  • message (str) – The question that will be asked to the user.

  • default_option (str, optional) – Default response if users does not give a valid response. Must be AskResult.YES, or AskResult.NO.

  • flag (AskFlag | None, optional) – If not None; does not ask the question and return the flag’s truth value.

Returns:

True if the user accepted / False if he didn’t.

True if the user’s input is unsupported and default_option is AskResult.YES False if the user’s input is unsupported and default_option is AskResult.NO True if flag is AskFlag.YES_TO_ALL False if flag is AskFlag.NO_TO_ALL default_option if flag is AskFlag.DEFAULT_TO_ALL

Return type:

bool

Notes

If flag is not None, the question will not be displayed.

Raises:
  • ValueError – If the default option ins’t AskResult.YES, or AskResult.NO.

  • ValueError – If the flag isn’t AskFlag.YES_TO_ALL, AskFlag.NO_TO_ALL, AskFlag.DEFAULT_TO_ALL, or None

ask_lib.ask_flag module#

class ask_lib.ask_flag.AskFlag(value)#

Bases: Enum

Each member represent a flag that could be given by the user when the CLI was started.

Example:

import sys
import ask_lib

flag = ask_lib.AskFlag.YES_TO_ALL if "-Y" in sys.argv else None

if ask_lib.ask("Delete file ?", flag=flag):
    print("Test")
DEFAULT_TO_ALL = 'default_to_all'#
NO_TO_ALL = 'no_to_all'#
YES_TO_ALL = 'yes_to_all'#
truth_value(default_option: AskResult) bool#

Returns the boolean representation of the flag based on the default_option.

Parameters:

default_option (AskResult) – The default option.

Returns:

True if YES_TO_ALL, False if NO_TO_ALL, default_option if DEFAULT_TO_ALL.

Return type:

bool

ask_lib.ask_result module#

class ask_lib.ask_result.AskResult(value)#

Bases: Enum

Each member represent the choice of the user, either yes or no.

NO = 'no'#
YES = 'yes'#
classmethod from_bool(b: bool) AskResult#

Converts a boolean into an AskResult member.

Parameters:

b (bool) – The boolean that needs to be converted.

Returns:

AskResult.YES if True, else AskResult.NO.

Return type:

AskResult

Module contents#

Utility function to ask for user’s confirmation in a CLI