Preprocess arguments to fix leading minus issue.

`argparse` treats argument of `--coordinates` and `--boundary-box` as
other option if it starts with `-`.  The new solution is to preprocess
arguments adding leading space to the argument value.
This commit is contained in:
Sergey Vartanov 2022-10-30 11:48:11 +04:00
parent 54f6b17fdc
commit eba1ed1606
2 changed files with 16 additions and 19 deletions

View file

@ -9,7 +9,7 @@ from map_machine.osm.osm_reader import Tags, OSMNode
def draw_node(tags: Tags, path: Path):
"""Draw separate node."""
grid: Grid = Grid(show_credit=False, margin=3.5)
grid: Grid = Grid(show_credit=False, margin=7.5)
grid.add_node(tags, 0, 0)
grid.draw(path)

View file

@ -31,21 +31,20 @@ COMMANDS: list[str] = [
"taginfo",
]
BOUNDARY_BOX_WARNING: str = (
"if the first value is negative, use the `=` sign or enclose the value "
"with quotes and prefix a space before `-`, e.g. "
"`-b=-84.752,39.504,-84.749,39.508` or `-b "
'" -84.752,39.504,-84.749,39.508"`'
)
COORDINATES_WARNING: str = (
"if the first value is negative, use the `=` sign or enclose the value "
"with quotes and prefix a space before `-`, e.g. `-c=-84.752,39.504` or "
'`-c " -84.752,39.504"`'
)
def parse_arguments(args: list[str]) -> argparse.Namespace:
"""Parse Map Machine command-line arguments."""
# Preparse arguments adding space before coordinates and boundary box if the
# first value is negative. In that case `argparse` interprets in as an
# option name.
for argument in "-c", "--coordinates", "-b", "--boundary-box":
if argument in args:
index: int = args.index(argument) + 1
if args[index].startswith("-"):
args[index] = " " + args[index]
break
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description="Map Machine. OpenStreetMap renderer with custom icon set"
)
@ -217,8 +216,7 @@ def add_tile_arguments(parser: argparse.ArgumentParser) -> None:
"-c",
"--coordinates",
metavar="<latitude>,<longitude>",
help="coordinates of any location inside the tile; "
+ COORDINATES_WARNING,
help="coordinates of any location inside the tile",
)
parser.add_argument(
"-t",
@ -236,7 +234,7 @@ def add_tile_arguments(parser: argparse.ArgumentParser) -> None:
"-b",
"--boundary-box",
help="construct the minimum amount of tiles that cover the requested "
"boundary box; " + BOUNDARY_BOX_WARNING,
"boundary box",
metavar="<lon1>,<lat1>,<lon2>,<lat2>",
)
parser.add_argument(
@ -305,7 +303,7 @@ def add_render_arguments(parser: argparse.ArgumentParser) -> None:
"-b",
"--boundary-box",
metavar="<lon1>,<lat1>,<lon2>,<lat2>",
help="geo boundary box; " + BOUNDARY_BOX_WARNING,
help="geo boundary box",
)
parser.add_argument(
"--cache",
@ -325,8 +323,7 @@ def add_render_arguments(parser: argparse.ArgumentParser) -> None:
"-c",
"--coordinates",
metavar="<latitude>,<longitude>",
help="coordinates of any location inside the tile; "
+ COORDINATES_WARNING,
help="coordinates of any location inside the tile",
)
parser.add_argument(
"-s",