10.4 C
Canberra
Friday, September 20, 2024

Executing command-line straight from Xcode — Erica Sadun


I obtained pulled into a type of conversations the place I find yourself saying, “Effective, 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 stated, 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 need to move 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 how many arguments you’re coping with.

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

Counter-intuitively, Xcode doesn’t routinely quote the arguments for you. This produces 5 arguments, not three, or six in the event you embrace 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 count on from the next?

You get this in the event you run straight 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 in the event you 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 Choice

When 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 approach to run these utilities within the terminal. Go to Run > Choices and scroll all the best way down.

This characteristic 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 appreciate the method has completed. It’s unattainable 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’ll be able to 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:

Nevertheless, 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  normal places like /usr/native/bin or ~/bin, or in the event you don’t need to place it there till it’s secure and prepared for deployment, you need to 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’ll be able to compile and run as you want.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles