Skip to content

Commit aebc06a

Browse files
committed
Readme: update with new functionality.
Also fix some other stuff.
1 parent 69eccb6 commit aebc06a

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

README.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ reader.readData()
244244

245245
All Contexts (`CustomContext` and `main`) implement `CommandRunning`, which means they can run commands using themselves as the Context. ReadableStream and String can also run commands, they use `main` as the Context and themselves as `.stdin`. As a shortcut you can just use `run(...)` instead of `main.run(...)`
246246

247-
There are three different ways of running a command:
247+
There are 4 different ways of running a command:
248248

249249
#### Run
250250

@@ -304,50 +304,58 @@ let command = runAsync("cmd", "-n", 245).onCompletion { command in
304304
// be notified when the command is finished.
305305
}
306306
command.stdout.onOutput { stdout in
307-
// be notified when the command produces output.
307+
// be notified when the command produces output (only on macOS).
308308
}
309309

310310
// do something with ‘command’ while it is still running.
311311

312312
try command.finish() // wait for it to finish.
313313
```
314314

315-
`runAsync` launches a command and continues before it's finished. It returns this:
315+
`runAsync` launches a command and continues before it's finished. It returns `AsyncCommand` which contains this:
316316

317317
```swift
318-
/// Output from the 'runAsync' methods.
319-
public final class AsyncCommand {
320-
public let stdout: ReadableStream
321-
public let stderror: ReadableStream
318+
public let stdout: ReadableStream
319+
public let stderror: ReadableStream
322320

323-
/// Is the command still running?
324-
public var isRunning: Bool
321+
/// Is the command still running?
322+
public var isRunning: Bool { get }
325323

326-
/// Terminates command.
327-
public func stop()
324+
/// Terminates the command by sending the SIGTERM signal.
325+
public func stop()
328326

329-
/// Waits for command to finish.
330-
/// - returns: itself
331-
/// - throws: `CommandError.returnedErrorCode(command: String, errorcode: Int)` if the exit code is anything but 0.
332-
public func finish() throws -> AsyncCommand
327+
/// Interrupts the command by sending the SIGINT signal.
328+
public func interrupt()
333329

334-
/// Waits for command to finish, then returns with exit code.
335-
public func exitcode() -> Int
330+
/// Temporarily suspends a command. Call resume() to resume a suspended command.
331+
public func suspend() -> Bool
336332

337-
/// Takes a closure to be called when the command has finished.
338-
/// - Parameter handler: A closure taking this AsyncCommand as input, returning nothing.
339-
/// - Returns: This AsyncCommand.
340-
public func onCompletion(_ handler: @escaping (AsyncCommand) -> Void) -> AsyncCommand
341-
}
333+
/// Resumes a command previously suspended with suspend().
334+
public func resume() -> Bool
335+
336+
/// Waits for this command to finish.
337+
public func finish() throws -> Self
338+
339+
/// Waits for command to finish, then returns with exit code.
340+
public func exitcode() -> Int
341+
342+
/// Waits for the command to finish, then returns why the command terminated.
343+
/// - returns: `.exited` if the command exited normally, otherwise `.uncaughtSignal`.
344+
public func terminationReason() -> Process.TerminationReason
345+
346+
/// Takes a closure to be called when the command has finished.
347+
public func onCompletion(_ handler: @escaping (AsyncCommand) -> Void) -> Self
342348
```
343349

344350
You can process standard output and standard error, and optionally wait until it's finished and handle any errors.
345351

346352
If you read all of command.stderror or command.stdout it will automatically wait for the command to close its streams (and presumably finish running). You can still call `finish()` to check for errors.
347353

354+
`runAsyncAndPrint` does the same as `runAsync`, but prints any output directly and it's return type `PrintedAsyncCommand` doesn't have the `.stdout` and `.stderror` properties.
355+
348356
#### Parameters
349357

350-
The 3 `run` functions above take 2 different types of parameters:
358+
The `run`* functions above take 2 different types of parameters:
351359

352360
##### (_ executable: String, _ args: Any ...)
353361

@@ -385,18 +393,18 @@ If the command provided to `runAsync` could not be launched for any reason the p
385393

386394
```swift
387395
let command = runAsync("cmd", "-n", 245)
388-
// do something with command.stderror or command.stdoutCommandError
396+
// ...
389397
do {
390398
try command.finish()
391-
} catch CommandError.ReturnedErrorCode(let error) {
399+
} catch CommandError.returnedErrorCode(let error) {
392400
// use error.command or error.errorcode
393401
}
394402
```
395403

396404
The `runAndPrint` command can also throw this error, in addition to this one if the command could not be launched:
397405

398406
```swift
399-
} catch CommandError.InAccessibleExecutable(let path) {
407+
} catch CommandError.inAccessibleExecutable(let path) {
400408
// ‘path’ is the full path to the executable
401409
}
402410
```

0 commit comments

Comments
 (0)