]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/lyricli/main.swift
Update swift files to use Bariloche
[rbdr/lyricli] / Sources / lyricli / main.swift
1 import Foundation
2 import Bariloche
3
4 // Entry point of the application. This is the main executable
5 private func main() {
6
7 // Bariloche assumes at least one argument, so bypass
8 // if that's the case.
9 if CommandLine.arguments.count > 1 {
10 let parser = Bariloche(command: LyricliCommand())
11 let result = parser.parse()
12
13 if result.count == 0 {
14 exit(EX_USAGE)
15 }
16
17 if let lyricliCommand = result[0] as? LyricliCommand {
18 // Flags
19 checkVersionFlag(lyricliCommand)
20 checkListSourcesFlag(lyricliCommand)
21 checkTitleFlag(lyricliCommand)
22
23 // String Options
24
25 checkEnableSourceFlag(lyricliCommand)
26 checkDisableSourceFlag(lyricliCommand)
27 checkResetSourceFlag(lyricliCommand)
28
29 checkPositionalArguments(lyricliCommand)
30
31 }
32 }
33
34 // Run Lyricli
35 Lyricli.printLyrics()
36 }
37
38 // Handle the version flag
39
40 private func checkVersionFlag(_ command: LyricliCommand) {
41 if command.version.value {
42 print(Lyricli.version)
43 exit(0)
44 }
45 }
46
47 // Handle the list sources flag
48
49 private func checkListSourcesFlag(_ command: LyricliCommand) {
50 if command.listSources.value {
51 Lyricli.printSources()
52 exit(0)
53 }
54 }
55
56 // Handle the title flag
57
58 private func checkTitleFlag(_ command: LyricliCommand) {
59 Lyricli.showTitle = command.showTitle.value
60 }
61
62 // Handle the enable source flag
63
64 private func checkEnableSourceFlag(_ command: LyricliCommand) {
65 if let source = command.enableSource.value {
66 Lyricli.enableSource(source)
67 exit(0)
68 }
69 }
70
71 // Handle the disable source flag
72
73 private func checkDisableSourceFlag(_ command: LyricliCommand) {
74 if let source = command.disableSource.value {
75 Lyricli.disableSource(source)
76 exit(0)
77 }
78 }
79
80 // Handle the reset source flag
81
82 private func checkResetSourceFlag(_ command: LyricliCommand) {
83 if let source = command.resetSource.value {
84 Lyricli.resetSource(source)
85 exit(0)
86 }
87 }
88
89 // Handle the positional arguments
90
91 private func checkPositionalArguments(_ command: LyricliCommand) {
92 if let artist = command.artist.value {
93
94 let currentTrack: Track
95
96 if let trackName = command.trackName.value {
97 currentTrack = Track(withName: trackName, andArtist: artist)
98 } else {
99 currentTrack = Track(withName: "", andArtist: artist)
100 }
101
102 Lyricli.printLyrics(currentTrack)
103 exit(0)
104 }
105 }
106
107 main()