JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Location conforms to ArgumentParser. ExpressibleByArgument #132

Closed doozMen closed 1 year ago

doozMen commented 2 years ago

To make it possible to use files in conjunction with Apples swift-argument-parser it would be handy if File and Folder confirm to ExpressibleByArgument. This way Files and Folder can be passed as options.

It is possible to extent Files to have this ability. The problem I faced was when I tried to make it conform to an Optional as well.

If you would consider this PR I can provide more info?

Background on the optional

Basically I want the following code to compile

import ArgumentParser
struct DoMagic: ParsableCommand {
    @Option var someOptionalFolder: Folder?
    @Option var someOptionalFile: File?
}

The above does not compile as I cannot extend Optional twice, one time for Folder and another for File

extension Optional: ExpressibleByArgument where Wrapped == Folder {
    public init?(argument: String) {
        do {
            self = try Folder(path: argument)
        } catch {
            do {
                var currentURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
                currentURL = currentURL.appendingPathComponent(argument)
                self = try Folder(path: currentURL.path)
            } catch {
                print("\(error)")
                return nil
            }
        }
    }
}

So maybe adding a dependency on swift-argument-parser might be an issue. But It is handy for commandline tools.

maximkrouk commented 2 years ago

The argument-parser is a pretty heavy dependency, plus it's specific for CLIs, probably you should just create another package like files-cli or smth (btw currently kebab-case is kinda more prefered for Swift packages 😎)

doozMen commented 1 year ago

The argument-parser is a pretty heavy dependency, plus it's specific for CLIs, probably you should just create another package like files-cli or smth (btw currently kebab-case is kinda more prefered for Swift packages 😎)

Thanks for the consideration. I think indeed that ArgumentParser dependency is a bit much