Reputation:
I wondering about how can I create my own command line or shell program like "cat", "git" etc... with Swift and Xcode?
For example:
rod -a "hello world!" rod --version rod /list -e "bye world!"
Thanks in advance,
Upvotes: 4
Views: 2281
Reputation: 2935
My answer pertains to the title of your post more than the content. I wanted to take a shell script that I use on my Mac for checking console.log(s) before commiting. I know lint can pick these up in git commit, but I was tired of needing to go change, add, re-commit. So, I wanted to run a check first. This, I think, could also help other folks finding your post per the title, and might help get you where you want too.
I've this script at ~/tools/console-log-check.sh:
#!/bin/sh
# get the output of git status
git_status_output=$(git status)
# split git_status_output to only have lines starting with the word modified
git_status_output=$(echo "$git_status_output" | grep "modified:")
# chop the "modified: " off of all lines in git_status_output
git_status_output=$(echo "$git_status_output" | sed 's/modified: //g')
# read each line as a relative path file and scan for console.log lines
# show line number of any found console.log lines
echo "$git_status_output" | while read line; do
echo "scanning $line for console.log(s)"
grep -n "console.log" "$line"
And, while I can run it at the commandline (after chmod'ing to make it executable) I didn't want to: ~/tools/console-log-check.sh
. I wanted to just simply, like grep
, just run it as a program console-log-check
, or shorter yet, console-check
- and I wanted auto suggest to suggest this program a few letters in... i.e. "con" (and hit tab).
To accomplish this I go into my ~/.zshrc
and add this:
console-check () {
~/tools/check-for-console-logs.sh
}
Then, if I'm on a prompt I already had opened I need to remember to:
source ~/.zshrc
Now, from within a commandline on any project I can simply:
console-check
I believe that is more what you're looking for. I hope that helps - if not you then others who find this post by title (like me). Goodday.
Upvotes: 0
Reputation: 458
The easiest way to do this is probably the following:
Create an empty swift file by entering the terminal command
touch myFirstProgram.swift
Make this file executable by entering the terminal command
chmod +x myFirstProgram.swift
Open this directory in finder by entering the terminal command
open .
Double-click on myFirstProgram.swift to open the empty file in Xcode. Then, inside Xcode, write the following lines into your first swift program. The first line starts the swift interpreter, anything after that is simple swift code.
#!/usr/bin/env xcrun swift
println("Hello world.")
After saving your program, you can start this by entering the terminal command
./myFirstProgram.swift
done.
Once you have understood these steps, you can go out and explore freely. I found, for example, an excellent first tutorial at http://practicalswift.com/2014/06/07/swift-scripts-how-to-write-small-command-line-scripts-in-swift/. The only outdated information I found there was that the first line of the swift program should be as I detailed in point 5 (without a "-i" at the end of the line).
Have fun!
Upvotes: 4