GNU grep has an argument to print a certain number of extra lines around a matching line. From the man page:
-C NUM, -NUM, --context=NUM Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
So I can do something like
grep -5 pattern file.txt
would be the equivalent of
grep -C 5 pattern file.txt
How can I emulate the -NUMBER behavior using argparse to where a user can pass in -NUM and I can get that number easily from argparse.
A silly way of doing this is to do something like this:
parser = argparse.ArgumentParser()
for i in range(1, 10000):
parser.add_argument(f'-{i}', action='store_true', dest=f'border_lines_{i}', help=argparse.SUPPRESS)
args = parser.parse_args()
border_line_count = 0
for i in range(1, 10000):
if getattr(args, f'border_lines_{i}'):
border_line_count = i
break
Basically, I add a bunch of hidden arguments for different -NUMs. Then find the one that is set. Is there a better way?