{"id":32149352,"url":"https://github.com/jpmhouston/renamecommand","last_synced_at":"2025-10-21T09:54:43.898Z","repository":{"id":63913633,"uuid":"248858235","full_name":"jpmhouston/RenameCommand","owner":"jpmhouston","description":"A ParsableArguments implementation for making minimal rename scripts in Swift","archived":false,"fork":false,"pushed_at":"2021-01-04T09:09:12.000Z","size":19,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-21T09:54:43.183Z","etag":null,"topics":["argumentparser","command-line","regular-expression","rename-script","swift","swift-package-manager","swift-sh"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jpmhouston.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-20T21:47:47.000Z","updated_at":"2025-09-29T08:24:07.000Z","dependencies_parsed_at":"2023-01-14T13:30:35.986Z","dependency_job_id":null,"html_url":"https://github.com/jpmhouston/RenameCommand","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/jpmhouston/RenameCommand","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpmhouston%2FRenameCommand","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpmhouston%2FRenameCommand/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpmhouston%2FRenameCommand/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpmhouston%2FRenameCommand/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpmhouston","download_url":"https://codeload.github.com/jpmhouston/RenameCommand/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpmhouston%2FRenameCommand/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280240308,"owners_count":26296527,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argumentparser","command-line","regular-expression","rename-script","swift","swift-package-manager","swift-sh"],"created_at":"2025-10-21T09:54:42.444Z","updated_at":"2025-10-21T09:54:43.893Z","avatar_url":"https://github.com/jpmhouston.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RenameCommand\n\nA library making it easy to make a swift command-line program for renaming files according to your own rules.\n\n#### Details\n\nThis package exports a struct `RenameOptions` conforming to the `ParsableArguments` protocol from Apple's `ArgumentParser`. It's intended to be used with `@OptionGroup()` and your own `ParsableCommand` and provides a `runRename()` function you can call within your own `run()`, implementing all the boilerplate file system and string processing involved in a command that renames files. Your code is little more than your custom regular expressions or any such manipulation of the base filename.\n\n `runRename()` takes a function argument with a inout `name` `String` (and file extension `String`), you provide this function which changes `name` as desired. This is called for every file passed on the command line, with the directory omitted and file extension separated, and the file gets renamed accordingly. Leave `name` unchanged (or change to empty string) to do nothing to the file.\n\n`RenameOptions`  defines arguments `--verbose`/`-v`, `--quiet`/`-q`, `--dry-run`, `--try` (not to mention the defaults provided by ArgumentParser, `--help`/`-h` and `--generate-completion-script`). The difference between  `--dry-run` and `--try` are that the former fails as usual if the file arguments aren't found, the latter will allow any file argument as if they were files that existed; both show the would-be results of the rename without carrying it out.\n\nIt works well with `swift-sh`, also the `sharplet/Regex` package which `RenameCommand` extends with an overload of its  `String` extension functions allowing you to more conveniently specify case insensitive. See below.\n\n#### Example\n\nWith `swift-sh` installed, this simple Swift \"script\" source file \"myrename\" (no \".swift\" extension needed) is all you need to give you a fully functional custom file renaming command:\n\n```swift\n#!/usr/bin/swift sh\nimport ArgumentParser // apple/swift-argument-parser\nimport RenameCommand // @jpmhouston\nimport Regex // @sharplet\n\nstruct RenameMoviesCommand: ParsableCommand {\n    static let configuration = CommandConfiguration(abstract: \"Renames my ripped movies from their old name format to how I prefer them now.\")\n    @OptionGroup() var options: RenameCommand.RenameOptions\n    \n    func run() throws {\n        try options.runRename() { name, _ in\n            name.replaceAll(matching: #\"\\.\"#, with: \" \")\n            name.replaceFirst(matching: \" 720p\", .ignoreCase, with: \"\")\n            name.replaceFirst(matching: \" 1080p\", .ignoreCase, with: \"\")\n            name.replaceFirst(matching: \" ([0-9][0-9][0-9][0-9])$\", with: \" ($1)\")\n        }\n    }\n}\n\nRenameMoviesCommand.main()\n```\n\nThe functions  `replaceFirst` and `replaceAll` are from `sharplet/Regex`. If your script uses this package too, you're also able to pass options such as `.ignoreCase`  to those shortcut functions as shown rather than having to construct a `Regex` yourself to provide those options.\n\nThanks to the magic of `swift-sh`, after a `chmod a+x myrename` and moving it to somewhere in the shell command path like `/usr/local/bin`, you can then do:\n\n```bash\n$ myrename --help\nOVERVIEW: Renames my ripped movies from their old name format to how I prefer them now.\n\nUSAGE: myrename [\u003cfiles\u003e ...] [--quiet] [--verbose] [--dry-run] [--try]\n\nARGUMENTS:\n  \u003cfiles\u003e                 Files to rename. \n\nOPTIONS:\n  -q, --quiet             Suppress non-error output. \n  -v, --verbose           Verbose output (overrides \"--quiet\"). \n  --dry-run               Show what would be renamed (overrides \"--quiet\", no files are changed).\n  --try                   Try hypothetical file names (overrides \"--quiet\", no files are changed).\n  -h, --help              Show help information.\n\n$ myrename ~/Movies/Die.Hard.1988.720p.mp4\n'Die.Hard.1988.720p.mp4' renamed to 'Die Hard (1988).mp4'\n```\n\n#### Tip for fish shell users\n\nIf you use the [fish shell](https://fishshell.com/), add this [selection function](https://gist.github.com/jpmhouston/4e23e60767055f98fccfee956eef9eda) and you can rename the current Finder selection with simply this:\n\n```bash\n$ myrename (selection)\n```\n\n(exercise for the reader: make something similar that works in other shells)\n\n## See Also\n\n- [ArgumentParser](https://github.com/apple/swift-argument-parser)\n- [swift-sh](https://github.com/mxcl/swift-sh)\n- [Regex](http://github.com/sharplet/Regex)\n- [Files](https://github.com/JohnSundell/Files)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpmhouston%2Frenamecommand","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpmhouston%2Frenamecommand","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpmhouston%2Frenamecommand/lists"}