Let run* commands take shell arguments of any type.
This commit is contained in:
+12
-78
@@ -67,19 +67,9 @@ extension ShellContextType {
|
||||
- parameter args: the arguments, one string for each.
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func run (executable: String, _ args: String ...) -> String {
|
||||
return run(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Shortcut for shell command, returns output and errors as a String.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of the arguments, one string for each.
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func run (executable: String, _ args: [String]) -> String {
|
||||
return outputFromRun(setupTask(executable, args: args))
|
||||
public func run (executable: String, _ args: Any ...) -> String {
|
||||
let stringargs = args.flatten().map { String($0) }
|
||||
return outputFromRun(setupTask(executable, args: stringargs))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,19 +143,9 @@ extension ShellContextType {
|
||||
- parameter args: arguments to the executable.
|
||||
- returns: an AsyncShellTask with standard output, standard error and a 'finish' function.
|
||||
*/
|
||||
public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask {
|
||||
return runAsync(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Run executable and return before it is finished.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of arguments to the executable.
|
||||
- returns: an AsyncShellTask with standard output, standard error and a 'finish' function.
|
||||
*/
|
||||
public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask {
|
||||
return AsyncShellTask(task: setupTask(executable, args: args))
|
||||
public func runAsync (executable: String, _ args: Any ...) -> AsyncShellTask {
|
||||
let stringargs = args.flatten().map { String($0) }
|
||||
return AsyncShellTask(task: setupTask(executable, args: stringargs))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,25 +161,14 @@ extension ShellContextType {
|
||||
- parameter args: arguments to the executable.
|
||||
- throws: a ShellError if the return code is anything but 0.
|
||||
*/
|
||||
public func runAndPrint (executable: String, _ args: String ...) throws {
|
||||
return try runAndPrint(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Run executable and print output and errors.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of arguments to the executable.
|
||||
- throws: a ShellError if the return code is anything but 0.
|
||||
*/
|
||||
public func runAndPrint (executable: String, _ args: [String]) throws {
|
||||
let task = setupTask(executable, args: args)
|
||||
public func runAndPrint (executable: String, _ args: Any ...) throws {
|
||||
let stringargs = args.flatten().map { String($0) }
|
||||
let task = setupTask(executable, args: stringargs)
|
||||
task.launch()
|
||||
try task.finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Global functions
|
||||
|
||||
/**
|
||||
@@ -209,22 +178,10 @@ Shortcut for shell command, returns output and errors as a String.
|
||||
- parameter args: the arguments, one string for each.
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func run (executable: String, _ args: String ...) -> String {
|
||||
public func run (executable: String, _ args: Any ...) -> String {
|
||||
return main.run(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Shortcut for shell command, returns output and errors as a String.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of the arguments, one string for each.
|
||||
- returns: standard output and standard error in one string, trimmed of whitespace and newline if it is single-line.
|
||||
*/
|
||||
public func run (executable: String, _ args: [String]) -> String {
|
||||
return main.run(executable, args)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Run executable and return before it is finished.
|
||||
|
||||
@@ -232,22 +189,10 @@ Run executable and return before it is finished.
|
||||
- parameter args: arguments to the executable.
|
||||
- returns: an AsyncShellTask with standard output, standard error and a 'finish' function.
|
||||
*/
|
||||
public func runAsync (executable: String, _ args: String ...) -> AsyncShellTask {
|
||||
public func runAsync (executable: String, _ args: Any ...) -> AsyncShellTask {
|
||||
return main.runAsync(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Run executable and return before it is finished.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of arguments to the executable.
|
||||
- returns: an AsyncShellTask with standard output, standard error and a 'finish' function.
|
||||
*/
|
||||
public func runAsync (executable: String, _ args: [String]) -> AsyncShellTask {
|
||||
return main.runAsync(executable, args)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Run executable and print output and errors.
|
||||
|
||||
@@ -255,17 +200,6 @@ Run executable and print output and errors.
|
||||
- parameter args: arguments to the executable.
|
||||
- throws: a ShellError if the return code is anything but 0.
|
||||
*/
|
||||
public func runAndPrint (executable: String, _ args: String ...) throws {
|
||||
return try main.runAndPrint(executable, args)
|
||||
}
|
||||
|
||||
/**
|
||||
Run executable and print output and errors.
|
||||
|
||||
- parameter executable: path to an executable file.
|
||||
- parameter args: array of arguments to the executable.
|
||||
- throws: a ShellError if the return code is anything but 0.
|
||||
*/
|
||||
public func runAndPrint (executable: String, _ args: [String]) throws {
|
||||
public func runAndPrint (executable: String, _ args: Any ...) throws {
|
||||
return try main.runAndPrint(executable, args)
|
||||
}
|
||||
|
||||
@@ -10,17 +10,18 @@ private protocol AnyArrayType {
|
||||
}
|
||||
|
||||
extension Array: AnyArrayType {
|
||||
var anyValues: [Any] {
|
||||
private var anyValues: [Any] {
|
||||
return self.map { $0 as Any }
|
||||
}
|
||||
}
|
||||
|
||||
public extension Array where Element: Any {
|
||||
|
||||
func flatten () -> [Any] {
|
||||
let result: [Any] = self.flatMap { x -> [Any] in
|
||||
switch x {
|
||||
case let anyarray as AnyArrayType:
|
||||
return anyarray.anyValues
|
||||
return anyarray.anyValues.flatten()
|
||||
default:
|
||||
return [x]
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ class Run_Tests: XCTestCase {
|
||||
}
|
||||
|
||||
func testArgumentsFromArray () {
|
||||
XCTAssertEqual( SwiftShell.run("/bin/echo", ["one", "two"]), "one two" )
|
||||
let stringarray = ["one", "two"]
|
||||
XCTAssertEqual( SwiftShell.run("/bin/echo", stringarray), "one two" )
|
||||
}
|
||||
|
||||
func testSinglelineOutput () {
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
@testable import SwiftShell
|
||||
|
||||
class Array_Tests: XCTestCase {
|
||||
@@ -16,9 +15,23 @@ class Array_Tests: XCTestCase {
|
||||
|
||||
func testAnyArrayFlattenAFlatOne () {
|
||||
XCTAssertEqual( ([1,"2"] as [Any]).flatten().description, "[1, \"2\"]")
|
||||
XCTAssertEqual( (["1","2"] as [Any]).flatten().description, ["1", "2"].description)
|
||||
XCTAssertEqual( ([1,2] as [Any]).flatten().description, [1,2].description)
|
||||
}
|
||||
|
||||
func testAnyArrayFlattenABumpyOne () {
|
||||
let intarray = [1,2]
|
||||
XCTAssertEqual( ([intarray] as [Any]).flatten().description, [1,2].description)
|
||||
XCTAssertEqual( (["1",[2,3]] as [Any]).flatten().description, "[\"1\", 2, 3]")
|
||||
|
||||
let stringarray = ["one", "two"]
|
||||
XCTAssertEqual( ([stringarray, "three"] as [Any]).flatten().description, ["one","two","three"].description)
|
||||
XCTAssertEqual( ([stringarray, 3] as [Any]).flatten().description, (["one","two",3] as [Any]).description)
|
||||
}
|
||||
|
||||
func testAnyArrayFlattenAVeryBumpyOne () {
|
||||
let intarray = [1,2]
|
||||
XCTAssertEqual( ([[intarray]] as [Any]).flatten().description, intarray.description)
|
||||
XCTAssertEqual( (["1",[2,[3] as Any]] as [Any]).flatten().description, "[\"1\", 2, 3]")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user