fix --last, --first accepting negative values

also don't accept zero, because every use of these doesn't cover that case,
and it arguably doesn't make a lot of sense.
This commit is contained in:
Marian Beermann 2017-07-02 23:08:15 +02:00
parent 51c1c22c1f
commit 2db377d6fb
2 changed files with 12 additions and 4 deletions

View File

@ -44,7 +44,7 @@ from .crypto.key import key_creator, tam_required_file, tam_required, RepoKey, P
from .crypto.keymanager import KeyManager
from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR
from .helpers import Error, NoManifestError, set_ec
from .helpers import location_validator, archivename_validator, ChunkerParams
from .helpers import positive_int_validator, location_validator, archivename_validator, ChunkerParams
from .helpers import PrefixSpec, SortBySpec, HUMAN_SORT_KEYS
from .helpers import BaseFormatter, ItemFormatter, ArchiveFormatter
from .helpers import format_timedelta, format_file_size, parse_file_size, format_archive
@ -2394,9 +2394,9 @@ class Archiver:
if first_last:
group = filters_group.add_mutually_exclusive_group()
group.add_argument('--first', metavar='N', dest='first', default=0, type=int,
group.add_argument('--first', metavar='N', dest='first', default=0, type=positive_int_validator,
help='consider first N archives after other filters were applied')
group.add_argument('--last', metavar='N', dest='last', default=0, type=int,
group.add_argument('--last', metavar='N', dest='last', default=0, type=positive_int_validator,
help='consider last N archives after other filters were applied')
parser = argparse.ArgumentParser(prog=self.prog, description='Borg - Deduplicated Backups',

View File

@ -236,7 +236,7 @@ class Archives(abc.MutableMapping):
if first:
archives = archives[:first]
elif last:
archives = archives[len(archives) - last:]
archives = archives[max(len(archives) - last, 0):]
return archives
def list_considering(self, args):
@ -400,6 +400,14 @@ class Manifest:
self.repository.put(self.MANIFEST_ID, self.key.encrypt(data))
def positive_int_validator(value):
"""argparse type for positive integers"""
int_value = int(value)
if int_value <= 0:
raise argparse.ArgumentTypeError('A positive integer is required: %s' % value)
return int_value
def interval(s):
"""Convert a string representing a valid interval to a number of hours."""
multiplier = {'H': 1, 'd': 24, 'w': 24 * 7, 'm': 24 * 31, 'y': 24 * 365}