|
| 1 | +""" |
| 2 | +_app.py — Typer application instance, arg normaliser, and namespace factory. |
| 3 | +""" |
| 4 | + |
| 5 | +# Copyright (C) 2015 Stefano Zaghi |
| 6 | +# |
| 7 | +# This file is part of FoBiS.py. |
| 8 | +# |
| 9 | +# FoBiS.py is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# FoBiS.py is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with FoBiS.py. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +import argparse |
| 23 | +import re |
| 24 | + |
| 25 | +import typer |
| 26 | +from typing_extensions import Annotated |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +# Argument normaliser — preserves backward compat with argparse-style options |
| 30 | +# --------------------------------------------------------------------------- |
| 31 | +_MULTI_CHAR_OPT = re.compile(r"^-[A-Za-z][A-Za-z0-9_-]+$") |
| 32 | + |
| 33 | + |
| 34 | +def _normalize_args(args): |
| 35 | + """ |
| 36 | + Normalise FoBiS legacy single-dash long options for Click/Typer. |
| 37 | +
|
| 38 | + Rules applied to each token: |
| 39 | + - Single-dash multi-char option (-compiler, -mode, -get_output_name) |
| 40 | + → double-dash with underscores turned to hyphens (--compiler, --mode, --get-output-name) |
| 41 | + - Double-dash option with underscores (--build_dir, --cflags_heritage) |
| 42 | + → double-dash with hyphens (--build-dir, --cflags-heritage) |
| 43 | + - Single-char short options (-f, -m, -q), values, and negative numbers |
| 44 | + are left unchanged. |
| 45 | + """ |
| 46 | + result = [] |
| 47 | + for arg in args: |
| 48 | + if _MULTI_CHAR_OPT.match(arg): |
| 49 | + result.append("--" + arg[1:].replace("_", "-")) |
| 50 | + elif arg.startswith("--") and len(arg) > 2: |
| 51 | + if "=" in arg: |
| 52 | + opt, val = arg[2:].split("=", 1) |
| 53 | + result.append("--" + opt.replace("_", "-") + "=" + val) |
| 54 | + else: |
| 55 | + result.append("--" + arg[2:].replace("_", "-")) |
| 56 | + else: |
| 57 | + result.append(arg) |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Typer application |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | +app = typer.Typer( |
| 65 | + name="FoBiS.py", |
| 66 | + help="a Fortran Building System", |
| 67 | + no_args_is_help=True, |
| 68 | + add_completion=True, |
| 69 | + rich_markup_mode=None, |
| 70 | +) |
| 71 | + |
| 72 | + |
| 73 | +def _version_callback(value: bool): |
| 74 | + if value: |
| 75 | + from .. import __version__ |
| 76 | + from ..FoBiSConfig import __appname__ |
| 77 | + |
| 78 | + typer.echo(f"{__appname__} {__version__}") |
| 79 | + raise typer.Exit() |
| 80 | + |
| 81 | + |
| 82 | +@app.callback() |
| 83 | +def _app_callback( |
| 84 | + ctx: typer.Context, |
| 85 | + version: Annotated[ |
| 86 | + bool, |
| 87 | + typer.Option( |
| 88 | + "--version", |
| 89 | + "-v", |
| 90 | + help="Show version and exit.", |
| 91 | + callback=_version_callback, |
| 92 | + is_eager=True, |
| 93 | + ), |
| 94 | + ] = False, |
| 95 | +): |
| 96 | + ctx.ensure_object(dict) |
| 97 | + |
| 98 | + |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | +# Namespace factory |
| 101 | +# --------------------------------------------------------------------------- |
| 102 | + |
| 103 | + |
| 104 | +def _ns(**kwargs) -> argparse.Namespace: |
| 105 | + """Build an argparse.Namespace — preserves the duck-type expected by all downstream code.""" |
| 106 | + return argparse.Namespace(**kwargs) |
0 commit comments