From 80b7ffeb895dd39e0f9332f478701f6b09086eec Mon Sep 17 00:00:00 2001 From: Snigdhajyoti Ghosh Date: Thu, 9 Nov 2023 01:55:02 +0530 Subject: [PATCH] fix version information and the argument controlling it --- aws_console/__init__.py | 10 ---------- aws_console/input_output/cli.py | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/aws_console/__init__.py b/aws_console/__init__.py index cbdf5a7..ddcff3b 100644 --- a/aws_console/__init__.py +++ b/aws_console/__init__.py @@ -11,8 +11,6 @@ def aws_console(): args = open_console_arguments() - __show_version_if_requested(args) - creds, region_name = aws_session_credential(args.profile, args.region) url = signin_url(creds, region_name) @@ -22,15 +20,7 @@ def aws_console(): def aws_system_credential(): args = credential_process_arguments() - __show_version_if_requested(args) - if args.command == 'store': store_aws_credential(args.account_id, args.username, args.access_key, args.secret_key) elif args.command == 'get': get_aws_credential(args.account_id, args.username, args.access_key, args.credential_process) - - -def __show_version_if_requested(args): - if args.version: - print(__version__) - exit(0) diff --git a/aws_console/input_output/cli.py b/aws_console/input_output/cli.py index aafca20..ff3f6f2 100644 --- a/aws_console/input_output/cli.py +++ b/aws_console/input_output/cli.py @@ -1,13 +1,15 @@ import os import argparse +from importlib.metadata import version + def open_console_arguments(): parser = argparse.ArgumentParser( description="Open the AWS console in your web browser, using your AWS CLI credentials") - parser.add_argument('-v', '--version', action='store_true', - help="Display the version of this tool") + parser.add_argument( + '-v', '--version', action='version', help="Display the version of this tool", version=version("aws-console")) parser.add_argument('-P', '--profile', default=os.getenv("AWS_PROFILE"), help="The AWS profile to create the pre-signed URL with") parser.add_argument('-R', '--region', default=os.getenv("AWS_REGION", os.getenv("AWS_DEFAULT_REGION")), @@ -23,17 +25,18 @@ def open_console_arguments(): def credential_process_arguments(): + global_parser = argparse.ArgumentParser(add_help=False) + global_parser.add_argument( + '-v', '--version', action='version', help="Display the version of this tool", version=version("aws-console")) + parser = argparse.ArgumentParser( - description='AWS Credential Management CLI') - - parser.add_argument('-v', '--version', action='store_true', - help="Display the version of this tool") + description='AWS Credential Management CLI', parents=[global_parser]) subparsers = parser.add_subparsers( - dest='command', help='Available commands') + dest='command', required=True, help='Available commands') # Subparser for 'store' command - store_parser = subparsers.add_parser('store', help='Store AWS credentials') + store_parser = subparsers.add_parser('store', help='Store AWS credentials', parents=[global_parser]) store_parser.add_argument( '--account-id', required=False, help='AWS Account ID for the name') store_parser.add_argument( @@ -44,7 +47,7 @@ def credential_process_arguments(): '--secret-key', required=True, help='AWS secret key') # Subparser for 'get' command - get_parser = subparsers.add_parser('get', help='Get AWS credentials') + get_parser = subparsers.add_parser('get', help='Get AWS credentials', parents=[global_parser]) get_parser.add_argument( '--account-id', required=False, help='AWS Account ID for the name') get_parser.add_argument(