Skip to content

Conversation

@cstamas
Copy link
Member

@cstamas cstamas commented Jun 9, 2025

Goal is to introduce new "ops" for CLI parser: a mode when unknown options are completely ignored or just skipped (added to args). At the same time encapsulation is loosened as well, by making relevant methods protected to allow possible override/extension of DefaultParser class.


Simpler variation of #378

Goal: to "postpone" full argument parsing. To be used in Maven. We introduced new "cli commands" aside of existing mvn, like mvnenc, mvnsh and mvnup.

The "base" options (all CLI command) are these:
https://github.com/apache/maven/blob/77ebd14d1580e0a67c5a929afdd3ac62a3bfea1f/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/Options.java

And each command extends this base with specific options, for example:
https://github.com/apache/maven/blob/77ebd14d1580e0a67c5a929afdd3ac62a3bfea1f/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/MavenOptions.java
https://github.com/apache/maven/blob/77ebd14d1580e0a67c5a929afdd3ac62a3bfea1f/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvnenc/EncryptOptions.java
https://github.com/apache/maven/blob/77ebd14d1580e0a67c5a929afdd3ac62a3bfea1f/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvnup/UpgradeOptions.java

Now the goal would be to move these commands (currently "burned into Maven core") into dynamically resolved extensions. Original idea is to "boot Maven DI base", figure out which command extension is wanted, and then once given "tool" loaded, have it parse the "remainder" specific arguments (that are unknown and start, the options depend on the tool being loaded).

tl;dr: we want to be able to parse "common" base set of options, that are same across multiple commands, then load command dynamically (Maven would resolve and load it into DI like any other extension), and then let the loaded command parse the "remainder" (own specific subset) or arguments.

tl;dr more short: to be able to "chain" arg parsing from "generic" to more "specific" (and unknown which specific at start).

@cstamas cstamas changed the title Make method possible override Make method possible to override Jun 9, 2025
@cstamas cstamas marked this pull request as ready for review June 9, 2025 17:04
Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cstamas

Thank you for the PR 😄

  • You'll need to add at least one unit test to validate the behavior of the new method. Otherwise, a future change might blow up the expected use case.
  • The now protected method needs a Javadoc @since tag.

@cstamas
Copy link
Member Author

cstamas commented Jun 9, 2025

Thanks for checking it out. Pushed tests and some more changes.

cstamas added 2 commits June 9, 2025 21:06
when there was one boolean doing this or that only
@garydgregory
Copy link
Member

I'll review/merge and so on in a few hours...

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cstamas

I'm somewhat confused - unsurprising 😉 - as to what some combinations mean.

I think it would help users to document what happens in the new matrix:

stopAtNonOption throwAtNonOption Means
true true ?
true false ?
false true ?
false false ?

The current docs don't say what overrides what if anything.

TY!

As not all combos make sense.
@cstamas
Copy link
Member Author

cstamas commented Jun 10, 2025

I'm somewhat confused - unsurprising 😉 - as to what some combinations mean.

I got too, as IMHO not all combos of booleans made sense, so I switched to enum.

* @throws ParseException if there are any problems encountered while parsing the command line tokens.
* @since 1.10.0
*/
public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final NonOptionAction nonOptionAction)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add an API, this could be an opportunity to simplify call sites to use variable arguments:

public CommandLine parse(final Options options, final Properties properties, final NonOptionAction nonOptionAction, final String... arguments)

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable.

@cstamas cstamas changed the title Make method possible to override Add new options for parsing: ignore and skip Jun 10, 2025
Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cstamas

Thank you for your update :-)

I don't see tests for the enum value STOP but we see new tests that use the other enum values.

TY

@cstamas
Copy link
Member Author

cstamas commented Jun 10, 2025

I don't see tests for the enum value STOP but we see new tests that use the other enum values.

The new legacyStopAtNonOption uses it implicitly.

@garydgregory garydgregory merged commit b1f015e into apache:master Jun 10, 2025
9 checks passed
@garydgregory
Copy link
Member

TY @cstamas, merged!

@garydgregory
Copy link
Member

@cstamas

When do you need a release?

Is there anything else you'd like to contribute before that?

TY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants