remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
https://picocli.info
Apache License 2.0
4.92k stars 424 forks source link

How to parse single string, not string array? #324

Closed saaadel closed 6 years ago

saaadel commented 6 years ago

[just question] Can I use picocli to parse single String, not String array? For example, I have system property that contains all args with spaces, quotes and escaped quotes there. How to parse it?

remkop commented 6 years ago

There is work in progress (see #293) on a tokenizer that splits up a single string into an array of command line arguments, which can then be processed with the CommandLine class as usual.

Can you try using the CommandTokenizer class in #293 and see if it does what you need? (Feedback very welcome.)

remkop commented 6 years ago

Did this answer your question?

saaadel commented 6 years ago

I need two days to check this

saaadel commented 6 years ago

Ok, I've reviewed this PR and ran some tests. It works like ArgumentTokenizer class from DrJava project.

Feedback on your implementation:

Conclusion: DrJava's class works better now. Please, fix issues.

PS. anyway, I'm searching for something with escapes in windows notation too (with caret symbol. Example: ^" escaped quote) - in separate method or in the same

saaadel commented 6 years ago

also, why you have two methods with separate implementation? what needs? new CommandTokenizer().parse(str) new CommandTokenizer().parse(scanner)

I mean, why you not replaced parse(str) with short code: parse(new Scanner(cmd)) ?

saaadel commented 6 years ago

Closing the question, temporary imported the ArgumentTokenizer class, but plan to switch your version, when you will publish it in maven with that PR and with fixed issues.

remkop commented 6 years ago

Cool. Thanks for reviewing the CommandTokenizer class!

ibalashov commented 4 years ago

Sorry to revive the old thread, but was the CommandTokenizer finally published? Somehow I can't locate it in the latest Maven release.

remkop commented 4 years ago

No, it wasn’t. Instead, we decided to add two modules, picocli-shell-jline2 and picocli-shell-jline3, and leverage the tokenizer built in to JLine.

ibalashov commented 4 years ago

@remkop Thanks!

However, I don't see any relevance of jline to solve the problem of this thread, i.e. parsing the full text command into the arguments. Do I miss something? 😕 It looks jline is about interactive command input only.

Just to clarify my use case: I want to use picocli to parse a raw user command which comes as a single text line. Does picocli provide this, or should I use some external tool to parse it to the list of arguments?

remkop commented 4 years ago

Picocli does not provide functionality to parse a single String into separate command arguments. See also https://github.com/remkop/picocli/issues/242 for more discussion and background.

The reason I mention JLine is that this library does provide it in its DefaultParser class.