diff --git a/typstwriter/arguments.py b/typstwriter/arguments.py new file mode 100644 index 0000000..1b1da44 --- /dev/null +++ b/typstwriter/arguments.py @@ -0,0 +1,76 @@ +import argparse + +import typstwriter + + +class APA(argparse.ArgumentParser): + """ + A customized subclass of argparse.ArgumentParser. + + Displays the description before the usage in the help output, capitalizes the section titles and adds a prolog section. + """ + + def __init__(self, *args, prolog=None, **kwargs): + """Patch positionals and optionals display names.""" + argparse.ArgumentParser.__init__(self, *args, **kwargs) + self.prolog = prolog + self._positionals.title = "Arguments" + self._optionals.title = "Options" + + def format_usage(self): + """Format the usage text.""" + formatter = self._get_formatter() + formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups, "Usage: ") + return formatter.format_help() + + def format_help(self): + """Format the help text.""" + formatter = self._get_formatter() + + # description + formatter.add_text(self.description) + + # prolog + formatter.add_text(self.prolog) + + # usage + formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups, "Usage: ") + + # positionals, optionals and user-defined groups + for action_group in self._action_groups: + formatter.start_section(action_group.title) + formatter.add_text(action_group.description) + formatter.add_arguments(action_group._group_actions) + formatter.end_section() + + # epilog + formatter.add_text(self.epilog) + + # format help message + return formatter.format_help() + + +def parse_args(): + """Parse CLI arguments.""" + parser = APA(description=typstwriter.__doc__, add_help=False) + + parser.add_argument("files", nargs="*", help="The list of files to open.") + + parser.add_argument( + "-h", + "--help", + action="help", + help="Show this help message and exit.", + ) + parser.add_argument( + "-V", + "--version", + action="version", + version=f"Typstwriter {typstwriter.__version__}", + help="Show the Typstwriter version and exit.", + ) + + return parser.parse_args() + + +Args = parse_args() diff --git a/typstwriter/mainwindow.py b/typstwriter/mainwindow.py index f0a4d7b..0cfdfba 100644 --- a/typstwriter/mainwindow.py +++ b/typstwriter/mainwindow.py @@ -15,10 +15,12 @@ from typstwriter import logging from typstwriter import configuration from typstwriter import globalstate +from typstwriter import arguments logger = logging.getLogger(__name__) config = configuration.Config state = globalstate.State +args = arguments.Args class MainWindow(QtWidgets.QMainWindow): @@ -159,6 +161,10 @@ def __init__(self): # Use default layout self.use_default_layout() + # Open files if given as arguments + for file in args.files: + self.editor.open_file(file) + # Check if typst is available QtCore.QTimer().singleShot(0, self.check_typst_availability) diff --git a/typstwriter/typstwriter.py b/typstwriter/typstwriter.py index 0cdbb0d..eebd84d 100644 --- a/typstwriter/typstwriter.py +++ b/typstwriter/typstwriter.py @@ -14,7 +14,13 @@ def main(): logging.setup_logger(os.environ.get("LOGLEVEL")) logger = logging.getLogger(__name__) + logger.debug("Logging initialized") + # Parse Arguments + logger.debug("Parse Arguments") + from typstwriter import arguments # noqa: F401 + + # Start Typstwriter logger.info("Typstwriter started") # Initialise Config