I want to write a command linte tool in swift that acts similar to grep.
I need to pipe the output of a program to this tool similar to "ls | mycommandlinetool"
Where do I find example code, tutorial or can anybody help me with the code.
I want to write a command linte tool in swift that acts similar to grep.
I need to pipe the output of a program to this tool similar to "ls | mycommandlinetool"
Where do I find example code, tutorial or can anybody help me with the code.
Where do I find example code, tutorial
There are not many users who write commandline tools in Swift, so it may be hard to find a good example or tutorial online.
But the concept of chainable commandline app is very simple, read input from standard input, write output to standard output.
(I assume you have a Mac with Xcode and its CommandLine Tools installed. If you have a question about Swift on other platform, you should better visit swit.org .)
Example:
double.swift
#!/usr/bin/swift
import Foundation
while let line = readLine() {
print(line, line)
}In Terminal:
OOPers-mini:commandline dev$ chmod +x double.swift
OOPers-mini:commandline dev$ ls -al
total 8
drwxr-xr-x 3 dev staff 96 1 26 08:29 .
drwxr-xr-x@ 4 dev staff 128 1 26 08:16 ..
-rwxr-xr-x 1 dev staff 90 1 26 08:25 double.swift
OOPers-mini:commandline dev$ ls -al | ./double.swift
total 8 total 8
drwxr-xr-x 3 dev staff 96 1 26 08:29 . drwxr-xr-x 3 dev staff 96 1 26 08:29 .
drwxr-xr-x@ 4 dev staff 128 1 26 08:16 .. drwxr-xr-x@ 4 dev staff 128 1 26 08:16 ..
-rwxr-xr-x 1 dev staff 90 1 26 08:25 double.swift -rwxr-xr-x 1 dev staff 90 1 26 08:25 double.swift
OOPers-mini:commandline dev$
One thing bad for you is that Swift Standard Library does not have many functionalites to work with standard input and output.
You may need to use some C lib functions.
You can create a more complex CommandLine app, using Xcode or Swift package manager.
Is Swift a mandate? Because Python might be a better choice.
Thank you, it probably will solve my problem, but as far as I remember tool chaining requires a signaling mechanism, so not to flood the receiving tool.
When chaining inside swift, there is a Pipe and Process object that probably contains that.
Anyhow, my requirement this time is to augment a log file and the receiving tool won't have a problem processing the data fast enough.
Thank you.
as far as I remember tool chaining requires a signaling mechanism
my requirement this time is to augment a log file and the receiving tool won't have a problem processing the data fast enough.
Frankly, I do not understand what sort of functionality you are mentioning.
So, I recommend you to implement one simple functionaly of your own. And when you find some difficulty implementing what you want, you can start a new thread.
as far as I remember tool chaining requires a signaling mechanism, so not to flood the receiving tool.
I’m not sure what you’re getting at here.
readline is a synchronous call. If your tool doesn’t call it fast enough, data will start to back up in the pipe connecting the standard output of the producer process to your tool’s standard in. If enough data backs up in that pipe, the
write call on the producer will block.
In theory you could construct your producer to do something clever in that case, but that assumes that you control the producer. From what you’ve posted it seems like you’re working on the consumer. If so, there’s really not much you can do other than to read data faster.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"