From 037c46a8e36a4ba37c3596afbd4b517b713bd3e2 Mon Sep 17 00:00:00 2001 From: Pierre Houston Date: Sun, 7 Jun 2020 17:31:38 -0700 Subject: [PATCH] change selent to quiet, expand readme example --- README.md | 39 ++++++++++++++++++++--- Sources/RenameCommand/RenameCommand.swift | 12 +++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f7e0b45..b404eda 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ It exports a struct `RenameOptions` conforming to the `ParsableArguments` protoc `runRename()` takes a function argument with a inout `name` parameter, you provide this function which changes `name` as desired. This is called for every file passed on the command line, with the file extension omitted if any, and the file gets renamed accordingly. -`RenameOptions` defines arguments `verbose`, `silent`, `dry-run`. +`RenameOptions` defines arguments `verbose`, `quiet`, `dry-run`. -It works well with `swift-sh`, also the `Regex` package at http://github.com/sharplet/Regex which it extends with a convenience function for case insensitive matching. +It works well with `swift-sh`, also the `Regex` package at http://github.com/sharplet/Regex which `RenameCommand` extends with an overload of its `replace` functions added to `String` allowing you to more conveniently specify case insensitive. -For example: +For example, this simple Swift "script" source file "myrename" (no ".swift" extension needed): ```swift #!/usr/bin/swift sh @@ -25,8 +25,8 @@ struct RenameMoviesCommand: ParsableCommand { func run() throws { try options.runRename() { name in name.replaceAll(matching: #"\."#, with: " ") - name.replaceFirst(matchingIgnoringCase: " 720p", with: "") - name.replaceFirst(matchingIgnoringCase: " 1080p", with: "") + name.replaceFirst(matching: " 720p", .ignoreCase, with: "") + name.replaceFirst(matching: " 1080p", .ignoreCase, with: "") name.replaceFirst(matching: " ([0-9][0-9][0-9][0-9])$", with: " ($1)") } } @@ -34,3 +34,32 @@ struct RenameMoviesCommand: ParsableCommand { RenameMoviesCommand.main() ``` + +after `chmod a+x myrename` and moving it to somewhere in the shell command path like `/usr/local/bin`, can then do: + +```bash +$ myrename --help +OVERVIEW: Renames my ripped movies from their old name format to how I prefer them now. + +USAGE: myrename [ ...] [--quiet] [--verbose] [--dry-run] + +ARGUMENTS: + Files to rename. + +OPTIONS: + -q, --quiet Suppress non-error output. + -v, --verbose Verbose output (overrides "--quiet"). + --dry-run Show what would be renamed (no files are changed). + -h, --help Show help information. + +$ myrename ~/Movies/Die.Hard.1988.720p.mp4 +'Die.Hard.1988.720p.mp4' renamed to 'Die Hard (1988).mp4' +``` + +## See Also + +- [ArgumentParser](https://github.com/apple/swift-argument-parser) +- [swift-sh](https://github.com/mxcl/swift-sh) +- [Regex](http://github.com/sharplet/Regex) +- [Files](https://github.com/JohnSundell/Files) + diff --git a/Sources/RenameCommand/RenameCommand.swift b/Sources/RenameCommand/RenameCommand.swift index 93ccd8f..32c9124 100644 --- a/Sources/RenameCommand/RenameCommand.swift +++ b/Sources/RenameCommand/RenameCommand.swift @@ -14,13 +14,13 @@ public struct RenameOptions: ParsableArguments { @Argument(help: "Files to rename.") public var files: [String] - @Flag(name: .shortAndLong, help: "Silent output.") - public var silent: Bool + @Flag(name: .shortAndLong, help: "Suppress non-error output.") + public var quiet: Bool - @Flag(name: .shortAndLong, help: "Verbose output (overrides \"--silent\").") + @Flag(name: .shortAndLong, help: "Verbose output (overrides \"--quiet\").") public var verbose: Bool - @Flag(name: .customLong("dry-run"), help: "Don't perform rename just output the result.") + @Flag(name: .customLong("dry-run"), help: "Show what would be renamed (no files are changed).") public var dryRun: Bool public init() { } // swift complains if this not present @@ -57,11 +57,11 @@ public struct RenameOptions: ParsableArguments { } if verbose { print("\(String(repeating: " ", count: "\(i)".count)) renamed to \(replacementName)") } - else if !silent { print("'\(fileName)' renamed to '\(replacementName)'") } + else if !quiet { print("'\(fileName)' renamed to '\(replacementName)'") } nrenamed += 1 } else { if verbose { print("\(String(repeating: " ", count: "\(i)".count)) not renamed") } - else if !silent && dryRun { print("'\(fileName)' not renamed") } + else if !quiet && dryRun { print("'\(fileName)' not renamed") } } } return nrenamed