diff options
author | Cedric Beust <cedric@beust.com> | 2010-08-05 21:47:20 -0700 |
---|---|---|
committer | Cedric Beust <cedric@beust.com> | 2010-08-05 21:47:20 -0700 |
commit | e6b03e45550ef19dd1067919e8be28f313bc2de7 (patch) | |
tree | c49741ed938ee988a03bff6c8e09f3f63df30360 /doc | |
parent | bd5ce9c45c1c30dfed8a553602c127104f254c9f (diff) | |
download | platform_external_jcommander-e6b03e45550ef19dd1067919e8be28f313bc2de7.tar.gz platform_external_jcommander-e6b03e45550ef19dd1067919e8be28f313bc2de7.tar.bz2 platform_external_jcommander-e6b03e45550ef19dd1067919e8be28f313bc2de7.zip |
Documented commands
Diffstat (limited to 'doc')
-rw-r--r-- | doc/index.html | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/doc/index.html b/doc/index.html index 5420f3e..e376145 100644 --- a/doc/index.html +++ b/doc/index.html @@ -478,6 +478,61 @@ JCommander jc = new JCommander(new Args()); jc.setDefaultProvider(DEFAULT_PROVIDER); </pre> +<h2 id="Complex">More complex syntaxes</h2> + +Complex tools such as <tt>git</tt> or <tt>svn</tt> understand a whole set of commands, each of which with their own specific syntax: + +<pre class="brush: plain"> + git commit --amend -m "Bug fix" +</pre> + +Words such as "commit" above are called "commands" in JCommander, and you can specify them by creating one arg object per command: + +<pre class="brush: java"> +@Parameters(separators = "=") +public class CommandCommit { + + @Parameter(description = "Record changes to the repository") + public List<String> files; + + @Parameter(names = "--amend", description = "Amend") + public Boolean amend = false; + + @Parameter(names = "--author") + public String author; +} +</pre> + +<pre class="brush: java"> +public class CommandAdd { + + @Parameter(description = "Add file contents to the index") + public List<String> patterns; + + @Parameter(names = "-i") + public Boolean interactive = false; +} +</pre> + +Then you register these commands with your JCommander object. After the parsing phase, you call <tt>getParsedCommand()</tt> on your JCommander object, and based on the command that is returned, you know which arg object to inspect (you can still use a main arg object if you want to support options before the first command appears on the command line): + +<pre class="brush: java"> +CommandMain cm = new CommandMain(); +JCommander jc = new JCommander(cm); + +CommandAdd add = new CommandAdd(); +jc.addCommand("add", add); +CommandCommit commit = new CommandCommit(); +jc.addCommand("commit", commit); + +jc.parse("-v", "commit", "--amend", "--author=cbeust", "A.java", "B.java"); + +Assert.assertTrue(cm.verbose); +Assert.assertEquals(jc.getParsedCommand(), "commit"); +Assert.assertTrue(commit.amend); +Assert.assertEquals(commit.author, "cbeust"); +Assert.assertEquals(commit.files, Arrays.asList("A.java", "B.java")); +</pre> <h2 id="Exceptions">Exception</h2> |