Argparse layer for the comicbox CLI.
Config-tree args name their dest after the confuse template path they set (general.config, read.except, …). set_args(dots=True) splits those dots into the nested config tree, so the parser and the template share one string instead of a hand-maintained reshaping table.
Online runtime args (--online, --id, --match, …) are the exception: they stay flat and are consumed by runtime_online_inputs / build_online_settings directly, which need typed per-flag errors and dynamically keyed maps that a fixed template can’t express.
CSVAction
Bases: Action
Parse comma delimited sequences.
Source code in comicbox/cli/parser.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 | class CSVAction(Action):
"""Parse comma delimited sequences."""
@override
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
"""Parse comma delimited sequences."""
if isinstance(values, str):
values_array = [v.strip() for v in values.split(",") if v.strip()]
elif isinstance(values, Sequence):
values_array = list(values)
else:
return
setattr(namespace, self.dest, values_array)
|
__call__(parser, namespace, values, option_string=None)
Parse comma delimited sequences.
Source code in comicbox/cli/parser.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 | @override
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
"""Parse comma delimited sequences."""
if isinstance(values, str):
values_array = [v.strip() for v in values.split(",") if v.strip()]
elif isinstance(values, Sequence):
values_array = list(values)
else:
return
setattr(namespace, self.dest, values_array)
|
LazyEpilog
Stand-in for the rich epilog that builds it only when help renders.
argparse asks for the epilog in format_help() and nowhere else, but build_epilog() was called eagerly in build_parser() — so every comicbox invocation imported comicbox.cli.epilog (and through it the whole format registry) and assembled seven rich Tables that almost no run ever prints. Rich resolves any object exposing __rich__ at render time, so the parser can hold this instead.
Source code in comicbox/cli/parser.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | class LazyEpilog:
"""
Stand-in for the rich epilog that builds it only when help renders.
argparse asks for the epilog in `format_help()` and nowhere else, but
`build_epilog()` was called eagerly in `build_parser()` — so every
`comicbox` invocation imported `comicbox.cli.epilog` (and through it
the whole format registry) and assembled seven rich Tables that
almost no run ever prints. Rich resolves any object exposing
`__rich__` at render time, so the parser can hold this instead.
"""
def __rich__(self) -> "Group":
"""Assemble the real epilog. Called by rich while rendering help."""
from comicbox.cli.epilog import build_epilog
return build_epilog()
|
__rich__()
Assemble the real epilog. Called by rich while rendering help.
Source code in comicbox/cli/parser.py
| def __rich__(self) -> "Group":
"""Assemble the real epilog. Called by rich while rendering help."""
from comicbox.cli.epilog import build_epilog
return build_epilog()
|
Bases: Action
Parse page range delimited by :.
Source code in comicbox/cli/parser.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 | class PageRangeAction(Action):
"""Parse page range delimited by ``:``."""
@override
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
"""Parse page range delimited by :."""
if isinstance(values, str):
values = values.split(":")
if not values:
return
index_from = int(values[0]) if len(values[0]) else None
if len(values) == 1:
index_to = index_from
elif len(values[1]):
index_to = int(values[1])
else:
index_to = None
# Dotted keys, matching the confuse template path directly. This
# action's own dest stays None and is dropped by confuse.
if index_from is not None:
setattr(namespace, "convert.extract_pages_from", index_from)
if index_to is not None:
setattr(namespace, "convert.extract_pages_to", index_to)
|
Parse page range delimited by :.
Source code in comicbox/cli/parser.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 | @override
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
"""Parse page range delimited by :."""
if isinstance(values, str):
values = values.split(":")
if not values:
return
index_from = int(values[0]) if len(values[0]) else None
if len(values) == 1:
index_to = index_from
elif len(values[1]):
index_to = int(values[1])
else:
index_to = None
# Dotted keys, matching the confuse template path directly. This
# action's own dest stays None and is dropped by confuse.
if index_from is not None:
setattr(namespace, "convert.extract_pages_from", index_from)
if index_to is not None:
setattr(namespace, "convert.extract_pages_to", index_to)
|
RichArgumentParser
Bases: ArgumentParser
An ArgumentParser whose help is rich by default.
Its subcommands’ parsers are built from the same class, so they are rich too without each naming the formatter.
Source code in comicbox/cli/parser.py
89
90
91
92
93
94
95
96
97
98
99
100 | class RichArgumentParser(ArgumentParser):
"""
An ArgumentParser whose help is rich by default.
Its subcommands' parsers are built from the same class, so they are
rich too without each naming the formatter.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Default the formatter to rich."""
kwargs.setdefault("formatter_class", RichHelpFormatter)
super().__init__(*args, **kwargs)
|
__init__(*args, **kwargs)
Default the formatter to rich.
Source code in comicbox/cli/parser.py
| def __init__(self, *args: Any, **kwargs: Any) -> None:
"""Default the formatter to rich."""
kwargs.setdefault("formatter_class", RichHelpFormatter)
super().__init__(*args, **kwargs)
|
add_online_auth_group(parser, sources=('metron', 'comicvine', 'versedb'))
Add --auth, its examples naming sources; the reading-list CLI shares it.
Source code in comicbox/cli/parser.py
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588 | def add_online_auth_group(
parser: ArgumentParser,
sources: Sequence[str] = ("metron", "comicvine", "versedb"),
) -> None:
"""Add ``--auth``, its examples naming ``sources``; the reading-list CLI shares it."""
examples = ", ".join(f"[green]--auth {source}:TOKEN[/green]" for source in sources)
group = parser.add_argument_group("online: auth")
group.add_argument(
"-a",
"--auth",
action="append",
default=None,
metavar="SRC:TOKEN",
dest="auth",
help=(
f"Per-source API token. Repeatable. Examples: {examples}. "
"[green]comicvine:url=URL[/green] overrides the ComicVine API "
"endpoint. A token on the command line lands in shell history; "
"use the [cyan]COMICBOX_ONLINE__AUTH__<SOURCE>__KEY[/cyan] "
"env vars where possible."
),
)
|
add_online_cache_group(parser)
Add the online cache options; the reading-list CLI shares them.
Source code in comicbox/cli/parser.py
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623 | def add_online_cache_group(parser: ArgumentParser) -> None:
"""Add the online cache options; the reading-list CLI shares them."""
group = parser.add_argument_group("online: cache")
group.add_argument(
"--cache",
action="store",
default=None,
choices=tuple(mode.value for mode in CacheMode),
metavar="MODE",
dest="cache",
help=(
"Cache mode for online responses: "
"[green]on[/green] (default), [green]off[/green] (no read or write), "
"[green]refresh[/green] (skip reads, write fresh results)."
),
)
group.add_argument(
"--cache-dir",
default=None,
metavar="PATH",
dest="cache_dir",
help="Override the on-disk cache directory for online responses.",
)
group.add_argument(
"--cache-ttl",
default=None,
metavar="DURATION",
dest="cache_ttl",
help=(
"Cache entry TTL ([green]7d[/green], [green]24h[/green], [green]60m[/green],"
" [green]0[/green] for no expiry)."
),
)
|
build_parser(*, prog=None)
Assemble the full comicbox argument parser from the group builders.
prog overrides the program name in usage lines; comicbox doctor passes its own so its --help doesn’t claim to be a normal run.
Source code in comicbox/cli/parser.py
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 | def build_parser(*, prog: str | None = None) -> ArgumentParser:
"""
Assemble the full comicbox argument parser from the group builders.
``prog`` overrides the program name in usage lines; ``comicbox doctor``
passes its own so its ``--help`` doesn't claim to be a normal run.
"""
description = "Comic book archive multi format metadata read/write/transform tool and image extractor."
if not PDF_ENABLED:
description += "\n[yellow]Comicbox is not installed with PDF support.[/yellow]"
parser = RichArgumentParser(
prog=prog,
description=description,
epilog=LazyEpilog(),
add_help=False,
)
_add_general_group(parser)
_add_read_group(parser)
_add_write_group(parser)
_add_print_group(parser)
_add_convert_group(parser)
_add_online_lookup_group(parser)
add_online_auth_group(parser)
add_online_cache_group(parser)
_add_online_tuning_group(parser)
_add_target_group(parser)
parser.add_argument(
"-h", "--help", action="help", help="Show this help message and exit."
)
return parser
|