Tuesday, June 25, 2024
HomeIOS DevelopmentExecuting command-line straight from Xcode — Erica Sadun

Executing command-line straight from Xcode — Erica Sadun

I received pulled into a type of conversations the place I find yourself saying, “Positive, I’ll put up a publish about it” and that is the publish. Sure, you can take a look at and run command-line apps straight 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.


Let’s say you want arguments. Open your scheme (⌘<) and choose the Run > Arguments tab. Add the arguments you need to move on launch one after the other. 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.


Counter-intuitively, Xcode doesn’t mechanically quote the arguments for you. This produces 5 arguments, not three, or six if you happen to embody the command itself:

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

And what do you count on from the next?

You get this if you happen to run straight in Xcode’s console:

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

However if you happen to set your code to execute utilizing Terminal:

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

Xcode’s Loopy Terminal Choice

In case you’re operating something with direct key enter (utilizing POSIX termios/uncooked mode) or curses, operating within the console doesn’t work. So Xcode offers 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 major time to launch, and much more time for Xcode to comprehend the method has completed. It’s inconceivable to make use of with paths that use areas (“warning: working listing would not exist: '/Volumes/Kiku/Xcode/Derived'“).

I don’t prefer it. I don’t use it. But it surely exists.

Sane Command-Line Execution

Until you’re coping with issues like automation and such, you may 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. Sort out your arguments and press return:

Nonetheless, I want 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  normal areas like /usr/native/bin or ~/bin, or if you happen to don’t need to place it there till it’s steady and prepared for deployment, you should use 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 may compile and run as you want.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments