4

I have an existing GUI mac app and I'd like to add command line call to it. For example, the GUI app called "Gallifrey" looks up the actor playing Dr. Who and optionally the companion. The GUI has a text field for year and a checkbox for "And companion" and a button to look up. From the command line, I imagine it would be:

> gallifrey -y2014 -c 
> Peter Capaldi, Jenna Coleman

I found this mention of a solution on apple-dev http://lists.apple.com/archives/cocoa-dev/2009/Oct/msg01480.html

Is that still the suggested solution? I considered moving the logic into an XPC service and bundling a separate command line target, but that seems needlessly complicated.

Edit: To be clear, I'm not asking how to parse the args, I've done that, just how to decide between launching the GUI vs just returning the answer.

Mark W.
  • 221
  • 2
  • 9
  • 1
    You can comment out `NSApplicationMain` in main.m and implement the main window launching or not in `ApplicationDidFinishLaunching`. [Answer here](http://stackoverflow.com/questions/8708207/loading-main-window-at-applicationdidfinishlaunching-in-cocoa-application/22852668#22852668)... I'm not sure it's canonical. I guess my suggestion differs from your link in that it would maintain whatever bootstrapping you have in the methods, but just quash the GUI launch. You can have a full-on App w/o a dock icon or menu, so there's no reason not to start the application. – stevesliva Aug 15 '15 at 05:37

2 Answers2

1

Option 2

Edit your main() function and in it decide whether to return an answer instead of launching NSApplicationMain. This way you keep technically one app, one executable, that supports both ways of launching.

Kornel
  • 97,764
  • 37
  • 219
  • 309
0

Option 1

Move all of your app's code into a framework (your own framework in your own app bundle).

Make separate GUI and CLI app targets, and have them both link to the framework with all the required functionality.

  • Linked framework makes everything available in the same process, so there's least amount of code change required, and no XPC to worry about.

  • The downside is that your CLI is not a standalone executable, but needs to live inside the app bundle.

Kornel
  • 97,764
  • 37
  • 219
  • 309