Skip to content

Date

sereto.cli.date

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(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