Skip to content

Commit df36d19

Browse files
committed
feat: enhance CLI with script listing and version management
## CHANGES - Add version information in dedicated module - Implement script listing functionality with `--list` flag - Make script argument optional with `nargs="?"` - Improve script path resolution with auto-detection - Organize CLI arguments into logical groups - Add `--version` flag to display package version - Update version management to use dynamic versioning - Modify script config retrieval to support filename-only option - Remove unused import in webui components
1 parent 543b1ee commit df36d19

File tree

5 files changed

+71
-14
lines changed

5 files changed

+71
-14
lines changed

binaural_generator/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Version information for binaural_generator package."
2+
3+
__version__ = "0.6.2"

binaural_generator/cli.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import argparse
44
import logging
55
import multiprocessing
6+
import os
7+
from pathlib import Path
68
import sys
79
import time
810
from dataclasses import dataclass
@@ -22,33 +24,53 @@
2224
generate_audio_sequence,
2325
save_audio_file,
2426
)
25-
from binaural_generator.core.utils import load_yaml_config
27+
from binaural_generator.core.utils import get_all_script_configs, load_yaml_config
28+
from binaural_generator import __version__
2629

2730

2831
def parse_args() -> argparse.Namespace:
2932
"""Parse command line arguments."""
3033
parser = argparse.ArgumentParser(
31-
description="Generate binaural beats audio (WAV or FLAC) from a YAML script."
34+
description="Generate binaural beats audio from a YAML script."
3235
)
33-
parser.add_argument("script", help="Path to YAML configuration script.")
34-
parser.add_argument(
36+
cmd_group = parser.add_argument_group(
37+
title="Audio Generation Options",
38+
description="Options for generating audio from a YAML script.",
39+
)
40+
cmd_group.add_argument(
3541
"-o", "--output", help="Output audio file path (overrides YAML setting)."
3642
)
37-
parser.add_argument(
43+
cmd_group.add_argument(
3844
"-v", "--verbose", action="store_true", help="Enable verbose logging."
3945
)
40-
parser.add_argument(
46+
cmd_group.add_argument(
4147
"-p",
4248
"--parallel",
4349
action="store_true",
4450
help="Use parallel processing for faster audio generation.",
4551
)
46-
parser.add_argument(
52+
cmd_group.add_argument(
4753
"--threads",
4854
type=int,
4955
help="Number of threads to use for parallel processing. Defaults to CPU count.",
5056
default=multiprocessing.cpu_count(),
5157
)
58+
cmd_group.add_argument(
59+
"script", help="Path to YAML configuration script.", nargs="?"
60+
)
61+
62+
info_group = parser.add_argument_group(
63+
title="Information Options",
64+
description="Options for displaying information about the script.",
65+
)
66+
67+
list_parser_group = info_group.add_mutually_exclusive_group()
68+
list_parser_group.add_argument(
69+
"--version", action="version", version=f"binaural-generator {__version__}"
70+
)
71+
list_parser_group.add_argument(
72+
"-l", "--list", action="store_true", help="List available scripts."
73+
)
5274
return parser.parse_args()
5375

5476

@@ -159,9 +181,37 @@ def main() -> None:
159181
configure_logging(args.verbose)
160182
logger = logging.getLogger(__name__)
161183

184+
script_dir = Path(__file__).parent.absolute()
185+
186+
# Determine script path
187+
script_path = args.script
188+
if script_path and not os.path.exists(args.script):
189+
# If not found and no path separators in the name, check in scripts directory
190+
if os.path.basename(args.script) == args.script:
191+
potential_script_path = script_dir / "scripts" / args.script
192+
if os.path.exists(potential_script_path):
193+
script_path = potential_script_path
194+
logger.info("Using script from scripts directory: %s", script_path)
195+
196+
if args.list:
197+
os.chdir(script_dir)
198+
all_scripts = get_all_script_configs(file_names_only=True)
199+
print(
200+
f"Available scripts: in {script_dir / 'scripts'}\n\n"
201+
+ "\n".join(
202+
sorted([f" {title}: {path}" for title, path in all_scripts.items()])
203+
)
204+
)
205+
print("\nUse --script <script_name.yaml> to run a specific script.")
206+
sys.exit(0)
207+
208+
if not script_path:
209+
logger.error("No script specified. Use --script <script_name.yaml>.")
210+
sys.exit(1)
211+
162212
try:
163213
# Load YAML configuration using the utility function
164-
config = load_yaml_config(args.script)
214+
config = load_yaml_config(script_path)
165215

166216
# Extract global settings, providing defaults if they are missing
167217
sample_rate = config.get("sample_rate", DEFAULT_SAMPLE_RATE)

binaural_generator/core/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def get_yaml_title(path: str) -> Optional[str]:
120120
return None
121121

122122

123-
def get_all_script_configs(scripts_dir: str = "scripts") -> dict[str, str]:
123+
def get_all_script_configs(
124+
scripts_dir: str = "scripts", file_names_only=False
125+
) -> dict[str, str]:
124126
"""Scan a directory for YAML files and return a mapping of titles to file paths.
125127
126128
Args:
@@ -134,7 +136,8 @@ def get_all_script_configs(scripts_dir: str = "scripts") -> dict[str, str]:
134136
# Find all YAML files in the scripts directory
135137
for yaml_path in glob.glob(os.path.join(scripts_dir, "*.yaml")):
136138
title = get_yaml_title(yaml_path)
137-
if title:
138-
config_files[title] = yaml_path
139+
k = title
140+
v = yaml_path if not file_names_only else os.path.basename(yaml_path)
141+
config_files[k] = v
139142

140143
return config_files

binaural_generator/webui/components/config_utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import streamlit as st
66
import yaml
77

8-
from binaural_generator.core.utils import get_all_script_configs
9-
108

119
def load_config_file(file_path: str) -> dict[str, Any]:
1210
"""Load a YAML configuration file."""

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "binaural-generator"
3-
version = "0.6.1"
3+
dynamic = ["version"]
44
description = "Generate binaural beat audio with background noise for brainwave entrainment"
55
authors = [{ name = "Kayvan Sylvan", email = "kayvan@sylvan.com" }]
66
license = "MIT"
@@ -45,6 +45,9 @@ dev = [
4545
"twine>=6.1.0",
4646
]
4747

48+
[tool.setuptools.dynamic]
49+
version = { attr = "binaural_generator.__version__" }
50+
4851
[tool.setuptools.packages.find]
4952
exclude = ["audio"]
5053

0 commit comments

Comments
 (0)