From 6d2c85dafce2ee4f7f3c07d7ba9f52cf86e95b4b Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Fri, 13 Dec 2024 00:21:04 +0700 Subject: [PATCH] Update pki.server.cli.password to use argparse --- base/server/python/pki/server/cli/password.py | 179 +++++++++--------- 1 file changed, 85 insertions(+), 94 deletions(-) diff --git a/base/server/python/pki/server/cli/password.py b/base/server/python/pki/server/cli/password.py index d3ee502e8d2..ce0f3a56b53 100644 --- a/base/server/python/pki/server/cli/password.py +++ b/base/server/python/pki/server/cli/password.py @@ -18,11 +18,8 @@ # All rights reserved. # -from __future__ import absolute_import -from __future__ import print_function -import getopt +import argparse import logging -import sys import pki.cli @@ -46,6 +43,24 @@ class PasswordFindCLI(pki.cli.CLI): def __init__(self): super().__init__('find', 'Find passwords') + self.parser = argparse.ArgumentParser( + prog=self.name, + add_help=False) + self.parser.add_argument( + '-i', + '--instance', + default='pki-tomcat') + self.parser.add_argument( + '-v', + '--verbose', + action='store_true') + self.parser.add_argument( + '--debug', + action='store_true') + self.parser.add_argument( + '--help', + action='store_true') + def print_help(self): print('Usage: pki-server password-find [OPTIONS]') print() @@ -57,36 +72,19 @@ def print_help(self): def execute(self, argv): - try: - opts, _ = getopt.gnu_getopt(argv, 'i:v', [ - 'instance=', 'force', - 'verbose', 'debug', 'help']) + args = self.parser.parse_args(args=argv) - except getopt.GetoptError as e: - print('ERROR: %s' % e) + if args.help: self.print_help() - sys.exit(1) - - instance_name = 'pki-tomcat' - - for o, a in opts: - if o in ('-i', '--instance'): - instance_name = a + return - elif o in ('-v', '--verbose'): - logging.getLogger().setLevel(logging.INFO) + if args.debug: + logging.getLogger().setLevel(logging.DEBUG) - elif o == '--debug': - logging.getLogger().setLevel(logging.DEBUG) + elif args.verbose: + logging.getLogger().setLevel(logging.INFO) - elif o == '--help': - self.print_help() - sys.exit() - - else: - print('ERROR: unknown option: %s' % o) - self.print_help() - sys.exit(1) + instance_name = args.instance instance = pki.server.PKIServerFactory.create(instance_name) @@ -112,6 +110,26 @@ class PasswordAddCLI(pki.cli.CLI): def __init__(self): super().__init__('add', 'Add password') + self.parser = argparse.ArgumentParser( + prog=self.name, + add_help=False) + self.parser.add_argument( + '-i', + '--instance', + default='pki-tomcat') + self.parser.add_argument('--password') + self.parser.add_argument( + '-v', + '--verbose', + action='store_true') + self.parser.add_argument( + '--debug', + action='store_true') + self.parser.add_argument( + '--help', + action='store_true') + self.parser.add_argument('name') + def print_help(self): print('Usage: pki-server password-add [OPTIONS] ') print() @@ -124,46 +142,21 @@ def print_help(self): def execute(self, argv): - try: - opts, args = getopt.gnu_getopt(argv, 'i:v', [ - 'instance=', - 'password=', - 'verbose', 'debug', 'help']) + args = self.parser.parse_args(args=argv) - except getopt.GetoptError as e: - print('ERROR: %s' % e) + if args.help: self.print_help() - sys.exit(1) - - instance_name = 'pki-tomcat' - password = None - - for o, a in opts: - if o in ('-i', '--instance'): - instance_name = a - - elif o == '--password': - password = a - - elif o in ('-v', '--verbose'): - logging.getLogger().setLevel(logging.INFO) + return - elif o == '--debug': - logging.getLogger().setLevel(logging.DEBUG) + if args.debug: + logging.getLogger().setLevel(logging.DEBUG) - elif o == '--help': - self.print_help() - sys.exit() + elif args.verbose: + logging.getLogger().setLevel(logging.INFO) - else: - print('ERROR: Unknown option: %s' % o) - self.print_help() - sys.exit(1) - - if len(args) < 1: - raise Exception('Missing password ID') - - name = args[0] + instance_name = args.instance + password = args.password + name = args.name instance = pki.server.PKIServerFactory.create(instance_name) @@ -184,6 +177,25 @@ class PasswordRemoveCLI(pki.cli.CLI): def __init__(self): super().__init__('del', 'Remove password') + self.parser = argparse.ArgumentParser( + prog=self.name, + add_help=False) + self.parser.add_argument( + '-i', + '--instance', + default='pki-tomcat') + self.parser.add_argument( + '-v', + '--verbose', + action='store_true') + self.parser.add_argument( + '--debug', + action='store_true') + self.parser.add_argument( + '--help', + action='store_true') + self.parser.add_argument('name') + def print_help(self): print('Usage: pki-server password-del [OPTIONS] ') print() @@ -195,41 +207,20 @@ def print_help(self): def execute(self, argv): - try: - opts, args = getopt.gnu_getopt(argv, 'i:v', [ - 'instance=', - 'verbose', 'debug', 'help']) + args = self.parser.parse_args(args=argv) - except getopt.GetoptError as e: - print('ERROR: %s' % e) + if args.help: self.print_help() - sys.exit(1) - - instance_name = 'pki-tomcat' + return - for o, a in opts: - if o in ('-i', '--instance'): - instance_name = a - - elif o in ('-v', '--verbose'): - logging.getLogger().setLevel(logging.INFO) - - elif o == '--debug': - logging.getLogger().setLevel(logging.DEBUG) - - elif o == '--help': - self.print_help() - sys.exit() - - else: - print('ERROR: Unknown option: %s' % o) - self.print_help() - sys.exit(1) + if args.debug: + logging.getLogger().setLevel(logging.DEBUG) - if len(args) < 1: - raise Exception('Missing password ID') + elif args.verbose: + logging.getLogger().setLevel(logging.INFO) - name = args[0] + instance_name = args.instance + name = args.name instance = pki.server.PKIServerFactory.create(instance_name)