简体   繁体   中英

Apache CLI alternative with whitespace and quoting support

In one of my largest project, I've been using org.apache.commons.cli.CommandLineParser for quite a long time. So long that currently, I have dozens of parameters in multiple exec classes.

Some time ago, I noticed that there is broken support for whitespaces and quoting. Simply said

--parameter="I need something like this"

or sometimes

--parameter="I would even appreciate espacing \"double quotes\""

Accoring to Apache CLI option value with whitespace , this seems like an OS-dependent bug (on my Windows machine, parsing quoted argument works, but on Debian server, it does not).

So my question is: Is there any alternative which is reasonably similar to Apache Commons CLI (so I don't have to do complete rewrite), but supports quoting?

Picocli is an alternative for Commons CLI that is gaining in popularity. It has both a programmatic API and an annotations API .

Picocli has excellent support for parsing quoted command line arguments . Quotes around command line parameters are preserved by default in picocli, but this can be configured with CommandLine::setTrimQuotes . When splitting parameters, quoted strings are not split (this can be configured with CommandLine::setSplitQuotedStrings ).

Example:

@Command(name = "example")
class Example {
    @Option(names = "-x", split = ",")
    String[] parts;
}

If we give this command some input with quoted values:

example -x "-Dvalues=a,b,c","-Dother=1,2"

This results in the parts array having the following values:

"-Dvalues=a,b,c"
"-Dother=1,2"

Other picocli features that may be of interest:

Disclaimer: I maintain picocli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM