From ca4a0bc7d264eb6aee17b0bf30a0b9456c0ac924 Mon Sep 17 00:00:00 2001 From: scito Date: Sun, 18 Dec 2022 21:34:24 +0100 Subject: [PATCH] improvements for - (stdout) - - implies -q - use add_mutually_exclusive_group for -v and -q - adapt and improve tests --- README.md | 6 +++--- extract_otp_secret_keys.py | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2945c72..2df19de 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,10 @@ cd extract_otp_secret_keys ## Program help: arguments and options -
usage: extract_otp_secret_keys.py [-h] [--json FILE] [--csv FILE] [--keepass FILE] [--printqr] [--saveqr DIR] [--verbose] [--quiet] infile
+
usage: extract_otp_secret_keys.py [-h] [--json FILE] [--csv FILE] [--keepass FILE] [--printqr] [--saveqr DIR] [--verbose | --quiet] infile
 
 positional arguments:
-  infile                   file or - for stdin (default: -) with "otpauth-migration://..." URLs separated by newlines, lines starting with # are ignored
+  infile                   file or - for stdin with "otpauth-migration://..." URLs separated by newlines, lines starting with # are ignored
 
 options:
   -h, --help               show this help message and exit
@@ -44,7 +44,7 @@ options:
   --printqr, -p            print QR code(s) as text to the terminal (requires qrcode module)
   --saveqr DIR, -s DIR     save QR code(s) as images to the given folder (requires qrcode module)
   --verbose, -v            verbose output
-  --quiet, -q              no stdout output
+ --quiet, -q no stdout output, except output set by -
## Dependencies diff --git a/extract_otp_secret_keys.py b/extract_otp_secret_keys.py index ba6870f..3f448ca 100644 --- a/extract_otp_secret_keys.py +++ b/extract_otp_secret_keys.py @@ -74,20 +74,20 @@ def main(sys_args): def parse_args(sys_args): - formatter = lambda prog: argparse.HelpFormatter(prog,max_help_position=52) + formatter = lambda prog: argparse.HelpFormatter(prog, max_help_position=52) arg_parser = argparse.ArgumentParser(formatter_class=formatter) - arg_parser.add_argument('infile', help='file or - for stdin (default: -) with "otpauth-migration://..." URLs separated by newlines, lines starting with # are ignored') + arg_parser.add_argument('infile', help='file or - for stdin with "otpauth-migration://..." URLs separated by newlines, lines starting with # are ignored') arg_parser.add_argument('--json', '-j', help='export json file or - for stdout', metavar=('FILE')) arg_parser.add_argument('--csv', '-c', help='export csv file or - for stdout', metavar=('FILE')) arg_parser.add_argument('--keepass', '-k', help='export totp/hotp csv file(s) for KeePass, - for stdout', metavar=('FILE')) arg_parser.add_argument('--printqr', '-p', help='print QR code(s) as text to the terminal (requires qrcode module)', action='store_true') arg_parser.add_argument('--saveqr', '-s', help='save QR code(s) as images to the given folder (requires qrcode module)', metavar=('DIR')) - arg_parser.add_argument('--verbose', '-v', help='verbose output', action='count') - arg_parser.add_argument('--quiet', '-q', help='no stdout output', action='store_true') + output_group = arg_parser.add_mutually_exclusive_group() + output_group.add_argument('--verbose', '-v', help='verbose output', action='count') + output_group.add_argument('--quiet', '-q', help='no stdout output, except output set by -', action='store_true') args = arg_parser.parse_args(sys_args) - if args.verbose and args.quiet: - eprint("The arguments --verbose and --quiet are mutually exclusive.") - sys.exit(1) + if args.csv == '-' or args.json == '-' or args.keepass == '-': + args.quiet = args.q = True return args