Executing command-line immediately from Xcode — Erica Sadun


I received pulled into a kind of conversations the place I find yourself saying, “Positive, I’ll put up a submit about it” and that is the submit. Sure, you can take a look at and run command-line apps immediately from Xcode however I just about by no means do. It’s a ache with few advantages. That mentioned, right here’s the way you do it.

Arguments

Let’s say you want arguments. Open your scheme (⌘<) and choose the Run > Arguments tab. Add the arguments you wish to go on launch separately. Double-click to edit any argument:

The arguments are vended byCommandLine.arguments. Both rely the array or use CommandLine.argc to learn the way many arguments you’re coping with.

print(CommandLine.arguments)
print(CommandLine.argc)

Counter-intuitively, Xcode doesn’t robotically quote the arguments for you. This produces 5 arguments, not three, or six for those who embody the command itself:

["/Users/ericasadun/Library/Developer/Xcode/DerivedData/Test-gwehknnihlcsiucsovtbnlrdtfun/Build/Products/Debug/Test", "first", "second", "third", "fourth", "fifth"]
6

And what do you anticipate from the next?

You get this for those who run immediately in Xcode’s console:

["/Users/ericasadun/Library/Developer/Xcode/DerivedData/Test-gwehknnihlcsiucsovtbnlrdtfun/Build/Products/Debug/Test", "first", "several items at once", "third"]
4
Program ended with exit code: 0

However for those who set your code to execute utilizing Terminal:

Launching: '/Customers/ericasadun/Library/Developer/Xcode/DerivedData/Check-gwehknnihlcsiucsovtbnlrdtfun/Construct/Merchandise/Debug/Check'
Working listing: '/Customers/ericasadun/Library/Developer/Xcode/DerivedData/Check-gwehknnihlcsiucsovtbnlrdtfun/Construct/Merchandise/Debug'
3 arguments:
argv[0] = '/Customers/ericasadun/Library/Developer/Xcode/DerivedData/Check-gwehknnihlcsiucsovtbnlrdtfun/Construct/Merchandise/Debug/Check'
argv[1] = 'first'
argv[2] = 'a number of'
["/Users/ericasadun/Library/Developer/Xcode/DerivedData/Test-gwehknnihlcsiucsovtbnlrdtfun/Build/Products/Debug/Test", "first", "several"]
3

Xcode’s Loopy Terminal Possibility

For those who’re working something with direct key enter (utilizing POSIX termios/uncooked mode) or curses, working within the console doesn’t work. So Xcode gives a strategy to run these utilities within the terminal. Go to Run > Choices and scroll all the way in which down.

This function is buggy as hell, produces ridiculous quantities of extra textual content (see this), can take a big time to launch, and much more time for Xcode to comprehend the method has completed. It’s unattainable to make use of with paths that use areas (“warning: working listing does not exist: '/Volumes/Kiku/Xcode/Derived'“).

I don’t prefer it. I don’t use it. However it exists.

Sane Command-Line Execution

Until you’re coping with issues like automation and such, you possibly can check out your compiled command-line apps by dragging your executable from the Merchandise group onto the terminal. This locations the trail to your construct on the immediate. Kind out your arguments and press return:

Nonetheless, I desire to make use of a Copy File construct section. Choose your Goal > Construct Phases, click on plus (+) and add the executable. (I exploit absolute path and disable “solely when putting in”.) This allows you to set up on to  commonplace areas like /usr/native/bin or ~/bin, or for those who don’t wish to place it there till it’s steady and prepared for deployment, you should utilize a growth folder:

Assuming your vacation spot is in your shell’s path, begin a brand new shell for the executable to be picked up the primary time. After that, you possibly can compile and run as you want.

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox