diff options
author | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-05-07 11:52:20 -0400 |
---|---|---|
committer | Konstantin Ryabitsev <konstantin@linuxfoundation.org> | 2020-05-07 16:04:18 -0400 |
commit | b869fbd548e54f4ec308960ee96cc3d5165c6b69 (patch) | |
tree | f639c5d1738e0ede10b3d47c02aebbbb8a39335e | |
parent | f903c40d31f21bb1018a16751bb50f2364a7e3c6 (diff) | |
download | b4-b869fbd548e54f4ec308960ee96cc3d5165c6b69.tar.gz |
Switch ty -s and -d to allow friendly ranges
Instead of insisting that people put in specific numbers, allow them to
specify ranges, such as:
b4 ty -s 1-3,5,7-
Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rw-r--r-- | b4/__init__.py | 18 | ||||
-rw-r--r-- | b4/command.py | 8 | ||||
-rw-r--r-- | b4/ty.py | 12 |
3 files changed, 28 insertions, 10 deletions
diff --git a/b4/__init__.py b/b4/__init__.py index 6ae284b..c9011ac 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -1861,3 +1861,21 @@ def make_quote(body, maxlines=5): quotelines.append('> %s' % line.rstrip()) qcount += 1 return '\n'.join(quotelines) + + +def parse_int_range(intrange, upper=None): + # Remove all whitespace + intrange = re.sub(r'\s', '', intrange) + for n in intrange.split(','): + if n.isdigit(): + yield int(n) + elif n.find('<') == 0 and len(n) > 1 and n[1:].isdigit(): + yield from range(0, int(n[1:])) + elif n.find('-') > 0: + nr = n.split('-') + if nr[0].isdigit() and nr[1].isdigit(): + yield from range(int(nr[0]), int(nr[1])+1) + elif not len(nr[1]) and nr[0].isdigit() and upper: + yield from range(int(nr[0]), upper+1) + else: + logger.critical('Unknown range value specified: %s', n) diff --git a/b4/command.py b/b4/command.py index 39ad3e5..f83c935 100644 --- a/b4/command.py +++ b/b4/command.py @@ -146,10 +146,10 @@ def cmd(): help='Write thanks files into this dir (default=.)') sp_ty.add_argument('-l', '--list', action='store_true', default=False, help='List pull requests and patch series you have retrieved') - sp_ty.add_argument('-s', '--send', nargs='+', - help='Generate thankyous for specified messages (use -l to get the list or "all")') - sp_ty.add_argument('-d', '--discard', nargs='+', - help='Discard specified messages (use -l to get the list, or use "all")') + sp_ty.add_argument('-s', '--send', default=None, + help='Generate thankyous for specific entries from -l (e.g.: 1,3-5,7-; or "all")') + sp_ty.add_argument('-d', '--discard', default=None, + help='Discard specific messages from -l (e.g.: 1,3-5,7-; or "all")') sp_ty.add_argument('-a', '--auto', action='store_true', default=False, help='Use the Auto-Thankanator to figure out what got applied/merged') sp_ty.add_argument('-b', '--branch', default=None, @@ -471,11 +471,11 @@ def send_selected(cmdargs): logger.info('Nothing to do') sys.exit(0) - if 'all' in cmdargs.send: + if cmdargs.send == 'all': listing = tracked else: listing = list() - for num in cmdargs.send: + for num in b4.parse_int_range(cmdargs.send, upper=len(tracked)): try: index = int(num) - 1 listing.append(tracked[index]) @@ -504,11 +504,11 @@ def discard_selected(cmdargs): logger.info('Nothing to do') sys.exit(0) - if 'all' in cmdargs.discard: + if cmdargs.discard == 'all': listing = tracked else: listing = list() - for num in cmdargs.discard: + for num in b4.parse_int_range(cmdargs.discard, upper=len(tracked)): try: index = int(num) - 1 listing.append(tracked[index]) @@ -618,5 +618,5 @@ def main(cmdargs): sys.exit(0) write_tracked(tracked) logger.info('---') - logger.info('You can send them using:') - logger.info(' b4 ty -s 1 [2 3 ...]') + logger.info('You can send them using number ranges, e.g:') + logger.info(' b4 ty -s 1-3,5,7-') |