Exemple #1
0
def read_reminder_params() -> Optional[Reminder]:
    """
    read parameters from the user and build a Reminder object.
    return a Reminder object, or None, meaning no action required (e.g. wrong
    user parameters, or user aborted the action)
    """
    title = input('What\'s the reminder: ')
    dt = parse_time_str(input('When do you want to be reminded: '))
    if dt is not None:
        print(f'\n"{title}" on {dt.strftime(DATE_FORMAT)}\n')
        save = read_yes_no('Do you want to save this?')
        if save:
            return Reminder(id=gen_id(), title=title, dt=dt)
def invoke_operation(args):
    """
    inspect the program arguments and invoke the appropriate operation
    """
    client = RemindersClient()

    if args.interactive or args.create:
        # prepare the reminder to create
        if args.interactive:
            reminder = read_reminder_params()
        else:
            title, time_str = args.create
            dt = parse_time_str(time_str)
            if dt is None:
                return
            reminder = Reminder(id=gen_id(),
                                title=title,
                                dt=dt,
                                all_day=args.all_day)

        # execute creation if applicable
        if reminder is not None:
            if client.create_reminder(reminder):
                print('Reminder set successfully:')
                print(reminder)

    elif args.get:
        id = args.get
        reminder = client.get_reminder(reminder_id=id)
        if reminder is not None:
            print(reminder)

    elif args.delete:
        id = args.delete
        if client.delete_reminder(reminder_id=id):
            print('Reminder deleted successfully')

    elif args.list:
        num_reminders = args.list
        if num_reminders < 0:
            print('argument to list command must be positive')
            return
        reminders = client.list_reminders(num_reminders=num_reminders)
        if reminders is not None:
            for r in sorted(reminders):
                print(r)

    else:
        print('Wrong usage: no valid action was specified\n'
              'please read help menu (-h) to see correct usage')