Borgmatic without arguments/parameters should show usage help instead of starting a backup #262

Closed
opened 2019-11-28 18:14:37 +00:00 by varac · 10 comments

I often type borgmatic and hit enter in order to get help on it's subcommands and it always confuses me that it starts a backup instead.
Most applications show their usage help without doing anything when called without arguments/parameters so I propose borgmatic to do the same.

I often type `borgmatic` and hit enter in order to get help on it's subcommands and it always confuses me that it starts a backup instead. Most applications show their usage help without doing anything when called without arguments/parameters so I propose borgmatic to do the same.
Owner

I agree that borgmatic is a little different than most command-line applications in this respect, but I don't think we could make a change like this without breaking a number of existing borgmatic uses (whether manual or automated) that rely on just running borgmatic directly without any arguments. And I think that's kind of the promise of borgmatic in some ways: Once you get your configuration file set up correctly, you don't have to pass any (or very many) arguments to borgmatic on the command-line.

I agree that borgmatic is a little different than most command-line applications in this respect, but I don't think we could make a change like this without breaking a number of existing borgmatic uses (whether manual or automated) that rely on just running `borgmatic` directly without any arguments. And I think that's kind of the promise of borgmatic in some ways: Once you get your configuration file set up correctly, you don't have to pass any (or very many) arguments to borgmatic on the command-line.
witten added the waiting for response label 2019-12-12 18:49:36 +00:00

I guess a config option disabled by default would be good for that.

I guess a config option disabled by default would be good for that.
Owner

Yup, that could certainly work!

Yup, that could certainly work!
witten removed the waiting for response label 2020-05-06 17:05:49 +00:00
witten added the good first issue label 2023-03-29 18:11:21 +00:00
Contributor

how should I proceed with this issue here and what needs to be implemented??

how should I proceed with this issue here and what needs to be implemented??
Owner

The idea is that there could be a new boolean configuration option that, when enabled, means that running borgmatic without any actions displays borgmatic help instead of running the default actions. But by default (without that configuration option present or with it set to false), borgmatic would continue to run default actions when none are specified on the command-line.

So you could proceed directly to a PR given that this is hopefully not too large. Or if you prefer you can write up your implementation plan for review. Thanks!

The idea is that there could be a new boolean configuration option that, when enabled, means that running `borgmatic` without any actions displays borgmatic help instead of running the default actions. But by default (without that configuration option present or with it set to false), borgmatic would continue to run default actions when none are specified on the command-line. So you could proceed directly to a PR given that this is hopefully not too large. Or if you prefer you can write up your implementation plan for review. Thanks!
Contributor

Here is my implementation plan for this issue-

def check_and_show_help_on_no_args():

"""
Check if the 'borgmatic' command is run without any arguments.
If the configuration option 'show_help_on_no_args' is set to True,
show the help message and exit. Otherwise, trigger the default backup behavior.
"""

# Check if the script was invoked using the 'borgmatic' command
if 'borgmatic' in sys.argv[0]:
# Check if no additional arguments were provided 
    if len(sys.argv) == 1:
        # Some code here
        
        # Check if  'show_help_on_no_args' to True
    
        if show_help_on_no_args:
            # Display the help message and exit
            print(parse_arguments(['--help']))
            sys.exit(0)

# Otherwise, trigger the default backup process
run_backup()
Here is my implementation plan for this issue- def check_and_show_help_on_no_args(): """ Check if the 'borgmatic' command is run without any arguments. If the configuration option 'show_help_on_no_args' is set to True, show the help message and exit. Otherwise, trigger the default backup behavior. """ # Check if the script was invoked using the 'borgmatic' command if 'borgmatic' in sys.argv[0]: # Check if no additional arguments were provided if len(sys.argv) == 1: # Some code here # Check if 'show_help_on_no_args' to True if show_help_on_no_args: # Display the help message and exit print(parse_arguments(['--help'])) sys.exit(0) # Otherwise, trigger the default backup process run_backup()
Contributor

i cant figure out how should we pass the value of show_help_on_no_args.Should we have a separate config file or it needs to be in some file in existing file structure?Also pls guide if I am moving in correct direction from above code

i cant figure out how should we pass the value of show_help_on_no_args.Should we have a separate config file or it needs to be in some file in existing file structure?Also pls guide if I am moving in correct direction from above code
Owner

Here are my thoughts:

The "ideal" place to do the kind of logic you're proposing would be in borgmatic/commands/arguments.py:parse_argument_for_actions() and specifically somewhere around this code:

    if not arguments and not help_requested:
        for default_action_name in ('create', 'prune', 'compact', 'check'):

... because that's where the decision to run the default actions is made, and so you could in theory make a different decision if your proposed configuration option is set.

The problem: This code runs way before any configuration files are actually loaded, because we're not even done with parsing command-line flags at this point. And those flags can (among other things) specify which configuration files to load! (I don't think it makes sense to have a separate purpose-specific configuration file for this feature.)

So that leaves a less-than-ideal location to call your code: After configuration files have already been loaded and parsed. For instance, somewhere in borgmatic/commands/borgmatic.py:main() after load_configurations(). There, I think an approach like what you outlined could work, but it'd probably have to iterate the configs to see if any have the proposed new option set. If so, your logic could trigger. It might be enough though to check sys.argv length there and not do the == 'borgmatic' check...?

Here are my thoughts: The "ideal" place to do the kind of logic you're proposing would be in `borgmatic/commands/arguments.py:parse_argument_for_actions()` and specifically somewhere around this code: ```python if not arguments and not help_requested: for default_action_name in ('create', 'prune', 'compact', 'check'): ``` ... because that's where the decision to run the default actions is made, and so you could in theory make a different decision if your proposed configuration option is set. The problem: This code runs *way* before any configuration files are actually loaded, because we're not even done with parsing command-line flags at this point. And those flags can (among other things) specify which configuration files to load! (I don't think it makes sense to have a separate purpose-specific configuration file for this feature.) So that leaves a less-than-ideal location to call your code: After configuration files have already been loaded and parsed. For instance, somewhere in `borgmatic/commands/borgmatic.py:main()` after `load_configurations()`. There, I think an approach like what you outlined could work, but it'd probably have to iterate the configs to see if any have the proposed new option set. If so, your logic could trigger. It might be enough though to check `sys.argv` length there and not do the `== 'borgmatic'` check...?
Owner

This is implemented in main by @gautamaggarwal2810 and will be part of the next release!

This is implemented in main by @gautamaggarwal2810 and will be part of the next release!
witten referenced this issue from a commit 2025-03-30 02:54:50 +00:00
witten referenced this issue from a commit 2025-03-30 05:52:29 +00:00
Owner
Released in borgmatic 2.0.0! Docs here: https://torsion.org/borgmatic/docs/how-to/set-up-backups/#disabling-default-actions
Sign in to join this conversation.
4 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: borgmatic-collective/borgmatic#262