Verified Commit 84b843a9 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Refactor argument parsing

parent 3f2ccd06
Loading
Loading
Loading
Loading
+2 −46
Original line number Diff line number Diff line
import argparse
import sys

from pathlib import Path
import lark.exceptions

from rpcc.arguments import parse_args
from rpcc.exceptions import RpccError
from rpcc.console import console
from rpcc.version import __version__ as rpcc_version
from rpcc.parser import Parser
from rpcc.transformers.cxx import Transformer as CxxTransformer


class PrintVersion(argparse.Action):
    def __init__(self, option_strings, dest, const=None, default=None,
                 required=False, help=None, metavar=None):
        super().__init__(option_strings=option_strings,
                         dest=dest,
                         nargs=0,
                         const=const,
                         default=default,
                         required=required,
                         help=help)

    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, self.const)
        console.print(f"rpcc {rpcc_version}")
        sys.exit(0)


def main(args=None):
    """"The main routine"""

    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(prog="rpcc",
                                     description="Parse RPC_PROTO_FILE and generate C++ output based on the options "
                                                 "given.")
    parser.add_argument(
        "rpc_proto_file",
        type=Path,
        metavar="RPC_PROTO_FILE",
        help="The file containing the specifications for the RPCs."
    )

    parser.add_argument(
        "--color-diagnostics",
        choices=["never", "always", "auto"],
        metavar="WHEN",
        default="auto",
        help="Use escape sequences in diagnostic messages to display them in color in the terminal. WHEN is never, "
             "always, or auto (the default)."
    )

    parser.add_argument(
        "--version",
        "-V",
        action=PrintVersion,
        help="Display rpcc version information."
    )
    args = parser.parse_args()
    args = parse_args(args)

    console.configure(args.color_diagnostics)

rpcc/arguments.py

0 → 100644
+52 −0
Original line number Diff line number Diff line
import argparse
import sys
from pathlib import Path
from rpcc.version import __version__ as rpcc_version
from rpcc.console import console


class PrintVersion(argparse.Action):
    def __init__(self, option_strings, dest, const=None, default=None,
                 required=False, help=None, metavar=None):
        super().__init__(option_strings=option_strings,
                         dest=dest,
                         nargs=0,
                         const=const,
                         default=default,
                         required=required,
                         help=help)

    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, self.const)
        console.print(f"rpcc {rpcc_version}")
        sys.exit(0)


def parse_args(args):
    parser = argparse.ArgumentParser(prog="rpcc",
                                     description="Parse RPC_PROTO_FILE and generate C++ output based on the options "
                                                 "given.")
    parser.add_argument(
        "rpc_proto_file",
        type=Path,
        metavar="RPC_PROTO_FILE",
        help="The file containing the specifications for the RPCs."
    )

    parser.add_argument(
        "--color-diagnostics",
        choices=["never", "always", "auto"],
        metavar="WHEN",
        default="auto",
        help="Use escape sequences in diagnostic messages to display them in color in the terminal. WHEN is never, "
             "always, or auto (the default)."
    )

    parser.add_argument(
        "--version",
        "-V",
        action=PrintVersion,
        help="Display rpcc version information."
    )

    return parser.parse_args(args)