Skip to content

Commit

Permalink
Add basic CLI options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bzero committed Aug 30, 2024
1 parent 08a6f98 commit 24ddee0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
76 changes: 76 additions & 0 deletions typstwriter/arguments.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions typstwriter/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions typstwriter/typstwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24ddee0

Please sign in to comment.