2

I'm about to start a new Cocoa project from scratch and one of the requirements I have already been given is being able to choose either a Cocoa GUI or a command line mode from the same executable. Scriptability may be a possible addition in the future but that is not a concern right now.

What is the best strategy for going about this in Cocoa, especially if I want to maintain certain Cocoa features like Obj-C garbage collection?

Anonymous
  • 1,750
  • 3
  • 16
  • 21

1 Answers1

4

Xcode will generate a main.m for you with a main function that has the command line arguments. You should parse them and if you need to stay in command-line mode, never do the application startup. If you are being a GUI, just start normally.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Is there a way to get this functionality for a Swift-only project that has no main.m? – todd412 Jan 28 '19 at 04:29
  • I haven't checked it, but look at `CommandLine.arguments` in the app delegate's didFinishLaunching or willFinishLaunching -- you should be able to exit after handling the arguments if present. – Lou Franco Jan 28 '19 at 14:27