|
| 1 | +############################################################################## |
| 2 | +# AUTHOR(S): Vaclav Petras <wenzeslaus gmail com> |
| 3 | +# |
| 4 | +# PURPOSE: Python-driven CLI interface |
| 5 | +# |
| 6 | +# COPYRIGHT: (C) 2025 Vaclav Petras and the GRASS Development Team |
| 7 | +# |
| 8 | +# This program is free software under the GNU General Public |
| 9 | +# License (>=v2). Read the file COPYING that comes with GRASS |
| 10 | +# for details. |
| 11 | +############################################################################## |
| 12 | + |
| 13 | +""" |
| 14 | +Experimental low-level CLI interface for the main GRASS executable functionality |
| 15 | +
|
| 16 | +This is not a stable part of the API. Contact developers before using it. |
| 17 | +""" |
| 18 | + |
| 19 | +import argparse |
| 20 | +import tempfile |
| 21 | +import os |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import grass.script as gs |
| 26 | + |
| 27 | + |
| 28 | +def call_g_manual(**kwargs): |
| 29 | + with tempfile.TemporaryDirectory() as tmp_dir_name: |
| 30 | + project_name = "project" |
| 31 | + project_path = Path(tmp_dir_name) / project_name |
| 32 | + gs.create_project(project_path) |
| 33 | + with gs.setup.init(project_path) as session: |
| 34 | + return gs.run_command( |
| 35 | + "g.manual", **kwargs, env=session.env, errors="status" |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def subcommand_show_help(args): |
| 40 | + return call_g_manual(entry=args.page) |
| 41 | + |
| 42 | + |
| 43 | +def subcommand_show_man(args): |
| 44 | + return call_g_manual(entry=args.page, flags="m") |
| 45 | + |
| 46 | + |
| 47 | +def main(args=None, program=None): |
| 48 | + # Top-level parser |
| 49 | + if program is None: |
| 50 | + program = os.path.basename(sys.argv[0]) |
| 51 | + if program == "__main__.py": |
| 52 | + program = f"{Path(sys.executable).name} -m grass.app" |
| 53 | + parser = argparse.ArgumentParser( |
| 54 | + description="Experimental low-level CLI interface to GRASS. Consult developers before using it.", |
| 55 | + prog=program, |
| 56 | + ) |
| 57 | + subparsers = parser.add_subparsers( |
| 58 | + title="subcommands", required=True, dest="subcommand" |
| 59 | + ) |
| 60 | + |
| 61 | + # Subcommand parsers |
| 62 | + |
| 63 | + subparser = subparsers.add_parser( |
| 64 | + "help", help="show HTML documentation for a tool or topic" |
| 65 | + ) |
| 66 | + subparser.add_argument("page", type=str) |
| 67 | + subparser.set_defaults(func=subcommand_show_help) |
| 68 | + |
| 69 | + subparser = subparsers.add_parser( |
| 70 | + "man", help="show man page for a tool or topic using" |
| 71 | + ) |
| 72 | + subparser.add_argument("page", type=str) |
| 73 | + subparser.set_defaults(func=subcommand_show_man) |
| 74 | + |
| 75 | + parsed_args = parser.parse_args(args) |
| 76 | + return parsed_args.func(parsed_args) |
0 commit comments