Skip to content

Commit

Permalink
Fix broken filename argument handling (issue olivierkes#898)
Browse files Browse the repository at this point in the history
In addition to fixing the bug, related code that allowed this one to
slip under the radar has been cleaned up. Validation of the FILENAME
argument is now performed during parsing.
  • Loading branch information
worstje committed Jul 10, 2021
1 parent 4bfd663 commit 0a0ffb6
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions manuskript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ def respectSystemDarkThemeSetting():
MW._defaultCursorFlashTime = qApp.cursorFlashTime()

# Command line project
#if len(sys.argv) > 1 and sys.argv[1][-4:] == ".msk":
if arguments.filename is not None and arguments.filename[-4:] == ".msk":
#TODO: integrate better with argparsing.
if os.path.exists(sys.argv[1]):
path = os.path.abspath(sys.argv[1])
MW._autoLoadProject = path
# The file is verified to already exist during argument parsing.
# Our ".msk" check has been moved there too for better feedback,
# but leaving it here to err on the side of caution.
path = os.path.abspath(arguments.filename)
MW._autoLoadProject = path

return app, MW

Expand Down Expand Up @@ -255,14 +255,24 @@ def setup_signal_handlers(MW):
signal.signal(signal.SIGTERM, sigint_handler("SIGTERM", MW))


def is_valid_project(parser, arg):
if arg[-4:] != ".msk":
parser.error("only manuskript projects (.msk) are supported!")
if not os.path.isfile(arg):
parser.error("the project %s does not exist!" % arg)
else:
return arg


def process_commandline(argv):
import argparse
parser = argparse.ArgumentParser(description="Run the manuskript application.")
parser.add_argument("--console", help="open the IPython Jupyter QT Console as a debugging aid",
action="store_true")
parser.add_argument("-v", "--verbose", action="count", default=1, help="lower the threshold for messages logged to the terminal")
parser.add_argument("-L", "--logfile", default=None, help="override the default log file location")
parser.add_argument("filename", nargs="?", metavar="FILENAME", help="the manuskript project (.msk) to open")
parser.add_argument("filename", nargs="?", metavar="FILENAME", help="the manuskript project (.msk) to open",
type=lambda x: is_valid_project(parser, x))

args = parser.parse_args(args=argv)

Expand All @@ -283,7 +293,7 @@ def run():
2. So that prepare can be used in tests, without running the whole thing
"""
# Parse command-line arguments.
arguments = process_commandline(sys.argv)
arguments = process_commandline(sys.argv[1:])
# Initialize logging. (Does not include Qt integration yet.)
manuskript.logging.setUp(console_level=arguments.verbose)

Expand Down

0 comments on commit 0a0ffb6

Please sign in to comment.