So I'm building a Java CLI application that will have features similair to Windows's dir. I'm using the jopt-simple 4.9 library for my CLI parsing needs, and for acquiring options it seems pretty straightforward...
public static void main(String[] args) throws Exception {
OptionParser parser = new OptionParser("a::b?*");
OptionSet options = parser.parse(args);
parser.accepts("a", "Display all");
parser.accepts("b", "Bare output without metadata");
parser.accepts("?", "Displays this help prompt");
But what if I want to run my app without any args? Like I would run dir or ls to display local contents. And what if I want to run my app without any options? Like if I'm just telling dir or ls which directory I want it to print out.
OK, so to achieve this is simply used the fact the JOpt also parses non-option arguments:
OptionParser parser = new OptionParser("a::b?*");
parser.allowsUnrecognizedOptions();
// In case the user specifies a path instead of just running the command
// locally, create an array out of the parsed directory strings.
String[] dirStringArray = options.nonOptionArguments().toArray(new String[options.nonOptionArguments().size()]);
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.