-
Notifications
You must be signed in to change notification settings - Fork 0
LS Example
Massimiliano Ziccardi edited this page Jan 19, 2017
·
15 revisions
In this document. we are going to describe how to create a command line by using the ls
example (the example syntax has been taken from the commons-cli documentation.
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.
-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information)
with -l: show ctime and sort by name
otherwise: sort by ctime
-C list entries by columns
The following code can be used to parse this command line with YACLP:
Parser parser = ParserBuilder
.forNewParser()
.withOption(
OptionBuilder
.forOption("-a", "--all")
.description("do not hide entries starting with .")
.build())
.withOption(
OptionBuilder
.forOption("-A", "--almost-all")
.description("do not list implied . and ..")
.build()
)
.withOption(
OptionBuilder
.forOption("-b", "--escape")
.description("print octal escapes for nongraphic characters")
.build()
)
.withOption(
OptionBuilder
.forOption("--block-size", "--block-size")
.description("use SIZE-byte blocks")
.argument(ArgumentBuilder.forArgument("SIZE").build())
.build()
)
.withOption(
OptionBuilder
.forOption("-B", "--ignore-backups")
.description("do not list implied entried ending with ~")
.build()
)
.withOption(
OptionBuilder
.forOption("-c", "-c")
.description("with -lt: sort by, and show, ctime (time of last "
+ "modification of file status information) with "
+ "-l:show ctime and sort by name otherwise: sort "
+ "by ctime")
.build()
)
.withOption(
OptionBuilder
.forOption("-C", "-C")
.description("list entries by columns")
.build()
).build();
String[] args = new String[]{ "--block-size", "10"};
try {
CommandLine line = parser.parse(args);
if (line.hasOption("--block-size")) {
System.out.println (line.getValue("--block-size"));
}
} catch (ParsingException pe) {
System.out.println ("Unexpected exception: " + pe.getMessage());
}
The above code does exactly the same thing as the commons-cli example.
It doesn't make sense to pass both -a
and -A
parameters, so we want to make them mutually exclusive.
That can be done with YACLP
by changing the code:
.withOption(
OptionBuilder
.forOption("-a", "--all")
.description("do not hide entries starting with .")
.build())
.withOption(
OptionBuilder
.forOption("-A", "--almost-all")
.description("do not list implied . and ..")
.build()
)
to
.withOption(
OptionBuilder
.forMutuallyExclusiveOption()
.withOption(
OptionBuilder
.forOption("-a", "--all")
.description("do not hide entries starting with .")
.build()
)
.withOption(
OptionBuilder
.forOption("-A", "--almost-all")
.description("do not list implied . and ..")
.build()
)
.build()
)