-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
If I parse a command line and I use the option -h or --help, the program exits through a path not under my control, i.e. if in a try/except block, the program just exits, this is ok for the usual case, but if my program is modal, that is I have different parsings for different modes, I'd like to capture a bad option (or simply handle a return from --help). In the modal case, a bad parse or even --help will cause the program to lock up.
note: parse_known_args() locks up
the parse routine:
def parse_afe_send(theLine):
#
err_f = False
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--iter", type=int,
default=1, help="iterations.")
parser.add_argument("-d", "--delay",type=int, default=0,
help="seconds delay between iterations.")
parser.add_argument("-r", "--response",action='store_true',
help="response expected.")
parser.add_argument("-e", "--expect",
default="", help="expected response.")
parser.add_argument("-a", "--append",
default="", help="append response to file.")
#
# adding listen without wait argument
# default delay determine stochastically
#
theWait = 3
parser.add_argument("-l", "--listen",type = int,
default = theWait,
const = theWait,
nargs='?',
help = "listen seconds.")
#
# Note 1: bad option causes exit()
# which bypasses the exception handler and
# prevents cleanup, so vector elsewhere
#
# Note 2: argument parser doesn't handle no options, so
# treats a no option parameter as an error,
# Need to have a pass-through for this case
#
try:
(args,unknowns) = parser.parse_known_args(theLine)
except Exception as eobj:
#
# One would think that a bad option leads to unknowns,
# however option -3 does not
#
err_str = "options exception in parse_afe_send()"
err_code = -1
eobj_str = repr(eobj)
errDict = {'err_code': err_code, 'err_str': err_str, 'eobj_str': eobj_str}
raise UserDefinedException(errDict)
else:
theLast = len(unknowns)
ii = 0
while (ii < theLast):
if (unknowns[ii][0] == '-'):
print("unknown option=",unknowns[ii])
err_f = True # keep going, print all
ii = ii + 1
# end while
if (err_f):
err_str = "other options exception in parse_afe_send()"
err_code = -2
eobj_str = None
errDict = {'err_code': err_code, 'err_str': err_str, 'eobj_str': eobj_str}
raise UserDefinedException(errDict)
# end else
return args
# end parse_afe_send
the invocation, trimmed:
try:
shlex_out = shlex.split(theCmdString)
except Exception as eobj: # likely no closing quotations
print("afe_send: shlex exception:",eobj)
status = -2
err_f = True
return status
# # still here?
try:
args = parse_afe_send(shlex_out) # parse send options
except Exception as eobj:
err_f = True
err_str = "afe_send: other options exception in parse_afe_send()"
err_code = -3
eobj_str = repr(eobj)
errDict = {'err_code': err_code, 'err_str': err_str, 'eobj_str': eobj_str}
raise UserDefinedException(errDict)
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
Status
Doc issues