audiobook_forge/cli/
commands.rs

1//! CLI commands and arguments
2
3use clap::{Parser, Subcommand, Args};
4use std::path::PathBuf;
5
6use crate::VERSION;
7
8/// Audiobook Forge - Convert audiobook directories to M4B format
9#[derive(Parser)]
10#[command(name = "audiobook-forge")]
11#[command(version = VERSION)]
12#[command(about = "Convert audiobook directories to M4B format with chapters and metadata")]
13#[command(long_about = "
14Audiobook Forge is a CLI tool that converts audiobook directories containing
15MP3 files into high-quality M4B audiobook files with proper chapters and metadata.
16
17Features:
18• Automatic quality detection and preservation
19• Smart chapter generation from multiple sources
20• Parallel batch processing
21• Metadata extraction and enhancement
22• Cover art embedding
23")]
24pub struct Cli {
25    #[command(subcommand)]
26    pub command: Commands,
27
28    /// Enable verbose output
29    #[arg(global = true, short, long)]
30    pub verbose: bool,
31}
32
33#[derive(Subcommand)]
34pub enum Commands {
35    /// Process audiobooks and convert to M4B
36    Build(BuildArgs),
37
38    /// Organize audiobooks into M4B and To_Convert folders
39    Organize(OrganizeArgs),
40
41    /// Manage configuration
42    #[command(subcommand)]
43    Config(ConfigCommands),
44
45    /// Fetch and manage Audible metadata
46    #[command(subcommand)]
47    Metadata(MetadataCommands),
48
49    /// Interactive metadata matching for M4B files
50    Match(MatchArgs),
51
52    /// Check system dependencies
53    Check,
54
55    /// Show version information
56    Version,
57}
58
59#[derive(Args)]
60pub struct BuildArgs {
61    /// Root directory containing audiobook folders
62    #[arg(short, long)]
63    pub root: Option<PathBuf>,
64
65    /// Output directory (defaults to same as root)
66    #[arg(short, long)]
67    pub out: Option<PathBuf>,
68
69    /// Number of parallel workers (1-8)
70    #[arg(short = 'j', long, value_parser = clap::value_parser!(u8).range(1..=8))]
71    pub parallel: Option<u8>,
72
73    /// Skip folders with existing M4B files
74    #[arg(long)]
75    pub skip_existing: Option<bool>,
76
77    /// Force reprocessing (overwrite existing)
78    #[arg(long)]
79    pub force: bool,
80
81    /// Normalize existing M4B files (fix metadata)
82    #[arg(long)]
83    pub normalize: bool,
84
85    /// Dry run (analyze without creating files)
86    #[arg(long)]
87    pub dry_run: bool,
88
89    /// Prefer stereo over mono
90    #[arg(long)]
91    pub prefer_stereo: Option<bool>,
92
93    /// Chapter source priority
94    #[arg(long, value_parser = ["auto", "files", "cue", "id3", "none"])]
95    pub chapter_source: Option<String>,
96
97    /// Cover art filenames (comma-separated)
98    #[arg(long)]
99    pub cover_names: Option<String>,
100
101    /// Default language for metadata
102    #[arg(long)]
103    pub language: Option<String>,
104
105    /// Keep temporary files for debugging
106    #[arg(long)]
107    pub keep_temp: bool,
108
109    /// Delete original files after conversion
110    #[arg(long)]
111    pub delete_originals: bool,
112
113    /// Quality preset for output audio
114    #[arg(long, value_parser = ["low", "medium", "high", "ultra", "maximum", "source"])]
115    pub quality: Option<String>,
116
117    /// AAC encoder to use (auto, aac_at, libfdk_aac, aac)
118    #[arg(long)]
119    pub aac_encoder: Option<String>,
120
121    /// DEPRECATED: Use --aac-encoder instead
122    #[arg(long, hide = true)]
123    pub use_apple_silicon_encoder: Option<bool>,
124
125    /// Fetch metadata from Audible during build
126    #[arg(long)]
127    pub fetch_audible: bool,
128
129    /// Audible region (us, uk, ca, au, fr, de, jp, it, in, es)
130    #[arg(long)]
131    pub audible_region: Option<String>,
132
133    /// Auto-match books with Audible by folder name
134    #[arg(long)]
135    pub audible_auto_match: bool,
136
137    /// Configuration file path
138    #[arg(long)]
139    pub config: Option<PathBuf>,
140}
141
142#[derive(Args)]
143pub struct OrganizeArgs {
144    /// Root directory to organize
145    #[arg(short, long)]
146    pub root: Option<PathBuf>,
147
148    /// Dry run (show what would be done)
149    #[arg(long)]
150    pub dry_run: bool,
151
152    /// Configuration file path
153    #[arg(long)]
154    pub config: Option<PathBuf>,
155}
156
157#[derive(Subcommand)]
158pub enum ConfigCommands {
159    /// Initialize config file with defaults
160    Init {
161        /// Overwrite existing config file
162        #[arg(long)]
163        force: bool,
164    },
165
166    /// Show current configuration
167    Show {
168        /// Configuration file path
169        #[arg(long)]
170        config: Option<PathBuf>,
171    },
172
173    /// Validate configuration file
174    Validate {
175        /// Configuration file path
176        #[arg(long)]
177        config: Option<PathBuf>,
178    },
179
180    /// Show config file path
181    Path,
182
183    /// Edit config file in default editor
184    Edit,
185}
186
187#[derive(Subcommand)]
188pub enum MetadataCommands {
189    /// Fetch metadata from Audible
190    Fetch {
191        /// Audible ASIN (B002V5D7RU format)
192        #[arg(long)]
193        asin: Option<String>,
194
195        /// Search by title
196        #[arg(long)]
197        title: Option<String>,
198
199        /// Search by author
200        #[arg(long)]
201        author: Option<String>,
202
203        /// Audible region (us, uk, ca, au, fr, de, jp, it, in, es)
204        #[arg(long, default_value = "us")]
205        region: String,
206
207        /// Save metadata to JSON file
208        #[arg(long)]
209        output: Option<PathBuf>,
210    },
211
212    /// Enrich M4B file with Audible metadata
213    Enrich {
214        /// M4B file to enrich
215        #[arg(long)]
216        file: PathBuf,
217
218        /// Audible ASIN
219        #[arg(long)]
220        asin: Option<String>,
221
222        /// Auto-detect ASIN from filename
223        #[arg(long)]
224        auto_detect: bool,
225
226        /// Audible region
227        #[arg(long, default_value = "us")]
228        region: String,
229    },
230}
231
232/// Arguments for the match command
233#[derive(Args)]
234pub struct MatchArgs {
235    /// M4B file to match
236    #[arg(long, short = 'f', conflicts_with = "dir")]
237    pub file: Option<PathBuf>,
238
239    /// Directory of M4B files
240    #[arg(long, short = 'd', conflicts_with = "file")]
241    pub dir: Option<PathBuf>,
242
243    /// Manual title override
244    #[arg(long)]
245    pub title: Option<String>,
246
247    /// Manual author override
248    #[arg(long)]
249    pub author: Option<String>,
250
251    /// Auto mode (non-interactive, select best match)
252    #[arg(long)]
253    pub auto: bool,
254
255    /// Audible region
256    #[arg(long, default_value = "us")]
257    pub region: String,
258
259    /// Keep existing cover art instead of downloading
260    #[arg(long)]
261    pub keep_cover: bool,
262
263    /// Dry run (show matches but don't apply)
264    #[arg(long)]
265    pub dry_run: bool,
266}