Replace Click with Typer for improved CLI argument handling

This commit is contained in:
Esa Kataja
2025-04-17 12:36:31 +03:00
parent 55cd3e7ec0
commit a30791a036
3 changed files with 41 additions and 13 deletions
+1
View File
@@ -10,4 +10,5 @@ dependencies = [
"python-dotenv>=1.0.1", "python-dotenv>=1.0.1",
"requests>=2.32.3", "requests>=2.32.3",
"rich>=13.9.4", "rich>=13.9.4",
"typer>=0.15.2",
] ]
+12 -12
View File
@@ -1,13 +1,13 @@
import json import json
import subprocess import subprocess
import click import typer
from pathlib import Path from pathlib import Path
# from rich import print
from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream
from movie_details import get_movie_details from movie_details import get_movie_details
from lib.fs import write_shell_script from lib.fs import write_shell_script
app = typer.Typer()
def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile: def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile:
for stream in stream_data["streams"]: for stream in stream_data["streams"]:
@@ -23,15 +23,15 @@ def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile:
return VideoFile return VideoFile
@click.command() @app.command()
@click.argument("filename", type=click.Path(exists=True)) def main(
@click.option("-i", "--imdb", type=str, help="IMDB ID") filename: Path = typer.Argument(..., exists=True, help="Input video file"),
@click.option("-ns", "--nosubtitles", is_flag=True, help="No subtitles") imdb: str = typer.Option(None, "-i", "--imdb", help="IMDB ID"),
@click.option("-p", "--preset", type=int, default=2, help="Encoding preset (default: 2)") nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"),
@click.option("-c", "--crf", type=int, default=30, help="CRF value (default: 30)") preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"),
@click.option('-o', '--original-media-type', default='DVD', help='Set the ORIGINAL_MEDIA_TYPE metadata') crf: int = typer.Option(30, "-c", "--crf", help="CRF value"),
def main(filename: Path, imdb: str, nosubtitles: bool, preset: int, crf: int, original_media_type: str): original_media_type: str = typer.Option("DVD", "-o", "--original-media-type", help="Set the ORIGINAL_MEDIA_TYPE metadata")
"""Run ffprobe on a file and print the output in JSON format.""" ):
video_details = VideoFile( video_details = VideoFile(
imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles
) )
@@ -62,4 +62,4 @@ def main(filename: Path, imdb: str, nosubtitles: bool, preset: int, crf: int, or
if __name__ == "__main__": if __name__ == "__main__":
main() app()
Generated
+28 -1
View File
@@ -1,4 +1,5 @@
version = 1 version = 1
revision = 1
requires-python = ">=3.12" requires-python = ">=3.12"
resolution-markers = [ resolution-markers = [
"python_full_version < '3.13'", "python_full_version < '3.13'",
@@ -67,7 +68,7 @@ name = "click"
version = "8.1.7" version = "8.1.7"
source = { registry = "https://pypi.org/simple" } source = { registry = "https://pypi.org/simple" }
dependencies = [ dependencies = [
{ name = "colorama", marker = "platform_system == 'Windows'" }, { name = "colorama", marker = "sys_platform == 'win32'" },
] ]
sdist = { url = "https://files.pythonhosted.org/packages/96/d3/f04c7bfcf5c1862a2a5b845c6b2b360488cf47af55dfa79c98f6a6bf98b5/click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de", size = 336121 } sdist = { url = "https://files.pythonhosted.org/packages/96/d3/f04c7bfcf5c1862a2a5b845c6b2b360488cf47af55dfa79c98f6a6bf98b5/click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de", size = 336121 }
wheels = [ wheels = [
@@ -208,6 +209,30 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/19/71/39c7c0d87f8d4e6c020a393182060eaefeeae6c01dab6a84ec346f2567df/rich-13.9.4-py3-none-any.whl", hash = "sha256:6049d5e6ec054bf2779ab3358186963bac2ea89175919d699e378b99738c2a90", size = 242424 }, { url = "https://files.pythonhosted.org/packages/19/71/39c7c0d87f8d4e6c020a393182060eaefeeae6c01dab6a84ec346f2567df/rich-13.9.4-py3-none-any.whl", hash = "sha256:6049d5e6ec054bf2779ab3358186963bac2ea89175919d699e378b99738c2a90", size = 242424 },
] ]
[[package]]
name = "shellingham"
version = "1.5.4"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de", size = 10310 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686", size = 9755 },
]
[[package]]
name = "typer"
version = "0.15.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "click" },
{ name = "rich" },
{ name = "shellingham" },
{ name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/8b/6f/3991f0f1c7fcb2df31aef28e0594d8d54b05393a0e4e34c65e475c2a5d41/typer-0.15.2.tar.gz", hash = "sha256:ab2fab47533a813c49fe1f16b1a370fd5819099c00b119e0633df65f22144ba5", size = 100711 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/7f/fc/5b29fea8cee020515ca82cc68e3b8e1e34bb19a3535ad854cac9257b414c/typer-0.15.2-py3-none-any.whl", hash = "sha256:46a499c6107d645a9c13f7ee46c5d5096cae6f5fc57dd11eccbbb9ae3e44ddfc", size = 45061 },
]
[[package]] [[package]]
name = "typing-extensions" name = "typing-extensions"
version = "4.12.2" version = "4.12.2"
@@ -236,6 +261,7 @@ dependencies = [
{ name = "python-dotenv" }, { name = "python-dotenv" },
{ name = "requests" }, { name = "requests" },
{ name = "rich" }, { name = "rich" },
{ name = "typer" },
] ]
[package.metadata] [package.metadata]
@@ -245,4 +271,5 @@ requires-dist = [
{ name = "python-dotenv", specifier = ">=1.0.1" }, { name = "python-dotenv", specifier = ">=1.0.1" },
{ name = "requests", specifier = ">=2.32.3" }, { name = "requests", specifier = ">=2.32.3" },
{ name = "rich", specifier = ">=13.9.4" }, { name = "rich", specifier = ">=13.9.4" },
{ name = "typer", specifier = ">=0.15.2" },
] ]