Skip to content

Commit

Permalink
More graceful exit on early keyboard interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
acabal committed Jan 6, 2025
1 parent 6c6cb72 commit 62314d2
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions se/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,52 @@ def main() -> None:
This function delegates subcommands (like `se typogrify`) to individual submodules under `se.commands`.
"""

# If we're asked for the version, short circuit and exit
if len(sys.argv) == 2 and (sys.argv[1] == "-v" or sys.argv[1] == "--version"):
module = importlib.import_module("se.commands.version")
sys.exit(getattr(module, "version")())

commands = get_commands()

parser = argparse.ArgumentParser(description="The entry point for the Standard Ebooks toolset.")
parser.add_argument("-p", "--plain", dest="plain_output", action="store_true", help="print plain text output, without tables or formatting")
parser.add_argument("-v", "--version", action="store_true", help="print version number and exit")
parser.add_argument("command", metavar="COMMAND", choices=commands, help="one of: " + " ".join(commands))
parser.add_argument("arguments", metavar="ARGS", nargs="*", help="arguments for the subcommand")

# We do some hand-parsing of high-level args, because argparse
# can expect flags at any point in the command. We'll pass any args up to
# and including the subcommand to the main argparse instance, then pass
# the subcommand and its args to the final function we call.
main_args = []
subcommand_args = []
parsing_subcommand = False
for arg in sys.argv[1:]:
if not parsing_subcommand and arg.startswith("-"):
main_args.append(arg)
elif not parsing_subcommand and not arg.startswith("-"):
main_args.append(arg)
subcommand_args.append(arg)
parsing_subcommand = True
elif parsing_subcommand:
subcommand_args.append(arg)

args = parser.parse_args(main_args)

# Change argv to our subcommand values, so that arg parsing by child functions works as expected
sys.argv = subcommand_args

command_name = args.command.replace("-", "_")
command_module = f"se.commands.{command_name}"
if command_name == "help":
command_function = "se_help" # Avoid name conflict with built-in function
else:
command_function = command_name

# Import command module and call command entrypoint
module = importlib.import_module(command_module)

try:
# If we're asked for the version, short circuit and exit
if len(sys.argv) == 2 and (sys.argv[1] == "-v" or sys.argv[1] == "--version"):
module = importlib.import_module("se.commands.version")
sys.exit(getattr(module, "version")())

commands = get_commands()

parser = argparse.ArgumentParser(description="The entry point for the Standard Ebooks toolset.")
parser.add_argument("-p", "--plain", dest="plain_output", action="store_true", help="print plain text output, without tables or formatting")
parser.add_argument("-v", "--version", action="store_true", help="print version number and exit")
parser.add_argument("command", metavar="COMMAND", choices=commands, help="one of: " + " ".join(commands))
parser.add_argument("arguments", metavar="ARGS", nargs="*", help="arguments for the subcommand")

# We do some hand-parsing of high-level args, because argparse
# can expect flags at any point in the command. We'll pass any args up to
# and including the subcommand to the main argparse instance, then pass
# the subcommand and its args to the final function we call.
main_args = []
subcommand_args = []
parsing_subcommand = False
for arg in sys.argv[1:]:
if not parsing_subcommand and arg.startswith("-"):
main_args.append(arg)
elif not parsing_subcommand and not arg.startswith("-"):
main_args.append(arg)
subcommand_args.append(arg)
parsing_subcommand = True
elif parsing_subcommand:
subcommand_args.append(arg)

args = parser.parse_args(main_args)

# Change argv to our subcommand values, so that arg parsing by child functions works as expected
sys.argv = subcommand_args

command_name = args.command.replace("-", "_")
command_module = f"se.commands.{command_name}"
if command_name == "help":
command_function = "se_help" # Avoid name conflict with built-in function
else:
command_function = command_name

# Import command module and call command entrypoint
module = importlib.import_module(command_module)

sys.exit(getattr(module, command_function)(args.plain_output))
except KeyboardInterrupt:
sys.exit(130) # See http://www.tldp.org/LDP/abs/html/exitcodes.html

0 comments on commit 62314d2

Please sign in to comment.