-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix getopt deprecation warnings (part 1) #4908
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d3d4a9
Update pki.server.cli.banner to use argparse
edewata 5ed9960
Update pki.server.cli.group to use argparse
edewata fcb9fe9
Update pki.server.cli.jss to use argparse
edewata 059e6a5
Update pki.server.cli.listener to use argparse
edewata b5160d5
Update pki.server.cli.migrate to use argparse
edewata 1ab2c49
Update pki.server.cli.nss to use argparse
edewata 6d2c85d
Update pki.server.cli.password to use argparse
edewata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,7 @@ | |
# All rights reserved. | ||
# | ||
|
||
from __future__ import absolute_import | ||
from __future__ import print_function | ||
|
||
import getopt | ||
import argparse | ||
import logging | ||
import io | ||
import sys | ||
|
@@ -45,7 +42,25 @@ class BannerShowCLI(pki.cli.CLI): | |
def __init__(self): | ||
super().__init__('show', 'Show banner') | ||
|
||
def usage(self): | ||
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 banner-show [OPTIONS]') | ||
print() | ||
print(' -i, --instance <instance ID> Instance ID (default: pki-tomcat).') | ||
|
@@ -56,36 +71,19 @@ def usage(self): | |
|
||
def execute(self, argv): | ||
|
||
try: | ||
opts, _ = getopt.gnu_getopt(argv, 'i:v', [ | ||
'instance=', | ||
'verbose', 'debug', 'help']) | ||
|
||
except getopt.GetoptError as e: | ||
logger.error(e) | ||
self.usage() | ||
sys.exit(1) | ||
|
||
instance_name = 'pki-tomcat' | ||
|
||
for o, a in opts: | ||
if o in ('-i', '--instance'): | ||
instance_name = a | ||
args = self.parser.parse_args(args=argv) | ||
|
||
elif o == '--debug': | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
if args.help: | ||
self.print_help() | ||
return | ||
|
||
elif o in ('-v', '--verbose'): | ||
logging.getLogger().setLevel(logging.INFO) | ||
if args.debug: | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
|
||
elif o == '--help': | ||
self.usage() | ||
sys.exit() | ||
elif args.verbose: | ||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
else: | ||
logger.error('Unknown option: %s', o) | ||
self.usage() | ||
sys.exit(1) | ||
instance_name = args.instance | ||
|
||
instance = pki.server.PKIServerFactory.create(instance_name) | ||
|
||
|
@@ -107,7 +105,29 @@ class BannerValidateCLI(pki.cli.CLI): | |
def __init__(self): | ||
super().__init__('validate', 'Validate banner') | ||
|
||
def usage(self): | ||
self.parser = argparse.ArgumentParser( | ||
prog=self.name, | ||
add_help=False) | ||
self.parser.add_argument( | ||
'-i', | ||
'--instance', | ||
default='pki-tomcat') | ||
self.parser.add_argument('--file') | ||
self.parser.add_argument( | ||
'--silent', | ||
action='store_true') | ||
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 banner-validate [OPTIONS]') | ||
print() | ||
print(' -i, --instance <instance ID> Instance ID (default: pki-tomcat).') | ||
|
@@ -120,44 +140,21 @@ def usage(self): | |
|
||
def execute(self, argv): | ||
|
||
try: | ||
opts, _ = getopt.gnu_getopt(argv, 'i:v', [ | ||
'instance=', 'file=', 'silent', | ||
'verbose', 'debug', 'help']) | ||
args = self.parser.parse_args(args=argv) | ||
|
||
except getopt.GetoptError as e: | ||
logger.error(e) | ||
self.usage() | ||
sys.exit(1) | ||
|
||
instance_name = 'pki-tomcat' | ||
banner_file = None | ||
silent = False | ||
|
||
for o, a in opts: | ||
if o in ('-i', '--instance'): | ||
instance_name = a | ||
|
||
elif o == '--file': | ||
banner_file = a | ||
if args.help: | ||
self.print_help() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above |
||
return | ||
|
||
elif o == '--debug': | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
if args.debug: | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
|
||
elif o in ('-v', '--verbose'): | ||
logging.getLogger().setLevel(logging.INFO) | ||
elif args.verbose: | ||
logging.getLogger().setLevel(logging.INFO) | ||
|
||
elif o == '--silent': | ||
silent = True | ||
|
||
elif o == '--help': | ||
self.usage() | ||
sys.exit() | ||
|
||
else: | ||
logger.error('Unknown option: %s', o) | ||
self.usage() | ||
sys.exit(1) | ||
instance_name = args.instance | ||
banner_file = args.file | ||
silent = args.silent | ||
|
||
try: | ||
if banner_file: | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
print_help()
is not defined in the BannerShowCLI.The argparse has its way to print the help message, we could use it making easier to keep option and description aligned but not sure how to configure with these modular commands. Can be investigated in separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I've renamed
usage()
toprint_help()
.For now I just want to fix the deprecation warnings, but later we can try to use sub-parsers to define the options & help messages for the sub-commands.