Skip to content

Commit a11d2c9

Browse files
feat: add standard automation flags (--yes, --force) (#1)
Co-authored-by: Aurelian Shuttleworth <aurelian@shuttleworth.tech>
1 parent 493d013 commit a11d2c9

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

docs/automation.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Automation Guide
2+
3+
`gcalcli` provides features specifically designed for automation scripts and agents.
4+
5+
## Non-Interactive Mode support (CI/CD)
6+
7+
Use automation flags to prevent the tool from pausing for user input:
8+
9+
- `--yes`, `--force`, `--no-prompt`: Automatically answer "yes" to confirmation prompts (like delete).
10+
- `--noprompt` (specific to `add`): Skips prompting for missing fields during event creation.

gcalcli/argparsers.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,30 @@ def get_search_parser():
354354
return search_parser
355355

356356

357+
def get_automation_parser():
358+
# Flags for skipping confirmation prompts
359+
automation_parser = argparse.ArgumentParser(add_help=False)
360+
automation_parser.add_argument(
361+
'--yes', '-y',
362+
action='store_true',
363+
dest='noconfirm',
364+
help='Answer "yes" to all prompts (e.g. delete confirmation)'
365+
)
366+
automation_parser.add_argument(
367+
'--force', '-f',
368+
action='store_true',
369+
dest='noconfirm',
370+
help='Alias for --yes'
371+
)
372+
automation_parser.add_argument(
373+
'--no-prompt',
374+
action='store_true',
375+
dest='noconfirm',
376+
help='Alias for --yes'
377+
)
378+
return automation_parser
379+
380+
357381
def handle_unparsed(unparsed, namespace):
358382
# Attempt a reparse against the program options.
359383
# Provides some robustness for misplaced global options
@@ -450,6 +474,9 @@ def get_argument_parser():
450474

451475
# tacks on search text
452476
search_parser = get_search_parser()
477+
478+
# automation flags
479+
automation_parser = get_automation_parser()
453480

454481
sub = parser.add_subparsers(
455482
help='Invoking a subcommand with --help prints subcommand usage.',
@@ -496,13 +523,13 @@ def get_argument_parser():
496523

497524
delete = sub.add_parser(
498525
'delete',
499-
parents=[calendars_parser, output_parser, search_parser],
526+
parents=[calendars_parser, output_parser, search_parser, automation_parser],
500527
help='delete events from the calendar',
501528
description='Case insensitive search for items to delete '
502529
'interactively.',
503530
)
504531
delete.add_argument(
505-
'--iamaexpert', action='store_true', help='Probably not'
532+
'--iamaexpert', action='store_true', help='Legacy alias for --yes'
506533
)
507534

508535
sub.add_parser(
@@ -656,7 +683,7 @@ def get_argument_parser():
656683

657684
_import = sub.add_parser(
658685
'import',
659-
parents=[calendar_parser, remind_parser],
686+
parents=[calendar_parser, remind_parser, automation_parser],
660687
help='import an ics/vcal file to a calendar',
661688
description='Import from an ics/vcal file; a single --calendar '
662689
'must be specified. Reads from stdin when no file argument is '

gcalcli/gcal.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,18 @@ def _delete_event(self, event):
997997
cal_id = event['gcalcli_cal']['id']
998998
event_id = event['id']
999999

1000-
if self.expert:
1000+
if self.expert or self.options.get('noconfirm'):
10011001
self.delete(cal_id, event_id)
10021002
self.printer.msg('Deleted!\n', 'red')
10031003
return
10041004

1005+
# Print "Safe Prompt" summary
1006+
time_str = event['s'].strftime('%Y-%m-%d %H:%M')
1007+
if is_all_day(event):
1008+
time_str = event['s'].strftime('%Y-%m-%d')
1009+
1010+
self.printer.msg(f'> Found Event: "{_valid_title(event).strip()}" ({time_str})\n', 'yellow')
1011+
10051012
self.printer.msg('Delete? [N]o [y]es [q]uit: ', 'magenta')
10061013
val = input()
10071014

@@ -1643,7 +1650,7 @@ def ImportICS(self, verbose=False, dump=False, reminders=None,
16431650

16441651
self._add_reminders(event.body, reminders)
16451652

1646-
if not verbose:
1653+
if not verbose or self.options.get('noconfirm'):
16471654
# Don't prompt, just assume user wants to import.
16481655
pass
16491656
else:

0 commit comments

Comments
 (0)