Can ArgumentParser just relax and ignore unknown arguments?

For an odd use case, I'm developing a macOS application (yes, a .app) which uses the Swift package ArgumentParser.

When launching an app, macOS inserts the command-line argument -NSDocumentRevisionsDebugMode YES. I do not need the value of this argument, but my ParsableCommand must declare it as an @Option anyhow. Otherwise, my app prints to the console Error: Unknown option … Usage: …, and then quits.

This -NSDocumentRevisionsDebugMode YES was not in macOS app launches 20 years ago. If in the future Apple adds some other weird argument to app launching, my app will break.

Is there any way to tell ArgumentParser to just relax and ignore unknown arguments?

1 Like

You may want to look at the allUnrecognized parameter for capturing any unknown arguments.

https://swiftpackageindex.com/apple/swift-argument-parser/1.3.0/documentation/argumentparser/argumentarrayparsingstrategy/allunrecognized

5 Likes

Thank you, @Pippin. Clearly your answer is the correct, because the documentation there says:

You can use the allUnrecognized parsing strategy to suppress “unexpected argument” errors or …

When I try this, I get a complier error Type 'ArgumentArrayParsingStrategy' has no member 'allUnrecognized'. Maybe I have got an old version of ArgumentParser somehow. This is my first time using a Swift Package, so I shall look elsewhere for how to fix that.

3 Likes

Just to confirm, after I got the current version of ArgumentParser resolved into my project, allUnrecognized does what I need.