jakeheis / SwiftCLI

A powerful framework for developing CLIs in Swift
MIT License
861 stars 72 forks source link

[Question] How to passthrough stdout/stdin for External Tasks #106

Open joshuawright11 opened 3 years ago

joshuawright11 commented 3 years ago

I'd like to run an external task from my command that may ask the user for input.

What's the best way of passing through requests for input (I assume from the external task's stdout) to the user, and also passing the user's input to the external tasks stdin?

A very simple example:

I'd like to run sudo echo hello as an external task and have the user be notified when sudo asks for their password. How could I accomplish that with the Task API?

joshuawright11 commented 3 years ago

So far what I've got is

@main struct Run {
    static func main() {
        let cli = CLI(singleCommand: MyCommand())
        cli.go()
    }
}

final class MyCommand: Command {
    var name: String = "something"

    func execute() throws {
        Task(executable: "sudo", arguments: ["-S", "echo", "hello"], stdout: stdout, stderr: stderr).runSync()
    }
}

Which results in prompting the user for their password, but I can't figure out how to pass input to the command through to the Task.

I also see there is a Task.execvp which causes input to go to the subcommand, but it seems to mean that the rest of the current command won't be executed.