Move command line argument parsing to the top

This will make it possible to load localization data for a language
defined by a command-line argument, throughout the program.

Unfortunately this separates the usage of the arguments from their
definitions by a wide margin, making it harder to edit, especially
with AI autocomplete. For now. I could refactor things, either move
the bulk of this file to separate files, or define a configure_app
function up top to use down below.
This commit is contained in:
Isaiah Odhner 2023-04-18 03:21:16 -04:00
parent 8971122581
commit 5e994fbc91

View File

@ -24,9 +24,35 @@ from menus import MenuBar, Menu, MenuItem, Separator
from windows import Window, DialogWindow
from localization.i18n import get as _
# These can go away now that args are parsed up top
ascii_only_icons = False
inspect_layout = False
# Command line arguments
# Please keep in sync with the README
parser = argparse.ArgumentParser(description='Paint in the terminal.')
parser.add_argument('--theme', default='light', help='Theme to use, either "light" or "dark"', choices=['light', 'dark'])
parser.add_argument('--ascii-only-icons', action='store_true', help='Use only ASCII characters for tool icons')
parser.add_argument('--inspect-layout', action='store_true', help='Inspect the layout with middle click, for development')
# This flag is for development, because it's very confusing
# to see the error message from the previous run,
# when a problem is actually solved.
# There are enough ACTUAL "that should have worked!!" moments to deal with.
# I really don't want false ones mixed in. You want to reward your brain for finding good solutions, after all.
parser.add_argument('--clear-screen', action='store_true', help='Clear the screen before starting; useful for development, to avoid seeing fixed errors')
parser.add_argument('filename', nargs='?', default=None, help='File to open')
if __name__ == "<run_path>":
# Arguments have to be passed like `textual run --dev "paint.py LICENSE.txt"`
# so we need to look for an argument starting with "paint.py",
# and parse the rest of the string as arguments.
args = None
for arg in sys.argv:
if arg.startswith("paint.py"):
args = parser.parse_args(arg[len("paint.py") :].split())
break
else:
args = parser.parse_args()
class Tool(Enum):
"""The tools available in the Paint app."""
free_form_select = 1
@ -1319,30 +1345,6 @@ class PaintApp(App):
# and it would create a new app instance, and all arguments would be ignored.
app = PaintApp()
# Command line arguments
# Please keep in sync with the README
parser = argparse.ArgumentParser(description='Paint in the terminal.')
parser.add_argument('--theme', default='light', help='Theme to use, either "light" or "dark"', choices=['light', 'dark'])
parser.add_argument('--ascii-only-icons', action='store_true', help='Use only ASCII characters for tool icons')
parser.add_argument('--inspect-layout', action='store_true', help='Inspect the layout with middle click, for development')
# This flag is for development, because it's very confusing
# to see the error message from the previous run,
# when a problem is actually solved.
# There are enough ACTUAL "that should have worked!!" moments to deal with.
# I really don't want false ones mixed in. You want to reward your brain for finding good solutions, after all.
parser.add_argument('--clear-screen', action='store_true', help='Clear the screen before starting; useful for development, to avoid seeing fixed errors')
parser.add_argument('filename', nargs='?', default=None, help='File to open')
if __name__ == "<run_path>":
# Arguments have to be passed like `textual run --dev "paint.py LICENSE.txt"`
# so we need to look for an argument starting with "paint.py",
# and parse the rest of the string as arguments.
args = None
for arg in sys.argv:
if arg.startswith("paint.py"):
args = parser.parse_args(arg[len("paint.py") :].split())
break
else:
args = parser.parse_args()
if args.ascii_only_icons:
ascii_only_icons = True
if args.inspect_layout: