Skip to content

Commit

Permalink
[config,cli] Updated CL options of config options
Browse files Browse the repository at this point in the history
- Change: Updated default value and validation of CL options with corresponding config options.
  - CL options with corresponding config options now have default values of `None`.
  - `None` will later be replaced with config option value.
  • Loading branch information
AnonymouX47 committed Aug 24, 2022
1 parent 5f3f988 commit b10a594
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
16 changes: 12 additions & 4 deletions src/term_image/cli.py
Expand Up @@ -589,10 +589,12 @@ def check_arg(
if force_cli_mode:
args.cli = True

# `check_arg()` requires logging.
init_log(
(
args.log_file
if config_options["log file"].is_valid(args.log_file)
# If the argument is invalid, the error will be emitted later.
if args.log_file and config_options["log file"].is_valid(args.log_file)
else config_options.log_file
),
getattr(_logging, args.log_level),
Expand Down Expand Up @@ -621,12 +623,18 @@ def check_arg(

for name, option in config_options.items():
var_name = name.replace(" ", "_")
value = getattr(args, var_name, None)
try:
arg_value = getattr(args, var_name)
# Not all config options have corresponding command-line arguments
if value is not None and not option.is_valid(value):
except AttributeError:
continue

if arg_value is None:
setattr(args, var_name, option.value)
elif not option.is_valid(arg_value):
arg_name = f"--{name.replace(' ', '-')}"
notify.notify(
f"{arg_name}: {option.error_msg} (got: {value!r})",
f"{arg_name}: {option.error_msg} (got: {arg_value!r})",
level=notify.ERROR,
)
notify.notify(
Expand Down
13 changes: 0 additions & 13 deletions src/term_image/parsers.py
Expand Up @@ -98,7 +98,6 @@
"--query-timeout",
type=float,
metavar="N",
default=config_options.query_timeout,
help=(
"Timeout (in seconds) for all terminal queries "
f"(default: {config_options.query_timeout})"
Expand All @@ -108,7 +107,6 @@
"-S",
"--style",
choices=("auto", "block", "iterm2", "kitty"),
default=config_options.style,
help=(
f"Image render style (default: {config_options.style}). "
'See "Render Styles" below'
Expand All @@ -129,7 +127,6 @@
"--font-ratio",
type=float,
metavar="N",
default=config_options.font_ratio,
help=(
"The width-to-height ratio of a character cell in the terminal, to "
f"preserve image aspect ratio (default: {config_options.font_ratio or 'auto'})"
Expand All @@ -145,7 +142,6 @@
win_size_options.add_argument(
"--swap-win-size",
action="store_true",
default=config_options.swap_win_size,
help=(
"A workaround for 'auto font ratio' on some terminal emulators (e.g older "
"VTE-based ones) that wrongly report window dimensions swapped"
Expand All @@ -154,7 +150,6 @@
win_size_options.add_argument(
"--no-swap-win-size",
action="store_false",
default=config_options.swap_win_size,
dest="swap_win_size",
help="Unlike '--swap-win-size', use the reported window size as-is (default)",
)
Expand Down Expand Up @@ -202,7 +197,6 @@
anim_cache_options.add_argument(
"--anim-cache",
type=int,
default=config_options.anim_cache,
metavar="N",
help=(
"Maximum frame count for animation frames to be cached (Better performance "
Expand Down Expand Up @@ -460,7 +454,6 @@
"--max-pixels",
type=int,
metavar="N",
default=config_options.max_pixels,
help=(
"Maximum amount of pixels in images to be displayed "
f"(default: {config_options.max_pixels}) [4]"
Expand All @@ -471,7 +464,6 @@
"--checkers",
type=int,
metavar="N",
default=config_options.checkers,
help=(
"Maximum number of sub-processes for checking directory sources "
f"(default: {config_options.checkers})"
Expand All @@ -481,7 +473,6 @@
"--getters",
type=int,
metavar="N",
default=config_options.getters,
help=(
"Number of threads for downloading images from URL sources "
f"(default: {config_options.getters})"
Expand All @@ -491,7 +482,6 @@
"--grid-renderers",
type=int,
metavar="N",
default=config_options.grid_renderers,
help=(
"Number of subprocesses for rendering grid cells "
f"(default: {config_options.grid_renderers})"
Expand All @@ -502,14 +492,12 @@
multi_options.add_argument(
"--multi",
action="store_false",
default=config_options.no_multi,
dest="no_multi",
help="Enable multiprocessing, if supported (default)",
)
multi_options.add_argument(
"--no-multi",
action="store_true",
default=config_options.no_multi,
help="Disable multiprocessing",
)

Expand All @@ -524,7 +512,6 @@
"-l",
"--log-file",
metavar="FILE",
default=config_options.log_file,
help=f"The file to write logs to (default: {config_options.log_file})",
)
log_options.add_argument(
Expand Down

0 comments on commit b10a594

Please sign in to comment.