Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make simplified @files JCommander-compatible #573

Closed
triceo opened this issue Dec 20, 2018 · 14 comments
Closed

Make simplified @files JCommander-compatible #573

triceo opened this issue Dec 20, 2018 · 14 comments

Comments

@triceo
Copy link
Contributor

triceo commented Dec 20, 2018

When we did #551 , we forgot about one corner case, and that is empty lines at the end. JCommander accepts files without them, our implementation does not.

@remkop
Copy link
Owner

remkop commented Dec 20, 2018

What is the expected behaviour? Can you provide a failing unit test?

@triceo
Copy link
Contributor Author

triceo commented Dec 20, 2018

Will do.

@remkop
Copy link
Owner

remkop commented Dec 24, 2018

I'm planning to do a picocli 3.9 release end of this week, maybe around December 29. Do you think you can provide a failing unit test before then?

@triceo
Copy link
Contributor Author

triceo commented Dec 25, 2018

@remkop OK, will take a look later today. Thanks for the extra motivation. :-)

@triceo triceo changed the title Support empty line at the end of JCommander-like @file Support no newline at the end of JCommander-like @file Dec 25, 2018
@remkop
Copy link
Owner

remkop commented Dec 25, 2018

Thanks for the PR! I'll take a look ASAP.

@remkop
Copy link
Owner

remkop commented Dec 25, 2018

To be honest I had some trouble understanding what the problem was here...

If I apply the PR in #577 and run the tests, I see the testAtFileSimplified test fail like this:

picocli.CommandLine$UnmatchedArgumentException: Unmatched argument: 

	at picocli.CommandLine$Interpreter.parse(CommandLine.java:7166)
	at picocli.CommandLine$Interpreter.parse(CommandLine.java:7022)
	at picocli.CommandLine.parse(CommandLine.java:824)
	at picocli.CommandLine.populateCommand(CommandLine.java:777)
	at picocli.CommandLineTest.testAtFileSimplified(CommandLineTest.java:2850)

The reason it fails is that the parser finds an empty String as the last argument. The AtFileTestingApp command used in the test only defines named options, and does not define positional parameters. So, if the parser encounters a positional parameter (the empty string) it throws an UnmatchedArgumentException.

This seemed perfectly fine to me, and I did not understand what the problem was until I looked at the JCommander source. It appears that JCommander drops empty lines in argument files (@-files). This means it is not possible to specify empty string parameters in this format.

So if the goal is to make "simplified at files" behaviour compatible with JCommander, we should drop all empty lines, not just the trailing empty lines. Is this what you have in mind?

@triceo
Copy link
Contributor Author

triceo commented Dec 25, 2018

@remkop If you're saying that the behavior applies to all empty lines, then I agree that it makes sense to remove them as well.

(For context: I did not experience this issue myself. It was reported by one of my users, who used an old JCommander at-file with a newer version of my application, now using Picocli. And the problem the user demonstrated was the trailing empty line.)

@remkop
Copy link
Owner

remkop commented Dec 25, 2018

Ok, I’ll change the behaviour of simplified at files to:

  • ignore all empty lines
  • trim the line before checking if it’s a comment line (starting with `#)

That will make the behaviour compatible with JCommander.

I’m also thinking to introduce API for this:

  • CommandLine.isUseSimplifiedAtFiles
  • CommandLine.setUseSimplifiedAtFiles
  • ParserSpec.useSimplifiedAtFiles getter
  • ParserSpec.useSimplifiedAtFiles setter

It may make sense to keep the system property but in the documentation mention the API instead and remove mentions of the system property.

Thoughts?

@remkop remkop changed the title Support no newline at the end of JCommander-like @file Make simplified @files JCommander-compatible Dec 25, 2018
@triceo
Copy link
Contributor Author

triceo commented Dec 26, 2018

@remkop Sounds good. I appreciate the new API - I never really liked setting things through system properties.

@remkop remkop closed this as completed in 9624b6c Dec 31, 2018
@remkop
Copy link
Owner

remkop commented Dec 31, 2018

The new parsing behaviour that ignores empty lines and comments with leading whitespace has been pushed to master. Thanks again for the PR with the test.

@remkop
Copy link
Owner

remkop commented Dec 31, 2018

The additional API to control simplified @file format has been pushed to master.
Please verify if you have a chance.

@triceo
Copy link
Contributor Author

triceo commented Dec 31, 2018

@remkop I built the latest master a couple minutes ago and verified that the feature works for my use case.

@remkop
Copy link
Owner

remkop commented Dec 31, 2018

Great, thanks for the confirmation!

@triceo
Copy link
Contributor Author

triceo commented Dec 31, 2018

@remkop Thanks for fixing the issue. Looking forward to 3.9.0!

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

No branches or pull requests

2 participants