18
18
# All rights reserved.
19
19
#
20
20
21
- from __future__ import absolute_import
22
- from __future__ import print_function
23
- import getopt
21
+ import argparse
24
22
import logging
25
- import sys
26
23
27
24
import pki .cli
28
25
@@ -46,6 +43,24 @@ class PasswordFindCLI(pki.cli.CLI):
46
43
def __init__ (self ):
47
44
super ().__init__ ('find' , 'Find passwords' )
48
45
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
+
49
64
def print_help (self ):
50
65
print ('Usage: pki-server password-find [OPTIONS]' )
51
66
print ()
@@ -57,36 +72,19 @@ def print_help(self):
57
72
58
73
def execute (self , argv ):
59
74
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 )
64
76
65
- except getopt .GetoptError as e :
66
- print ('ERROR: %s' % e )
77
+ if args .help :
67
78
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
75
80
76
- elif o in ( '-v' , '--verbose' ) :
77
- logging .getLogger ().setLevel (logging .INFO )
81
+ if args . debug :
82
+ logging .getLogger ().setLevel (logging .DEBUG )
78
83
79
- elif o == '--debug' :
80
- logging .getLogger ().setLevel (logging .DEBUG )
84
+ elif args . verbose :
85
+ logging .getLogger ().setLevel (logging .INFO )
81
86
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
90
88
91
89
instance = pki .server .PKIServerFactory .create (instance_name )
92
90
@@ -112,6 +110,26 @@ class PasswordAddCLI(pki.cli.CLI):
112
110
def __init__ (self ):
113
111
super ().__init__ ('add' , 'Add password' )
114
112
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
+
115
133
def print_help (self ):
116
134
print ('Usage: pki-server password-add [OPTIONS] <password ID>' )
117
135
print ()
@@ -124,46 +142,21 @@ def print_help(self):
124
142
125
143
def execute (self , argv ):
126
144
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 )
132
146
133
- except getopt .GetoptError as e :
134
- print ('ERROR: %s' % e )
147
+ if args .help :
135
148
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
150
150
151
- elif o == '-- debug' :
152
- logging .getLogger ().setLevel (logging .DEBUG )
151
+ if args . debug :
152
+ logging .getLogger ().setLevel (logging .DEBUG )
153
153
154
- elif o == '--help' :
155
- self .print_help ()
156
- sys .exit ()
154
+ elif args .verbose :
155
+ logging .getLogger ().setLevel (logging .INFO )
157
156
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
167
160
168
161
instance = pki .server .PKIServerFactory .create (instance_name )
169
162
@@ -184,6 +177,25 @@ class PasswordRemoveCLI(pki.cli.CLI):
184
177
def __init__ (self ):
185
178
super ().__init__ ('del' , 'Remove password' )
186
179
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
+
187
199
def print_help (self ):
188
200
print ('Usage: pki-server password-del [OPTIONS] <password ID>' )
189
201
print ()
@@ -195,41 +207,20 @@ def print_help(self):
195
207
196
208
def execute (self , argv ):
197
209
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 )
202
211
203
- except getopt .GetoptError as e :
204
- print ('ERROR: %s' % e )
212
+ if args .help :
205
213
self .print_help ()
206
- sys .exit (1 )
207
-
208
- instance_name = 'pki-tomcat'
214
+ return
209
215
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 )
228
218
229
- if len ( args ) < 1 :
230
- raise Exception ( 'Missing password ID' )
219
+ elif args . verbose :
220
+ logging . getLogger (). setLevel ( logging . INFO )
231
221
232
- name = args [0 ]
222
+ instance_name = args .instance
223
+ name = args .name
233
224
234
225
instance = pki .server .PKIServerFactory .create (instance_name )
235
226
0 commit comments