Skip to content

Date

sereto.cli.date

_prompt_date(message, default=None)

Interactively prompt the user for a date in the format DD-Mmm-YYYY.

Parameters:

Name Type Description Default
message str

The message to display to the user.

required
default SeretoDate | None

The default date, which the user can easily accept. Defaults to None.

None

Returns:

Type Description
SeretoDate | None

SeretoDate if correct input was provided, None otherwise

Source code in sereto/cli/date.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@validate_call
def _prompt_date(message: str, default: SeretoDate | None = None) -> SeretoDate | None:
    """Interactively prompt the user for a date in the format DD-Mmm-YYYY.

    Args:
        message: The message to display to the user.
        default: The default date, which the user can easily accept. Defaults to None.

    Returns:
        SeretoDate if correct input was provided, None otherwise
    """
    user_input = prompt(message) if default is None else prompt(message, default=str(default))

    try:
        return SeretoDate.from_str(user_input)
    except (ValueError, TypeError):
        return None

prompt_user_for_date(date_type)

Prompt user for a date or date range, depending on the provided date type.

Date format is DD-Mmm-YYYY.

Parameters:

Name Type Description Default
date_type DateType

The type of date to prompt for.

required

Returns:

Type Description
SeretoDate | DateRange

The date as provided by the user.

Source code in sereto/cli/date.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@validate_call
def prompt_user_for_date(date_type: DateType) -> SeretoDate | DateRange:
    """Prompt user for a date or date range, depending on the provided date type.

    Date format is DD-Mmm-YYYY.

    Args:
        date_type: The type of date to prompt for.

    Returns:
        The date as provided by the user.
    """
    # Check if the date type allows for a range
    allow_range = date_type in TYPES_WITH_ALLOWED_RANGE

    while True:
        # Prompt user for the start date
        prompt: str = f"Date{' start' if allow_range else ''} (DD-Mmm-YYYY)"
        if (start_date := _prompt_date(f"{prompt}: ")) is None:
            Console().print("[red]Invalid input, try again\n")
            continue

        # Prompt user for the end date, if the date type allows it
        if allow_range:
            if (end_date := _prompt_date("Date end (DD-Mmm-YYYY): ", default=start_date)) is None:
                Console().print("[red]Invalid input, try again\n")
                continue
        else:
            end_date = start_date

        return DateRange(start=start_date, end=end_date) if start_date != end_date else start_date