spotify-cli 0.5.0

A command-line interface for Spotify
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! CLI argument definitions and parsing.
//!
//! This module defines the command-line interface using clap.

pub mod args;
pub mod commands;

use std::io;

use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::{Shell, generate};

use crate::constants::DEFAULT_LIMIT;

// Re-export all command types for convenience
pub use args::*;
pub use clap_complete::Shell as CompletionShell;

/// Generate shell completion script to stdout
pub fn print_completions(shell: Shell) {
    let mut cmd = Cli::command();
    generate(shell, &mut cmd, "spotify-cli", &mut io::stdout());
}

#[derive(Parser)]
#[command(name = "spotify-cli", version)]
#[command(about = "Command line interface for Spotify")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,

    /// Output JSON response (silent if not specified)
    #[arg(long, short = 'j', global = true)]
    pub json: bool,

    /// Enable verbose logging (use -vv for debug, -vvv for trace)
    #[arg(long, short = 'v', global = true, action = clap::ArgAction::Count)]
    pub verbose: u8,

    /// Log output format (pretty or json)
    #[arg(long, global = true, default_value = "pretty")]
    pub log_format: String,
}

#[derive(Subcommand)]
pub enum Command {
    /// Authentication commands
    Auth {
        #[command(subcommand)]
        command: AuthCommand,
    },
    /// Player controls (alias: p)
    #[command(alias = "p")]
    Player {
        #[command(subcommand)]
        command: PlayerCommand,
    },
    /// Manage pinned resources
    Pin {
        #[command(subcommand)]
        command: PinCommand,
    },
    /// Search Spotify and pinned resources (alias: s)
    #[command(alias = "s")]
    Search {
        /// Search query (can be empty if using filters)
        #[arg(default_value = "")]
        query: String,
        /// Filter by type(s): track, artist, album, playlist, show, episode, audiobook
        /// Can specify multiple: --type track --type album
        #[arg(long = "type", short = 'T')]
        types: Vec<String>,
        /// Results per type (default 20, max 50)
        #[arg(long, short = 'l', default_value_t = DEFAULT_LIMIT)]
        limit: u8,
        /// Only search pinned resources (skip Spotify API)
        #[arg(long)]
        pins_only: bool,
        /// Only show results where name contains the query
        #[arg(long, short = 'e')]
        exact: bool,
        /// Filter by artist name
        #[arg(long, short = 'a')]
        artist: Option<String>,
        /// Filter by album name
        #[arg(long, short = 'A')]
        album: Option<String>,
        /// Filter by track name
        #[arg(long, short = 't')]
        track: Option<String>,
        /// Filter by year or range (e.g., 2020 or 1990-2000)
        #[arg(long, short = 'y')]
        year: Option<String>,
        /// Filter by genre
        #[arg(long, short = 'g')]
        genre: Option<String>,
        /// Filter by ISRC code (tracks only)
        #[arg(long)]
        isrc: Option<String>,
        /// Filter by UPC code (albums only)
        #[arg(long)]
        upc: Option<String>,
        /// Only albums released in the past two weeks
        #[arg(long)]
        new: bool,
        /// Only albums with lowest 10% popularity
        #[arg(long)]
        hipster: bool,
        /// Play the first result
        #[arg(long, short = 'p')]
        play: bool,
        /// Sort results by fuzzy match score
        #[arg(long, short = 's')]
        sort: bool,
    },
    /// Manage playlists (alias: pl)
    #[command(alias = "pl")]
    Playlist {
        #[command(subcommand)]
        command: PlaylistCommand,
    },
    /// Manage your library (liked songs) (alias: lib)
    #[command(alias = "lib")]
    Library {
        #[command(subcommand)]
        command: LibraryCommand,
    },
    /// Get info about track, album, or artist (defaults to now playing) (alias: i)
    #[command(alias = "i")]
    Info {
        #[command(subcommand)]
        command: InfoCommand,
    },
    /// User profile and stats
    User {
        #[command(subcommand)]
        command: UserCommand,
    },
    /// Manage podcasts (shows)
    Show {
        #[command(subcommand)]
        command: ShowCommand,
    },
    /// Manage podcast episodes
    Episode {
        #[command(subcommand)]
        command: EpisodeCommand,
    },
    /// Manage audiobooks
    Audiobook {
        #[command(subcommand)]
        command: AudiobookCommand,
    },
    /// Manage saved albums
    Album {
        #[command(subcommand)]
        command: AlbumCommand,
    },
    /// Get audiobook chapter details
    Chapter {
        #[command(subcommand)]
        command: ChapterCommand,
    },
    /// Browse Spotify categories
    Category {
        #[command(subcommand)]
        command: CategoryCommand,
    },
    /// Follow/unfollow artists and users
    Follow {
        #[command(subcommand)]
        command: FollowCommand,
    },
    /// List available Spotify markets (countries)
    Markets,
    /// RPC daemon for external control (Neovim, scripts, etc.)
    #[cfg(unix)]
    Daemon {
        #[command(subcommand)]
        command: DaemonCommand,
    },
    /// Generate shell completions
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_auth_login() {
        let cli = Cli::try_parse_from(["spotify-cli", "auth", "login"]).unwrap();
        match cli.command {
            Command::Auth {
                command: AuthCommand::Login { force },
            } => {
                assert!(!force);
            }
            _ => panic!("Expected Auth Login command"),
        }
    }

    #[test]
    fn parse_auth_login_force() {
        let cli = Cli::try_parse_from(["spotify-cli", "auth", "login", "-f"]).unwrap();
        match cli.command {
            Command::Auth {
                command: AuthCommand::Login { force },
            } => {
                assert!(force);
            }
            _ => panic!("Expected Auth Login command"),
        }
    }

    #[test]
    fn parse_player_next() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "next"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Next,
            } => {}
            _ => panic!("Expected Player Next command"),
        }
    }

    #[test]
    fn parse_player_alias_p() {
        let cli = Cli::try_parse_from(["spotify-cli", "p", "next"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Next,
            } => {}
            _ => panic!("Expected Player Next command via alias"),
        }
    }

    #[test]
    fn parse_player_volume() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "volume", "50"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Volume { percent },
            } => {
                assert_eq!(percent, 50);
            }
            _ => panic!("Expected Player Volume command"),
        }
    }

    #[test]
    fn parse_player_volume_max() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "volume", "100"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Volume { percent },
            } => {
                assert_eq!(percent, 100);
            }
            _ => panic!("Expected Player Volume command"),
        }
    }

    #[test]
    fn parse_player_volume_invalid() {
        let result = Cli::try_parse_from(["spotify-cli", "player", "volume", "101"]);
        assert!(result.is_err());
    }

    #[test]
    fn parse_search_default() {
        let cli = Cli::try_parse_from(["spotify-cli", "search", "test query"]).unwrap();
        match cli.command {
            Command::Search {
                query,
                limit,
                pins_only,
                exact,
                ..
            } => {
                assert_eq!(query, "test query");
                assert_eq!(limit, 20);
                assert!(!pins_only);
                assert!(!exact);
            }
            _ => panic!("Expected Search command"),
        }
    }

    #[test]
    fn parse_search_with_options() {
        let cli = Cli::try_parse_from([
            "spotify-cli",
            "search",
            "query",
            "--type",
            "track",
            "--limit",
            "10",
            "--pins-only",
            "--exact",
        ])
        .unwrap();
        match cli.command {
            Command::Search {
                query,
                types,
                limit,
                pins_only,
                exact,
                ..
            } => {
                assert_eq!(query, "query");
                assert_eq!(types, vec!["track"]);
                assert_eq!(limit, 10);
                assert!(pins_only);
                assert!(exact);
            }
            _ => panic!("Expected Search command"),
        }
    }

    #[test]
    fn parse_search_alias_s() {
        let cli = Cli::try_parse_from(["spotify-cli", "s", "query"]).unwrap();
        match cli.command {
            Command::Search { query, .. } => {
                assert_eq!(query, "query");
            }
            _ => panic!("Expected Search command via alias"),
        }
    }

    #[test]
    fn parse_json_flag() {
        let cli = Cli::try_parse_from(["spotify-cli", "-j", "markets"]).unwrap();
        assert!(cli.json);
    }

    #[test]
    fn parse_verbose_flag() {
        let cli = Cli::try_parse_from(["spotify-cli", "-v", "markets"]).unwrap();
        assert_eq!(cli.verbose, 1);
    }

    #[test]
    fn parse_verbose_multiple() {
        let cli = Cli::try_parse_from(["spotify-cli", "-vvv", "markets"]).unwrap();
        assert_eq!(cli.verbose, 3);
    }

    #[test]
    fn parse_log_format() {
        let cli = Cli::try_parse_from(["spotify-cli", "--log-format", "json", "markets"]).unwrap();
        assert_eq!(cli.log_format, "json");
    }

    #[test]
    fn parse_pin_add() {
        let cli = Cli::try_parse_from([
            "spotify-cli",
            "pin",
            "add",
            "track",
            "spotify:track:123",
            "my alias",
        ])
        .unwrap();
        match cli.command {
            Command::Pin {
                command:
                    PinCommand::Add {
                        resource_type,
                        url_or_id,
                        alias,
                        tags,
                    },
            } => {
                assert_eq!(resource_type, "track");
                assert_eq!(url_or_id, "spotify:track:123");
                assert_eq!(alias, "my alias");
                assert!(tags.is_none());
            }
            _ => panic!("Expected Pin Add command"),
        }
    }

    #[test]
    fn parse_pin_add_with_tags() {
        let cli = Cli::try_parse_from([
            "spotify-cli",
            "pin",
            "add",
            "playlist",
            "123",
            "alias",
            "-t",
            "tag1,tag2",
        ])
        .unwrap();
        match cli.command {
            Command::Pin {
                command: PinCommand::Add { tags, .. },
            } => {
                assert_eq!(tags, Some("tag1,tag2".to_string()));
            }
            _ => panic!("Expected Pin Add command"),
        }
    }

    #[test]
    fn parse_playlist_list() {
        let cli = Cli::try_parse_from(["spotify-cli", "playlist", "list"]).unwrap();
        match cli.command {
            Command::Playlist {
                command: PlaylistCommand::List { limit, offset },
            } => {
                assert_eq!(limit, 20);
                assert_eq!(offset, 0);
            }
            _ => panic!("Expected Playlist List command"),
        }
    }

    #[test]
    fn parse_library_alias() {
        let cli = Cli::try_parse_from(["spotify-cli", "lib", "list"]).unwrap();
        match cli.command {
            Command::Library {
                command: LibraryCommand::List { .. },
            } => {}
            _ => panic!("Expected Library List command via alias"),
        }
    }

    #[test]
    fn parse_info_alias() {
        let cli = Cli::try_parse_from(["spotify-cli", "i", "track"]).unwrap();
        match cli.command {
            Command::Info {
                command: InfoCommand::Track { .. },
            } => {}
            _ => panic!("Expected Info Track command via alias"),
        }
    }

    #[test]
    fn parse_markets() {
        let cli = Cli::try_parse_from(["spotify-cli", "markets"]).unwrap();
        match cli.command {
            Command::Markets => {}
            _ => panic!("Expected Markets command"),
        }
    }

    #[test]
    fn parse_player_repeat() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "repeat", "track"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Repeat { mode },
            } => {
                assert_eq!(mode, "track");
            }
            _ => panic!("Expected Player Repeat command"),
        }
    }

    #[test]
    fn parse_player_shuffle() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "shuffle", "on"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Shuffle { state },
            } => {
                assert_eq!(state, "on");
            }
            _ => panic!("Expected Player Shuffle command"),
        }
    }

    #[test]
    fn parse_player_seek() {
        let cli = Cli::try_parse_from(["spotify-cli", "player", "seek", "1:30"]).unwrap();
        match cli.command {
            Command::Player {
                command: PlayerCommand::Seek { position },
            } => {
                assert_eq!(position, "1:30");
            }
            _ => panic!("Expected Player Seek command"),
        }
    }

    #[test]
    fn parse_user_top() {
        let cli =
            Cli::try_parse_from(["spotify-cli", "user", "top", "tracks", "-r", "short"]).unwrap();
        match cli.command {
            Command::User {
                command:
                    UserCommand::Top {
                        item_type,
                        range,
                        limit,
                    },
            } => {
                assert_eq!(item_type, "tracks");
                assert_eq!(range, "short");
                assert_eq!(limit, 20);
            }
            _ => panic!("Expected User Top command"),
        }
    }

    #[test]
    fn parse_user_top_default_range() {
        let cli = Cli::try_parse_from(["spotify-cli", "user", "top", "artists"]).unwrap();
        match cli.command {
            Command::User {
                command:
                    UserCommand::Top {
                        item_type,
                        range,
                        limit,
                    },
            } => {
                assert_eq!(item_type, "artists");
                assert_eq!(range, "medium");
                assert_eq!(limit, 20);
            }
            _ => panic!("Expected User Top command"),
        }
    }

    #[test]
    fn parse_follow_artist() {
        let cli = Cli::try_parse_from(["spotify-cli", "follow", "artist", "123"]).unwrap();
        match cli.command {
            Command::Follow {
                command: FollowCommand::Artist { ids, dry_run },
            } => {
                assert_eq!(ids, vec!["123"]);
                assert!(!dry_run);
            }
            _ => panic!("Expected Follow Artist command"),
        }
    }
}