From: Christian Heller Date: Mon, 9 Sep 2024 19:39:48 +0000 (+0200) Subject: For browser.py, simplify argument parsing. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/blog?a=commitdiff_plain;h=02924fc057abf064dea0a797873d11155a69f2fb;p=stable_plom For browser.py, simplify argument parsing. --- diff --git a/browser.py b/browser.py index 33791a6..5f6c26b 100755 --- a/browser.py +++ b/browser.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 """Browser for image files.""" -from sys import argv as sys_argv from json import dump as json_dump, load as json_load from os.path import exists as path_exists, join as path_join, abspath +from argparse import ArgumentParser from exiftool import ExifToolHelper # type: ignore import gi # type: ignore gi.require_version('Gtk', '4.0') @@ -262,7 +262,7 @@ class MainWindow(Gtk.Window): return True if a_cmp is None: return False - elif b_cmp is None: + if b_cmp is None: return True return a_cmp > b_cmp @@ -561,18 +561,17 @@ class MainWindow(Gtk.Window): class Application(Gtk.Application): """Image browser application class.""" + img_dir_absolute: str def __init__(self, *args, **kwargs): - super().__init__(flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, - *args, **kwargs) - self.img_dir_absolute = abspath(IMG_DIR_DEFAULT) - - def do_command_line(self, *args, **kwargs): - """Parse directory from command_line, start window and keep it open.""" - command_line = args[0] - argv = command_line.get_arguments() - if len(argv) > 1: - self.img_dir_absolute = abspath(argv[1]) + super().__init__(*args, **kwargs) + + def do_activate(self, *args, **kwargs): + """Parse arguments, start window, and keep it open.""" + parser = ArgumentParser() + parser.add_argument('directory', default=IMG_DIR_DEFAULT, nargs='?') + opts = parser.parse_args() + self.img_dir_absolute = abspath(opts.directory) win = MainWindow(self) win.present() self.hold() @@ -580,4 +579,4 @@ class Application(Gtk.Application): main_app = Application(application_id='plomlompom.com.StablePixBrowser.App') -main_app.run(sys_argv) +main_app.run()