Difference between `parse` and `parseAsRoot`

As the title said, what's the difference between ParsableCommand.parse and .parseAsRoot? Rather, when to use which.

It seems to be doing the same thing for top-level command, but parse throws error when invoking subcommand.

parse() returns Self, so it only works on a standalone command or a ParsableArguments type. If you try to call it with inputs that would return a subcommand, it fails.

parseAsRoot() returns a type-erased ParsableCommand instance, so it can return the root command, a subcommand, or even an internal command type. You'll need to cast the result to a specific type if you want to do much more than call run() on it.

Regarding when to use which — if you're trying to parse a ParsableArguments type, parse() is your only option. For commands you should generally use the other.

1 Like

The Manual Parsing and Testing document might help. Please let us know if it needs more details.

2 Likes