Parameter Types
This page maps Python literal types to their fargv equivalents, and lists fargv parameter types that have no Python literal counterpart.
Mapping table
Python literal |
Example |
|
Legacy dict default |
OO CLI syntax |
Notes |
|---|---|---|---|---|---|
|
|
|
|
|
Must be checked before |
|
|
|
|
|
Set |
|
|
|
|
|
Accepts scientific notation ( |
|
|
|
|
|
Supports |
|
|
|
|
|
First element is the default; 2-item tuple is |
|
|
|
|
|
Collects unmatched tokens; no |
|
|
|
|
|
Same as |
|
|
|
— |
|
Git-style subcommand; flag style |
(none) |
— |
|
— |
|
Defaults to |
(none) |
— |
|
— |
|
Defaults to |
(none) |
— |
|
— |
|
Base class; use |
(none) |
— |
|
— |
|
Returns |
(none) |
— |
|
— |
|
|
(none) |
— |
|
— |
|
|
(none) |
— |
|
— |
|
|
|
(function param) |
|
— |
|
Parsed via |
(none) |
— |
|
— |
|
|
Notes
Two-element tuple (default, "description")
When a tuple has exactly two elements and the second is a str, fargv
interprets it as (default_value, "help text") — not as a choice. Use
three or more elements for a choice parameter:
# WRONG — treated as (default="fast", description="slow"):
{"mode": ("fast", "slow")}
# CORRECT — three-element choice:
{"mode": ("fast", "slow", "medium")}
# Attaching help text to a non-choice:
{"lr": (0.001, "Learning rate")}
FargvStr — cross-parameter interpolation
String parameters support {key} references to other string parameters.
Resolution is lazy (evaluated when .value is read) and recursive:
from fargv import parse
p, _ = parse({"base": "/tmp/run", "out": "{base}/predictions"})
# --base=/data → p.out == "/data/predictions"
FargvChoice
The first element of the list / tuple is the default. All elements must be
strings. Passing a value not in the list raises FargvError.
from fargv.parameters import FargvChoice
FargvChoice(["adam", "sgd", "rmsprop"], name="opt")
# --opt=sgd → "sgd"
# --opt=lion → FargvError
FargvPositional
At most one positional parameter is supported per parser. All tokens that do not start with the long or short prefix are routed to it.
from fargv import parse
p, _ = parse({"files": [], "count": 0})
# prog --count=2 a.txt b.txt → p.files == ["a.txt", "b.txt"]
FargvSubcommand — nested sub-parsers
Mirrors git-style subcommands. The value is always a dict:
{"name": "<subcommand>", "result": {<sub-namespace>}}.
With subcommand_return_type="flat" (default), the result is merged into the
top-level namespace and the subcommand key holds the selected name:
from fargv import parse
p, _ = parse(
{"cmd": {"train": {"lr": 0.01}, "eval": {"dataset": "val"}}},
given_parameters=["prog", "train", "--lr=0.5"],
)
# p.cmd == "train", p.lr == 0.5
FargvStream — text I/O streams
Accepts a file path, or the keywords stdin / stdout / stderr.
The stream mode (r / w / a) is determined by the default value:
sys.stdin → r, sys.stdout/sys.stderr → w.
from fargv.parameters import FargvOutputStream
out = FargvOutputStream(name="out")
# --out=results.txt → open("results.txt", "w")
# --out=stdout → sys.stdout
FargvPath hierarchy
FargvPath (pathlib.Path, no validation)
├── FargvExistingFile (must_exist=True)
├── FargvNonExistingFile (must_not_exist=True)
└── FargvFile (parent_must_exist=True)
FargvTuple — typed fixed-length tuples
Parsed from a single string using ast.literal_eval.
Single-element shorthand: "224" → (224,) for element_types=(int,).
Optional tuples: "()" → None when optional=True.
from fargv.parameters import FargvTuple
p = FargvTuple((int, int), name="size")
p.ingest_value_strings("(640, 480)")
# p.value == (640, 480)
Type annotations in function signatures are also supported:
from typing import Tuple, Optional
from fargv import parse
def train(lr: float = 0.01, size: Tuple[int, int] = (224, 224)):
...
p, _ = parse(train)
FargvInt as a count switch
Set is_count_switch=True to count repeated short flags:
from fargv.parameters import FargvInt
v = FargvInt(0, name="verbosity", short_name="v", is_count_switch=True)
# -vvvv → 4
# --verbosity=3 → 3