Skip to content

Commit

Permalink
change selent to quiet, expand readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Houston committed Jun 8, 2020
1 parent 78b1f9c commit 037c46a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,12 +25,41 @@ 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)")
}
}
}

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 [<files> ...] [--quiet] [--verbose] [--dry-run]

ARGUMENTS:
<files> 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)

12 changes: 6 additions & 6 deletions Sources/RenameCommand/RenameCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 037c46a

Please sign in to comment.