From 711f5fa6cb9c0d7b8e5626632b2d9d279ab3aa66 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 17 Mar 2025 22:58:25 -0700 Subject: [PATCH] UX nicety to make default-false boolean options into valueless CLI flags (#303). --- borgmatic/commands/arguments.py | 38 +++++++++++++++++++++++++++------ borgmatic/config/schema.yaml | 12 +++++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 3f2da08b..72980327 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -288,6 +288,24 @@ def parse_arguments_for_actions(unparsed_arguments, action_parsers, global_parse ) +# As a UX nicety, allow boolean options that have a default of false to have command-line flags +# without values. +DEFAULT_FALSE_FLAG_NAMES = { + 'one_file_system', + 'numeric_ids', + 'read_special', + 'exclude_caches', + 'keep_exclude_tags', + 'exclude_nodump', + 'source_directories_must_exist', + 'relocated_repo_access_is_ok', + 'unknown_unencrypted_repo_access_is_ok', + 'check_i_know_what_i_am_doing', + 'postgresql_databases[0].no_owner', + 'healthchecks.create_slug', +} + + def add_arguments_from_schema(arguments_group, schema, unparsed_arguments, names=None): ''' Given an argparse._ArgumentGroup instance, a configuration schema dict, and a sequence of @@ -377,13 +395,21 @@ def add_arguments_from_schema(arguments_group, schema, unparsed_arguments, names description = description.replace('%', '%%') argument_type = borgmatic.config.schema.parse_type(schema_type) + full_flag_name = f"--{flag_name.replace('_', '-')}" - arguments_group.add_argument( - f"--{flag_name.replace('_', '-')}", - type=argument_type, - metavar=metavar, - help=description, - ) + if flag_name in DEFAULT_FALSE_FLAG_NAMES: + arguments_group.add_argument( + full_flag_name, + action='store_true', + help=description, + ) + else: + arguments_group.add_argument( + full_flag_name, + type=argument_type, + metavar=metavar, + help=description, + ) # We want to support flags that can have arbitrary indices like: # diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index e4901dc7..909bc511 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1983,8 +1983,8 @@ properties: type: boolean description: | Set to True to enable HTML parsing of the message. - Set to False for plain text. - example: True + Set to false for plain text. + example: true sound: type: string description: | @@ -2058,8 +2058,8 @@ properties: type: boolean description: | Set to True to enable HTML parsing of the message. - Set to False for plain text. - example: True + Set to false for plain text. + example: true sound: type: string description: | @@ -2133,8 +2133,8 @@ properties: type: boolean description: | Set to True to enable HTML parsing of the message. - Set to False for plain text. - example: True + Set to false for plain text. + example: true sound: type: string description: |