Skip to content

Commit b0f7ecd

Browse files
committed
User manual improvements
1 parent 79e57dd commit b0f7ecd

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

docs/index.adoc

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
= picocli - a mighty tiny command line interface
22
//:author: Remko Popma
33
4-
:revnumber: 1.1.0-SNAPSHOT
5-
:revdate: 2017-08-26
4+
:revnumber: 1.0.1
5+
:revdate: 2017-08-29
66
:toc: left
77
:numbered:
88
:toclevels: 2
@@ -17,12 +17,14 @@ image::https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png[Fork
1717
Every main method deserves picocli!
1818

1919
== Introduction
20-
Picocli is a one-file Java framework for parsing command line arguments
21-
and generating polished, easily tailored usage help messages. With <<ANSI Colors and Styles,colors>>.
20+
Picocli is a one-file framework for creating Java command line applications with almost zero code.
21+
Supports a variety of command line syntax styles including POSIX, GNU, MS-DOS and more.
22+
Generates highly customizable usage help messages with <<ANSI Colors and Styles,ANSI colors and styles>>.
23+
Picocli-based applications can have link:autocomplete.html[command line TAB completion] showing available options, option parameters and subcommands, for any level of nested subcommands.
2224

2325
image:ExampleUsageANSI.png[Screenshot of usage help with Ansi codes enabled]
2426

25-
Now in BETA: link:autocomplete.html[command line autocompletion]. (Comments, bug reports, pull requests welcome!)
27+
link:autocomplete.html[Command line autocompletion] is in BETA. Comments, bug reports, pull requests welcome!
2628

2729

2830
A distinguishing feature of picocli is how it aims
@@ -58,13 +60,14 @@ assert app.verbose;
5860
assert app.inputFiles != null && app.inputFiles.length == 2;
5961
----
6062

61-
Here is a small example application:
63+
Here is a small example application that uses the `CommandLine.call` <<Less Boilerplate,convenience method>>
64+
to do parsing and error handling in one line of code.
6265

6366
[[CheckSum-application]]
6467
[source,java]
6568
----
6669
@Command(name = "checksum", description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
67-
class CheckSum {
70+
class CheckSum implements Callable<Void> {
6871
6972
@Parameters(index = "0", description = "The file whose checksum to calculate.")
7073
private File file;
@@ -76,34 +79,28 @@ class CheckSum {
7679
private boolean helpRequested;
7780
7881
public static void main(String[] args) throws Exception {
79-
CheckSum checkSum = new CheckSum();
80-
try {
81-
new CommandLine(checkSum).parse(args);
82-
} catch (ParameterException ex) { // handle invalid input
83-
System.err.println(ex.getMessage());
84-
CommandLine.usage(checkSum, System.err);
85-
return;
86-
}
87-
checkSum.call();
82+
// CheckSum implements Callable,
83+
// so parsing and error handling can be done in one line of code
84+
CommandLine.call(new CheckSum(), System.err, args);
8885
}
8986
90-
public void call() throws Exception {
87+
@Override
88+
public Void call() throws Exception {
89+
// business logic: do different things depending on options the user specified
9190
if (helpRequested) {
9291
CommandLine.usage(this, System.err);
93-
return;
92+
return null;
9493
}
9594
byte[] digest = MessageDigest.getInstance(algorithm).digest(readBytes(file));
9695
print(digest, System.out);
96+
return null;
9797
}
9898
9999
byte[] readBytes(File f) throws IOException {...}
100100
void print(byte[] digest, PrintStream out) {...}
101101
}
102102
----
103103

104-
NOTE: You may also be interested in the `CommandLine.run` and `CommandLine.call` <<Less Boilerplate,convenience methods>>
105-
for parsing that take care of handling invalid command line arguments.
106-
107104

108105
== Options and Parameters
109106
Command line arguments can be separated into _options_ and _positional parameters_.
@@ -1681,27 +1678,27 @@ See the <<Source,source code>> below. Copy and paste it into a file called `Comm
16811678

16821679
=== Gradle
16831680
----
1684-
compile 'info.picocli:picocli:1.1.0-SNAPSHOT'
1681+
compile 'info.picocli:picocli:1.0.1'
16851682
----
16861683

16871684
=== Maven
16881685
----
16891686
<dependency>
16901687
<groupId>info.picocli</groupId>
16911688
<artifactId>picocli</artifactId>
1692-
<version>1.1.0-SNAPSHOT</version>
1689+
<version>1.0.1</version>
16931690
</dependency>
16941691
----
16951692

16961693
=== Scala SBT
16971694
----
1698-
libraryDependencies += "info.picocli" % "picocli" % "1.1.0-SNAPSHOT"
1695+
libraryDependencies += "info.picocli" % "picocli" % "1.0.1"
16991696
----
17001697

17011698
=== Ivy
17021699

17031700
----
1704-
<dependency org="info.picocli" name="picocli" rev="1.1.0-SNAPSHOT" />
1701+
<dependency org="info.picocli" name="picocli" rev="1.0.1" />
17051702
----
17061703

17071704
=== Source

src/test/java/picocli/Demo.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -667,21 +667,24 @@ public void testUsageSubCommandCommit() {
667667
@Command(name = "checksum", description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
668668
class CheckSum implements Callable<Void> {
669669

670-
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Show this help message and exit.")
671-
private boolean helpRequested;
670+
@Parameters(index = "0", description = "The file whose checksum to calculate.")
671+
private File file;
672672

673673
@Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
674674
private String algorithm = "MD5";
675675

676-
@Parameters(index = "0", description = "The file whose checksum to calculate.")
677-
private File file;
676+
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Show this help message and exit.")
677+
private boolean helpRequested;
678678

679679
public static void main(String[] args) throws Exception {
680+
// CheckSum implements Callable,
681+
// so parsing and error handling can be done in one line of code
680682
CommandLine.call(new CheckSum(), System.err, args);
681683
}
682684

683685
@Override
684686
public Void call() throws Exception {
687+
// business logic: do different things depending on options the user specified
685688
if (helpRequested) {
686689
CommandLine.usage(this, System.err);
687690
return null;
@@ -695,9 +698,13 @@ byte[] readBytes(File f) throws IOException {
695698
int pos = 0;
696699
int len = 0;
697700
byte[] buffer = new byte[(int) f.length()];
698-
FileInputStream fis = new FileInputStream(f);
699-
while ((len = fis.read(buffer, pos, buffer.length - pos)) > 0) { pos += len; }
700-
fis.close();
701+
FileInputStream fis = null;
702+
try {
703+
fis = new FileInputStream(f);
704+
while ((len = fis.read(buffer, pos, buffer.length - pos)) > 0) { pos += len; }
705+
} finally {
706+
if (fis != null) { fis.close(); }
707+
}
701708
return buffer;
702709
}
703710
void print(byte[] digest, PrintStream out) {

0 commit comments

Comments
 (0)