Skip to content

Commit 6d2c85d

Browse files
committed
Update pki.server.cli.password to use argparse
1 parent 1ab2c49 commit 6d2c85d

File tree

1 file changed

+85
-94
lines changed

1 file changed

+85
-94
lines changed

base/server/python/pki/server/cli/password.py

Lines changed: 85 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
# All rights reserved.
1919
#
2020

21-
from __future__ import absolute_import
22-
from __future__ import print_function
23-
import getopt
21+
import argparse
2422
import logging
25-
import sys
2623

2724
import pki.cli
2825

@@ -46,6 +43,24 @@ class PasswordFindCLI(pki.cli.CLI):
4643
def __init__(self):
4744
super().__init__('find', 'Find passwords')
4845

46+
self.parser = argparse.ArgumentParser(
47+
prog=self.name,
48+
add_help=False)
49+
self.parser.add_argument(
50+
'-i',
51+
'--instance',
52+
default='pki-tomcat')
53+
self.parser.add_argument(
54+
'-v',
55+
'--verbose',
56+
action='store_true')
57+
self.parser.add_argument(
58+
'--debug',
59+
action='store_true')
60+
self.parser.add_argument(
61+
'--help',
62+
action='store_true')
63+
4964
def print_help(self):
5065
print('Usage: pki-server password-find [OPTIONS]')
5166
print()
@@ -57,36 +72,19 @@ def print_help(self):
5772

5873
def execute(self, argv):
5974

60-
try:
61-
opts, _ = getopt.gnu_getopt(argv, 'i:v', [
62-
'instance=', 'force',
63-
'verbose', 'debug', 'help'])
75+
args = self.parser.parse_args(args=argv)
6476

65-
except getopt.GetoptError as e:
66-
print('ERROR: %s' % e)
77+
if args.help:
6778
self.print_help()
68-
sys.exit(1)
69-
70-
instance_name = 'pki-tomcat'
71-
72-
for o, a in opts:
73-
if o in ('-i', '--instance'):
74-
instance_name = a
79+
return
7580

76-
elif o in ('-v', '--verbose'):
77-
logging.getLogger().setLevel(logging.INFO)
81+
if args.debug:
82+
logging.getLogger().setLevel(logging.DEBUG)
7883

79-
elif o == '--debug':
80-
logging.getLogger().setLevel(logging.DEBUG)
84+
elif args.verbose:
85+
logging.getLogger().setLevel(logging.INFO)
8186

82-
elif o == '--help':
83-
self.print_help()
84-
sys.exit()
85-
86-
else:
87-
print('ERROR: unknown option: %s' % o)
88-
self.print_help()
89-
sys.exit(1)
87+
instance_name = args.instance
9088

9189
instance = pki.server.PKIServerFactory.create(instance_name)
9290

@@ -112,6 +110,26 @@ class PasswordAddCLI(pki.cli.CLI):
112110
def __init__(self):
113111
super().__init__('add', 'Add password')
114112

113+
self.parser = argparse.ArgumentParser(
114+
prog=self.name,
115+
add_help=False)
116+
self.parser.add_argument(
117+
'-i',
118+
'--instance',
119+
default='pki-tomcat')
120+
self.parser.add_argument('--password')
121+
self.parser.add_argument(
122+
'-v',
123+
'--verbose',
124+
action='store_true')
125+
self.parser.add_argument(
126+
'--debug',
127+
action='store_true')
128+
self.parser.add_argument(
129+
'--help',
130+
action='store_true')
131+
self.parser.add_argument('name')
132+
115133
def print_help(self):
116134
print('Usage: pki-server password-add [OPTIONS] <password ID>')
117135
print()
@@ -124,46 +142,21 @@ def print_help(self):
124142

125143
def execute(self, argv):
126144

127-
try:
128-
opts, args = getopt.gnu_getopt(argv, 'i:v', [
129-
'instance=',
130-
'password=',
131-
'verbose', 'debug', 'help'])
145+
args = self.parser.parse_args(args=argv)
132146

133-
except getopt.GetoptError as e:
134-
print('ERROR: %s' % e)
147+
if args.help:
135148
self.print_help()
136-
sys.exit(1)
137-
138-
instance_name = 'pki-tomcat'
139-
password = None
140-
141-
for o, a in opts:
142-
if o in ('-i', '--instance'):
143-
instance_name = a
144-
145-
elif o == '--password':
146-
password = a
147-
148-
elif o in ('-v', '--verbose'):
149-
logging.getLogger().setLevel(logging.INFO)
149+
return
150150

151-
elif o == '--debug':
152-
logging.getLogger().setLevel(logging.DEBUG)
151+
if args.debug:
152+
logging.getLogger().setLevel(logging.DEBUG)
153153

154-
elif o == '--help':
155-
self.print_help()
156-
sys.exit()
154+
elif args.verbose:
155+
logging.getLogger().setLevel(logging.INFO)
157156

158-
else:
159-
print('ERROR: Unknown option: %s' % o)
160-
self.print_help()
161-
sys.exit(1)
162-
163-
if len(args) < 1:
164-
raise Exception('Missing password ID')
165-
166-
name = args[0]
157+
instance_name = args.instance
158+
password = args.password
159+
name = args.name
167160

168161
instance = pki.server.PKIServerFactory.create(instance_name)
169162

@@ -184,6 +177,25 @@ class PasswordRemoveCLI(pki.cli.CLI):
184177
def __init__(self):
185178
super().__init__('del', 'Remove password')
186179

180+
self.parser = argparse.ArgumentParser(
181+
prog=self.name,
182+
add_help=False)
183+
self.parser.add_argument(
184+
'-i',
185+
'--instance',
186+
default='pki-tomcat')
187+
self.parser.add_argument(
188+
'-v',
189+
'--verbose',
190+
action='store_true')
191+
self.parser.add_argument(
192+
'--debug',
193+
action='store_true')
194+
self.parser.add_argument(
195+
'--help',
196+
action='store_true')
197+
self.parser.add_argument('name')
198+
187199
def print_help(self):
188200
print('Usage: pki-server password-del [OPTIONS] <password ID>')
189201
print()
@@ -195,41 +207,20 @@ def print_help(self):
195207

196208
def execute(self, argv):
197209

198-
try:
199-
opts, args = getopt.gnu_getopt(argv, 'i:v', [
200-
'instance=',
201-
'verbose', 'debug', 'help'])
210+
args = self.parser.parse_args(args=argv)
202211

203-
except getopt.GetoptError as e:
204-
print('ERROR: %s' % e)
212+
if args.help:
205213
self.print_help()
206-
sys.exit(1)
207-
208-
instance_name = 'pki-tomcat'
214+
return
209215

210-
for o, a in opts:
211-
if o in ('-i', '--instance'):
212-
instance_name = a
213-
214-
elif o in ('-v', '--verbose'):
215-
logging.getLogger().setLevel(logging.INFO)
216-
217-
elif o == '--debug':
218-
logging.getLogger().setLevel(logging.DEBUG)
219-
220-
elif o == '--help':
221-
self.print_help()
222-
sys.exit()
223-
224-
else:
225-
print('ERROR: Unknown option: %s' % o)
226-
self.print_help()
227-
sys.exit(1)
216+
if args.debug:
217+
logging.getLogger().setLevel(logging.DEBUG)
228218

229-
if len(args) < 1:
230-
raise Exception('Missing password ID')
219+
elif args.verbose:
220+
logging.getLogger().setLevel(logging.INFO)
231221

232-
name = args[0]
222+
instance_name = args.instance
223+
name = args.name
233224

234225
instance = pki.server.PKIServerFactory.create(instance_name)
235226

0 commit comments

Comments
 (0)