Skip to content

fpc command line

fpc.cli

cli.py - the fpc command: library calculations from the terminal.

Rung 5 of docs/building-tools.md: a command that is installed with the package. pyproject.toml maps the name to this module's main::

[project.scripts]
fpc = "fpc.cli:main"

so after installing (uv sync here, or uv tool install git+https://github.com/kovirlabs/first-principles-code anywhere) you can run::

fpc --help
fpc pipe-flow --flow-l-s 12 --diameter-mm 102.3 --length-m 120
fpc water --temperature-c 37

Subcommands (argparse "subparsers") let one program offer several tools, the way git commit and git push share the git command.

build_parser()

Build the top-level parser with one subparser per tool.

Source code in fpc/cli.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def build_parser():
    """Build the top-level parser with one subparser per tool."""
    parser = argparse.ArgumentParser(
        prog="fpc",
        description="First Principles Code engineering calculations.",
    )
    tools = parser.add_subparsers(dest="tool", required=True)

    pipe = tools.add_parser("pipe-flow", help="pressure drop of water in a straight pipe")
    pipe.add_argument("--flow-l-s", type=float, required=True, help="flow rate [L/s]")
    pipe.add_argument("--diameter-mm", type=float, required=True, help="inner diameter [mm]")
    pipe.add_argument("--length-m", type=float, required=True, help="pipe length [m]")
    pipe.add_argument(
        "--roughness-mm",
        type=float,
        default=0.045,
        help="absolute roughness [mm] (default 0.045, commercial steel)",
    )
    pipe.add_argument(
        "--temperature-c", type=float, default=20.0, help="water temperature [degC] (default 20)"
    )
    # set_defaults(handler=...) attaches the function that runs this tool.
    pipe.set_defaults(handler=_pipe_flow)

    water = tools.add_parser("water", help="liquid water properties at 1 atm")
    water.add_argument(
        "--temperature-c", type=float, required=True, help="temperature [degC], 1 to 99"
    )
    water.set_defaults(handler=_water)
    return parser

main(argv=None)

Entry point for the fpc command.

Parameters:

Name Type Description Default
argv

list of arguments (defaults to the real command line); tests pass a list so they do not need a subprocess

None
Source code in fpc/cli.py
90
91
92
93
94
95
96
97
def main(argv=None):
    """Entry point for the ``fpc`` command.

    :param argv: list of arguments (defaults to the real command line);
        tests pass a list so they do not need a subprocess
    """
    args = build_parser().parse_args(argv)
    args.handler(args)