folly icon indicating copy to clipboard operation
folly copied to clipboard

Fix "--" parsing in NestedCommandLineApp

Open Sanjay-Ganeshan opened this issue 2 years ago • 2 comments

Summary: Adjusts the parsing order so that folly doesn't incorrectly consume multiple "--" arguments.

-- in unix

Folly is a facebook library for many common C++ operations. It's open-source.

"--" is a unix convention (example). It means: Anything after "--" should be parsed as a positional argument. This allows you to nest CLIs together.

For example "buck run" is a CLI, with various options. The binary that is built may have options. When you want to pass something to the underlying binary, rather than buck run, you can use "--":

buck run //my:binary --buck-option 1 -- --mybinary-option 2

This prevents buck from accidentally thinking "--mybinary-option" is an arg that should be parsed by the buck binary.

One such positional argument that can occur is "--" itself. In this case, the first "--" tells the binary to treat every subsequent arg as a positional arg, and the next "--" is interpreted as a positional arg:

buck run -- --wrapper-option -- --inner-option

The positional args here are: ["--wrapper-option", "--", "--inner-option"]

This allows for an arbitrary number of wrappers to be chained together.

What's wrong / The Fix

The current logic will never allow a "--" to be passed as a positional argument, as it does not consider if it's already seen a "--". We adjust the order to:

  1. If we've already seen "--" ALWAYS treat the arg as a positional arg
  2. If it's the first "--", consume it. All subsequent args are positional.
  3. If we haven't yet seen "--", it should be included when looking for options.

That way, we don't accidentally delete a "--" that's intended for an underlying parser.

Reviewed By: simpkins

Differential Revision: D44892191

Sanjay-Ganeshan avatar Apr 12 '23 14:04 Sanjay-Ganeshan

This pull request was exported from Phabricator. Differential Revision: D44892191

facebook-github-bot avatar Apr 12 '23 14:04 facebook-github-bot