blob: 8975316d639aa09cb443aa70b364b486a7815813 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.5">
<meta name="author" content="Cédric Beust">
<title>JCommander</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Varela+Round|Open+Sans:400italic,600italic,400,600|Ubuntu+Mono:400);
/*! normalize.css v2.1.2 | MIT License | git.io/normalize */
/* ========================================================================== HTML5 display definitions ========================================================================== */
/** Correct `block` display not defined in IE 8/9. */
article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; }
/** Correct `inline-block` display not defined in IE 8/9. */
audio, canvas, video { display: inline-block; }
/** Prevent modern browsers from displaying `audio` without controls. Remove excess height in iOS 5 devices. */
audio:not([controls]) { display: none; height: 0; }
/** Address `[hidden]` styling not present in IE 8/9. Hide the `template` element in IE, Safari, and Firefox < 22. */
[hidden], template { display: none; }
script { display: none !important; }
/* ========================================================================== Base ========================================================================== */
/** 1. Set default font family to sans-serif. 2. Prevent iOS text size adjust after orientation change, without disabling user zoom. */
html { font-family: sans-serif; /* 1 */ -ms-text-size-adjust: 100%; /* 2 */ -webkit-text-size-adjust: 100%; /* 2 */ }
/** Remove default margin. */
body { margin: 0; }
/* ========================================================================== Links ========================================================================== */
/** Remove the gray background color from active links in IE 10. */
a { background: transparent; }
/** Address `outline` inconsistency between Chrome and other browsers. */
a:focus { outline: thin dotted; }
/** Improve readability when focused and also mouse hovered in all browsers. */
a:active, a:hover { outline: 0; }
/* ========================================================================== Typography ========================================================================== */
/** Address variable `h1` font-size and margin within `section` and `article` contexts in Firefox 4+, Safari 5, and Chrome. */
h1 { font-size: 2em; margin: 0.67em 0; }
/** Address styling not present in IE 8/9, Safari 5, and Chrome. */
abbr[title] { border-bottom: 1px dotted; }
/** Address style set to `bolder` in Firefox 4+, Safari 5, and Chrome. */
b, strong { font-weight: bold; }
/** Address styling not present in Safari 5 and Chrome. */
dfn { font-style: italic; }
/** Address differences between Firefox and other browsers. */
hr { -moz-box-sizing: content-box; box-sizing: content-box; height: 0; }
/** Address styling not present in IE 8/9. */
mark { background: #ff0; color: #000; }
/** Correct font family set oddly in Safari 5 and Chrome. */
code, kbd, pre, samp { font-family: monospace, serif; font-size: 1em; }
/** Improve readability of pre-formatted text in all browsers. */
pre { white-space: pre-wrap; }
/** Set consistent quote types. */
q { quotes: "\201C" "\201D" "\2018" "\2019"; }
/** Address inconsistent and variable font size in all browsers. */
small { font-size: 80%; }
/** Prevent `sub` and `sup` affecting `line-height` in all browsers. */
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }
/* ========================================================================== Embedded content ========================================================================== */
/** Remove border when inside `a` element in IE 8/9. */
img { border: 0; }
/** Correct overflow displayed oddly in IE 9. */
svg:not(:root) { overflow: hidden; }
/* ========================================================================== Figures ========================================================================== */
/** Address margin not present in IE 8/9 and Safari 5. */
figure { margin: 0; }
/* ========================================================================== Forms ========================================================================== */
/** Define consistent border, margin, and padding. */
fieldset { border: 1px solid #c0c0c0; margin: 0 2px; padding: 0.35em 0.625em 0.75em; }
/** 1. Correct `color` not being inherited in IE 8/9. 2. Remove padding so people aren't caught out if they zero out fieldsets. */
legend { border: 0; /* 1 */ padding: 0; /* 2 */ }
/** 1. Correct font family not being inherited in all browsers. 2. Correct font size not being inherited in all browsers. 3. Address margins set differently in Firefox 4+, Safari 5, and Chrome. */
button, input, select, textarea { font-family: inherit; /* 1 */ font-size: 100%; /* 2 */ margin: 0; /* 3 */ }
/** Address Firefox 4+ setting `line-height` on `input` using `!important` in the UA stylesheet. */
button, input { line-height: normal; }
/** Address inconsistent `text-transform` inheritance for `button` and `select`. All other form control elements do not inherit `text-transform` values. Correct `button` style inheritance in Chrome, Safari 5+, and IE 8+. Correct `select` style inheritance in Firefox 4+ and Opera. */
button, select { text-transform: none; }
/** 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` and `video` controls. 2. Correct inability to style clickable `input` types in iOS. 3. Improve usability and consistency of cursor style between image-type `input` and others. */
button, html input[type="button"], input[type="reset"], input[type="submit"] { -webkit-appearance: button; /* 2 */ cursor: pointer; /* 3 */ }
/** Re-set default cursor for disabled elements. */
button[disabled], html input[disabled] { cursor: default; }
/** 1. Address box sizing set to `content-box` in IE 8/9. 2. Remove excess padding in IE 8/9. */
input[type="checkbox"], input[type="radio"] { box-sizing: border-box; /* 1 */ padding: 0; /* 2 */ }
/** 1. Address `appearance` set to `searchfield` in Safari 5 and Chrome. 2. Address `box-sizing` set to `border-box` in Safari 5 and Chrome (include `-moz` to future-proof). */
input[type="search"] { -webkit-appearance: textfield; /* 1 */ -moz-box-sizing: content-box; -webkit-box-sizing: content-box; /* 2 */ box-sizing: content-box; }
/** Remove inner padding and search cancel button in Safari 5 and Chrome on OS X. */
input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; }
/** Remove inner padding and border in Firefox 4+. */
button::-moz-focus-inner, input::-moz-focus-inner { border: 0; padding: 0; }
/** 1. Remove default vertical scrollbar in IE 8/9. 2. Improve readability and alignment in all browsers. */
textarea { overflow: auto; /* 1 */ vertical-align: top; /* 2 */ }
/* ========================================================================== Tables ========================================================================== */
/** Remove most spacing between table cells. */
table { border-collapse: collapse; border-spacing: 0; }
meta.foundation-mq-small { font-family: "only screen and (min-width: 768px)"; width: 768px; }
meta.foundation-mq-medium { font-family: "only screen and (min-width:1280px)"; width: 1280px; }
meta.foundation-mq-large { font-family: "only screen and (min-width:1440px)"; width: 1440px; }
*, *:before, *:after { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
html, body { font-size: 100%; }
body { background: #fefdfd; color: rgba(0, 0, 0, 0.8); padding: 0; margin: 0; font-family: "Open Sans", sans-serif; font-weight: normal; font-style: normal; line-height: 1; position: relative; cursor: auto; }
a:hover { cursor: pointer; }
img, object, embed { max-width: 100%; height: auto; }
object, embed { height: 100%; }
img { -ms-interpolation-mode: bicubic; }
#map_canvas img, #map_canvas embed, #map_canvas object, .map_canvas img, .map_canvas embed, .map_canvas object { max-width: none !important; }
.left { float: left !important; }
.right { float: right !important; }
.text-left { text-align: left !important; }
.text-right { text-align: right !important; }
.text-center { text-align: center !important; }
.text-justify { text-align: justify !important; }
.hide { display: none; }
.antialiased { -webkit-font-smoothing: antialiased; }
img { display: inline-block; vertical-align: middle; }
textarea { height: auto; min-height: 50px; }
select { width: 100%; }
object, svg { display: inline-block; vertical-align: middle; }
.center { margin-left: auto; margin-right: auto; }
.spread { width: 100%; }
p.lead, .paragraph.lead > p, #preamble > .sectionbody > .paragraph:first-of-type p { font-size: 1.21875em; line-height: 1.6; }
.subheader, .admonitionblock td.content > .title, .audioblock > .title, .exampleblock > .title, .imageblock > .title, .listingblock > .title, .literalblock > .title, .stemblock > .title, .openblock > .title, .paragraph > .title, .quoteblock > .title, table.tableblock > .title, .verseblock > .title, .videoblock > .title, .dlist > .title, .olist > .title, .ulist > .title, .qlist > .title, .hdlist > .title { line-height: 1.25; color: #002c5e; font-weight: 300; margin-top: 0.2em; margin-bottom: 0.5em; }
/* Typography resets */
div, dl, dt, dd, ul, ol, li, h1, h2, h3, #toctitle, .sidebarblock > .content > .title, h4, h5, h6, pre, form, p, blockquote, th, td { margin: 0; padding: 0; direction: ltr; }
/* Default Link Styles */
a { color: #005580; text-decoration: underline; line-height: inherit; }
a:hover, a:focus { color: #078d71; }
a img { border: none; }
/* Default paragraph styles */
p { font-family: inherit; font-weight: normal; font-size: 1em; line-height: 1.5; margin-bottom: 1.25em; text-rendering: optimizeLegibility; }
p aside { font-size: 0.875em; line-height: 1.35; font-style: italic; }
/* Default header styles */
h1, h2, h3, #toctitle, .sidebarblock > .content > .title, h4, h5, h6 { font-family: "Varela Round", sans-serif; font-weight: 400; font-style: normal; color: #00326b; text-rendering: optimizeLegibility; margin-top: 0.8em; margin-bottom: 0.5em; line-height: 1.0625em; }
h1 small, h2 small, h3 small, #toctitle small, .sidebarblock > .content > .title small, h4 small, h5 small, h6 small { font-size: 60%; color: #057aff; line-height: 0; }
h1 { font-size: 2.125em; }
h2 { font-size: 1.6875em; }
h3, #toctitle, .sidebarblock > .content > .title { font-size: 1.375em; }
h4 { font-size: 1.125em; }
h5 { font-size: 1.125em; }
h6 { font-size: 1em; }
hr { border: solid rgba(145, 135, 84, 0.15); border-width: 1px 0 0; clear: both; margin: 1.25em 0 1.1875em; height: 0; }
/* Helpful Typography Defaults */
em, i { font-style: italic; line-height: inherit; }
strong, b { font-weight: bold; line-height: inherit; }
small { font-size: 60%; line-height: inherit; }
code { font-family: "Ubuntu Mono", "Inconsolata", monospace; font-weight: 400; color: #331d00; }
/* Lists */
ul, ol, dl { font-size: 1em; line-height: 1.5; margin-bottom: 1.25em; list-style-position: outside; font-family: inherit; }
ul, ol { margin-left: 1.5em; }
ul.no-bullet, ol.no-bullet { margin-left: 1.5em; }
/* Unordered Lists */
ul li ul, ul li ol { margin-left: 1.25em; margin-bottom: 0; font-size: 1em; /* Override nested font-size change */ }
ul.square li ul, ul.circle li ul, ul.disc li ul { list-style: inherit; }
ul.square { list-style-type: square; }
ul.circle { list-style-type: circle; }
ul.disc { list-style-type: disc; }
ul.no-bullet { list-style: none; }
/* Ordered Lists */
ol li ul, ol li ol { margin-left: 1.25em; margin-bottom: 0; }
/* Definition Lists */
dl dt { margin-bottom: 0.3125em; font-weight: bold; }
dl dd { margin-bottom: 1.25em; }
/* Abbreviations */
abbr, acronym { text-transform: uppercase; font-size: 90%; color: rgba(0, 0, 0, 0.8); border-bottom: 1px dotted #dddddd; cursor: help; }
abbr { text-transform: none; }
/* Blockquotes */
blockquote { margin: 0 0 1.25em; padding: 0.5625em 1.25em 0 1.1875em; border-left: 1px solid #dddddd; }
blockquote cite { display: block; font-size: 0.8125em; color: #666666; }
blockquote cite:before { content: "\2014 \0020"; }
blockquote cite a, blockquote cite a:visited { color: #666666; }
blockquote, blockquote p { line-height: 1.5; color: #999999; }
/* Microformats */
.vcard { display: inline-block; margin: 0 0 1.25em 0; border: 1px solid #dddddd; padding: 0.625em 0.75em; }
.vcard li { margin: 0; display: block; }
.vcard .fn { font-weight: bold; font-size: 0.9375em; }
.vevent .summary { font-weight: bold; }
.vevent abbr { cursor: auto; text-decoration: none; font-weight: bold; border: none; padding: 0 0.0625em; }
@media only screen and (min-width: 768px) { h1, h2, h3, #toctitle, .sidebarblock > .content > .title, h4, h5, h6 { line-height: 1.25; }
h1 { font-size: 2.75em; }
h2 { font-size: 2.3125em; }
h3, #toctitle, .sidebarblock > .content > .title { font-size: 1.6875em; }
h4 { font-size: 1.4375em; } }
/* Tables */
table { background: white; margin-bottom: 1.25em; border: solid 1px rgba(145, 135, 84, 0.15); }
table thead, table tfoot { background: rgba(119, 84, 22, 0.1); font-weight: bold; }
table thead tr th, table thead tr td, table tfoot tr th, table tfoot tr td { padding: 0.5em 0.625em 0.625em; font-size: inherit; color: rgba(0, 0, 0, 0.8); text-align: left; }
table tr th, table tr td { padding: 0.5625em 0.625em; font-size: inherit; color: rgba(0, 0, 0, 0.8); }
table tr.even, table tr.alt, table tr:nth-of-type(even) { background: rgba(119, 84, 22, 0.025); }
table thead tr th, table tfoot tr th, table tbody tr td, table tr td, table tfoot tr td { display: table-cell; line-height: 1.5; }
body { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; tab-size: 4; }
h1, h2, h3, #toctitle, .sidebarblock > .content > .title, h4, h5, h6 { line-height: 1.25; }
.clearfix:before, .clearfix:after, .float-group:before, .float-group:after { content: " "; display: table; }
.clearfix:after, .float-group:after { clear: both; }
*:not(pre) > code { font-size: 1.0625em; font-style: normal !important; letter-spacing: 0; padding: 0; line-height: 1.25; word-wrap: break-word; }
*:not(pre) > code.nobreak { word-wrap: normal; }
*:not(pre) > code.nowrap { white-space: nowrap; }
pre, pre > code { line-height: 1.4; color: inherit; font-family: "Liberation Mono", "Consolas", monospace; font-weight: normal; }
em em { font-style: normal; }
strong strong { font-weight: normal; }
.keyseq { color: rgba(51, 51, 51, 0.8); }
kbd { font-family: "Ubuntu Mono", "Inconsolata", monospace; display: inline-block; color: rgba(0, 0, 0, 0.8); font-size: 0.65em; line-height: 1.45; background-color: #f7f7f7; border: 1px solid #ccc; -webkit-border-radius: 3px; border-radius: 3px; -webkit-box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2), 0 0 0 0.1em white inset; box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2), 0 0 0 0.1em white inset; margin: 0 0.15em; padding: 0.2em 0.5em; vertical-align: middle; position: relative; top: -0.1em; white-space: nowrap; }
.keyseq kbd:first-child { margin-left: 0; }
.keyseq kbd:last-child { margin-right: 0; }
.menuseq, .menu { color: rgba(0, 0, 0, 0.8); }
b.button:before, b.button:after { position: relative; top: -1px; font-weight: normal; }
b.button:before { content: "["; padding: 0 3px 0 2px; }
b.button:after { content: "]"; padding: 0 2px 0 3px; }
p a > code:hover { color: #1a0f00; }
#header, #content, #footnotes, #footer { width: 100%; margin-left: auto; margin-right: auto; margin-top: 0; margin-bottom: 0; max-width: 62.5em; *zoom: 1; position: relative; padding-left: 0.9375em; padding-right: 0.9375em; }
#header:before, #header:after, #content:before, #content:after, #footnotes:before, #footnotes:after, #footer:before, #footer:after { content: " "; display: table; }
#header:after, #content:after, #footnotes:after, #footer:after { clear: both; }
#content { margin-top: 1.25em; }
#content:before { content: none; }
#header > h1:first-child { color: #703f1c; margin-top: 2.25rem; margin-bottom: 0; }
#header > h1:first-child + #toc { margin-top: 8px; border-top: 1px solid rgba(145, 135, 84, 0.15); }
#header > h1:only-child, body.toc2 #header > h1:nth-last-child(2) { border-bottom: 1px solid rgba(145, 135, 84, 0.15); padding-bottom: 8px; }
#header .details { border-bottom: 1px solid rgba(145, 135, 84, 0.15); line-height: 1.45; padding-top: 0.25em; padding-bottom: 0.25em; padding-left: 0.25em; color: #666666; display: -ms-flexbox; display: -webkit-flex; display: flex; -ms-flex-flow: row wrap; -webkit-flex-flow: row wrap; flex-flow: row wrap; }
#header .details span:first-child { margin-left: -0.125em; }
#header .details span.email a { color: #999999; }
#header .details br { display: none; }
#header .details br + span:before { content: "\00a0\2013\00a0"; }
#header .details br + span.author:before { content: "\00a0\22c5\00a0"; color: #999999; }
#header .details br + span#revremark:before { content: "\00a0|\00a0"; }
#header #revnumber { text-transform: capitalize; }
#header #revnumber:after { content: "\00a0"; }
#content > h1:first-child:not([class]) { color: #703f1c; border-bottom: 1px solid rgba(145, 135, 84, 0.15); padding-bottom: 8px; margin-top: 0; padding-top: 1rem; margin-bottom: 1.25rem; }
#toc { border-bottom: 0px solid #dddddd; padding-bottom: 0.5em; }
#toc > ul { margin-left: 0.125em; }
#toc ul.sectlevel0 > li > a { font-style: italic; }
#toc ul.sectlevel0 ul.sectlevel1 { margin: 0.5em 0; }
#toc ul { font-family: "Varela Round", sans-serif; list-style-type: none; }
#toc li { line-height: 1.3334; margin-top: 0.3334em; }
#toc a { text-decoration: none; }
#toc a:active { text-decoration: underline; }
#toctitle { color: #002c5e; font-size: 1.2em; }
@media only screen and (min-width: 768px) { #toctitle { font-size: 1.375em; }
body.toc2 { padding-left: 15em; padding-right: 0; }
#toc.toc2 { margin-top: 0 !important; background-color: #f2f2f4; position: fixed; width: 15em; left: 0; top: 0; border-right: 1px solid #dddddd; border-top-width: 0 !important; border-bottom-width: 0 !important; z-index: 1000; padding: 1.25em 1em; height: 100%; overflow: auto; }
#toc.toc2 #toctitle { margin-top: 0; margin-bottom: 0.8rem; font-size: 1.2em; }
#toc.toc2 > ul { font-size: 0.9em; margin-bottom: 0; }
#toc.toc2 ul ul { margin-left: 0; padding-left: 1em; }
#toc.toc2 ul.sectlevel0 ul.sectlevel1 { padding-left: 0; margin-top: 0.5em; margin-bottom: 0.5em; }
body.toc2.toc-right { padding-left: 0; padding-right: 15em; }
body.toc2.toc-right #toc.toc2 { border-right-width: 0; border-left: 1px solid #dddddd; left: auto; right: 0; } }
@media only screen and (min-width: 1280px) { body.toc2 { padding-left: 20em; padding-right: 0; }
#toc.toc2 { width: 20em; }
#toc.toc2 #toctitle { font-size: 1.375em; }
#toc.toc2 > ul { font-size: 0.95em; }
#toc.toc2 ul ul { padding-left: 1.25em; }
body.toc2.toc-right { padding-left: 0; padding-right: 20em; } }
#content #toc { border-style: solid; border-width: 1px; border-color: #d6d6dd; margin-bottom: 1.25em; padding: 1.25em; background: #f2f2f4; -webkit-border-radius: 6px; border-radius: 6px; }
#content #toc > :first-child { margin-top: 0; }
#content #toc > :last-child { margin-bottom: 0; }
#footer { max-width: 100%; background-color: #0b445a; padding: 1.25em; }
#footer-text { color: #fefdfd; line-height: 1.35; }
.sect1 { padding-bottom: 0.625em; }
@media only screen and (min-width: 768px) { .sect1 { padding-bottom: 1.25em; } }
.sect1 + .sect1 { border-top: 0px solid #dddddd; }
#content h1 > a.anchor, h2 > a.anchor, h3 > a.anchor, #toctitle > a.anchor, .sidebarblock > .content > .title > a.anchor, h4 > a.anchor, h5 > a.anchor, h6 > a.anchor { position: absolute; z-index: 1001; width: 1.5ex; margin-left: -1.5ex; display: block; text-decoration: none !important; visibility: hidden; text-align: center; font-weight: normal; }
#content h1 > a.anchor:before, h2 > a.anchor:before, h3 > a.anchor:before, #toctitle > a.anchor:before, .sidebarblock > .content > .title > a.anchor:before, h4 > a.anchor:before, h5 > a.anchor:before, h6 > a.anchor:before { content: "\00A7"; font-size: 0.85em; display: block; padding-top: 0.1em; }
#content h1:hover > a.anchor, #content h1 > a.anchor:hover, h2:hover > a.anchor, h2 > a.anchor:hover, h3:hover > a.anchor, #toctitle:hover > a.anchor, .sidebarblock > .content > .title:hover > a.anchor, h3 > a.anchor:hover, #toctitle > a.anchor:hover, .sidebarblock > .content > .title > a.anchor:hover, h4:hover > a.anchor, h4 > a.anchor:hover, h5:hover > a.anchor, h5 > a.anchor:hover, h6:hover > a.anchor, h6 > a.anchor:hover { visibility: visible; }
#content h1 > a.link, h2 > a.link, h3 > a.link, #toctitle > a.link, .sidebarblock > .content > .title > a.link, h4 > a.link, h5 > a.link, h6 > a.link { color: #00326b; text-decoration: none; }
#content h1 > a.link:hover, h2 > a.link:hover, h3 > a.link:hover, #toctitle > a.link:hover, .sidebarblock > .content > .title > a.link:hover, h4 > a.link:hover, h5 > a.link:hover, h6 > a.link:hover { color: #002652; }
.audioblock, .imageblock, .literalblock, .listingblock, .stemblock, .videoblock { margin-bottom: 1.25em; }
.admonitionblock td.content > .title, .audioblock > .title, .exampleblock > .title, .imageblock > .title, .listingblock > .title, .literalblock > .title, .stemblock > .title, .openblock > .title, .paragraph > .title, .quoteblock > .title, table.tableblock > .title, .verseblock > .title, .videoblock > .title, .dlist > .title, .olist > .title, .ulist > .title, .qlist > .title, .hdlist > .title { text-rendering: optimizeLegibility; text-align: left; }
table.tableblock > caption.title { white-space: nowrap; overflow: visible; max-width: 0; }
.paragraph.lead > p, #preamble > .sectionbody > .paragraph:first-of-type p { color: #703f1c; }
table.tableblock #preamble > .sectionbody > .paragraph:first-of-type p { font-size: inherit; }
.admonitionblock > table { border-collapse: separate; border: 0; background: none; width: 100%; }
.admonitionblock > table td.icon { text-align: center; width: 80px; }
.admonitionblock > table td.icon img { max-width: initial; }
.admonitionblock > table td.icon .title { font-weight: bold; font-family: "Varela Round", sans-serif; text-transform: uppercase; }
.admonitionblock > table td.content { padding-left: 1.125em; padding-right: 1.25em; border-left: 1px solid rgba(145, 135, 84, 0.15); color: #666666; }
.admonitionblock > table td.content > :last-child > :last-child { margin-bottom: 0; }
.exampleblock > .content { border-style: solid; border-width: 1px; border-color: #eddbdb; margin-bottom: 1.25em; padding: 1.25em; background: #fefdfd; -webkit-border-radius: 6px; border-radius: 6px; }
.exampleblock > .content > :first-child { margin-top: 0; }
.exampleblock > .content > :last-child { margin-bottom: 0; }
.sidebarblock { border-style: solid; border-width: 1px; border-color: #d6d6dd; margin-bottom: 1.25em; padding: 1.25em; background: #f2f2f4; -webkit-border-radius: 6px; border-radius: 6px; }
.sidebarblock > :first-child { margin-top: 0; }
.sidebarblock > :last-child { margin-bottom: 0; }
.sidebarblock > .content > .title { color: #002c5e; margin-top: 0; }
.exampleblock > .content > :last-child > :last-child, .exampleblock > .content .olist > ol > li:last-child > :last-child, .exampleblock > .content .ulist > ul > li:last-child > :last-child, .exampleblock > .content .qlist > ol > li:last-child > :last-child, .sidebarblock > .content > :last-child > :last-child, .sidebarblock > .content .olist > ol > li:last-child > :last-child, .sidebarblock > .content .ulist > ul > li:last-child > :last-child, .sidebarblock > .content .qlist > ol > li:last-child > :last-child { margin-bottom: 0; }
.literalblock pre, .listingblock pre:not(.highlight), .listingblock pre[class="highlight"], .listingblock pre[class^="highlight "], .listingblock pre.CodeRay, .listingblock pre.prettyprint { background: rgba(16, 195, 196, 0.05); }
.sidebarblock .literalblock pre, .sidebarblock .listingblock pre:not(.highlight), .sidebarblock .listingblock pre[class="highlight"], .sidebarblock .listingblock pre[class^="highlight "], .sidebarblock .listingblock pre.CodeRay, .sidebarblock .listingblock pre.prettyprint { background: #f2f1f1; }
.literalblock pre, .literalblock pre[class], .listingblock pre, .listingblock pre[class] { border: 1px solid rgba(16, 195, 196, 0.125); -webkit-border-radius: 6px; border-radius: 6px; word-wrap: break-word; padding: 1em; font-size: 0.8125em; }
.literalblock pre.nowrap, .literalblock pre[class].nowrap, .listingblock pre.nowrap, .listingblock pre[class].nowrap { overflow-x: auto; white-space: pre; word-wrap: normal; }
@media only screen and (min-width: 768px) { .literalblock pre, .literalblock pre[class], .listingblock pre, .listingblock pre[class] { font-size: 0.90625em; } }
@media only screen and (min-width: 1280px) { .literalblock pre, .literalblock pre[class], .listingblock pre, .listingblock pre[class] { font-size: 1em; } }
.literalblock.output pre { color: rgba(16, 195, 196, 0.05); background-color: inherit; }
.listingblock pre.highlightjs { padding: 0; }
.listingblock pre.highlightjs > code { padding: 1em; -webkit-border-radius: 6px; border-radius: 6px; }
.listingblock > .content { position: relative; }
.listingblock code[data-lang]:before { display: none; content: attr(data-lang); position: absolute; font-size: 0.75em; top: 0.425rem; right: 0.5rem; line-height: 1; text-transform: uppercase; color: #999; }
.listingblock:hover code[data-lang]:before { display: block; }
.listingblock.terminal pre .command:before { content: attr(data-prompt); padding-right: 0.5em; color: #999; }
.listingblock.terminal pre .command:not([data-prompt]):before { content: "$"; }
table.pyhltable { border-collapse: separate; border: 0; margin-bottom: 0; background: none; }
table.pyhltable td { vertical-align: top; padding-top: 0; padding-bottom: 0; line-height: 1.4; }
table.pyhltable td.code { padding-left: .75em; padding-right: 0; }
pre.pygments .lineno, table.pyhltable td:not(.code) { color: #999; padding-left: 0; padding-right: .5em; border-right: 1px solid rgba(145, 135, 84, 0.15); }
pre.pygments .lineno { display: inline-block; margin-right: .25em; }
table.pyhltable .linenodiv { background: none !important; padding-right: 0 !important; }
.quoteblock { margin: 0 1em 1.25em 1.5em; display: table; }
.quoteblock > .title { margin-left: -1.5em; margin-bottom: 0.75em; }
.quoteblock blockquote, .quoteblock blockquote p { color: #999999; font-size: 1.15rem; line-height: 1.75; word-spacing: 0.1em; letter-spacing: 0; font-style: italic; text-align: justify; }
.quoteblock blockquote { margin: 0; padding: 0; border: 0; }
.quoteblock blockquote:before { content: "\201c"; float: left; font-size: 2.75em; font-weight: bold; line-height: 0.6em; margin-left: -0.6em; color: #002c5e; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); }
.quoteblock blockquote > .paragraph:last-child p { margin-bottom: 0; }
.quoteblock .attribution { margin-top: 0.5em; margin-right: 0.5ex; text-align: right; }
.quoteblock .quoteblock { margin-left: 0; margin-right: 0; padding: 0.5em 0; border-left: 3px solid #666666; }
.quoteblock .quoteblock blockquote { padding: 0 0 0 0.75em; }
.quoteblock .quoteblock blockquote:before { display: none; }
.verseblock { margin: 0 1em 1.25em 1em; }
.verseblock pre { font-family: "Open Sans", "DejaVu Sans", sans; font-size: 1.15rem; color: #999999; font-weight: 300; text-rendering: optimizeLegibility; }
.verseblock pre strong { font-weight: 400; }
.verseblock .attribution { margin-top: 1.25rem; margin-left: 0.5ex; }
.quoteblock .attribution, .verseblock .attribution { font-size: 0.8125em; line-height: 1.45; font-style: italic; }
.quoteblock .attribution br, .verseblock .attribution br { display: none; }
.quoteblock .attribution cite, .verseblock .attribution cite { display: block; letter-spacing: -0.025em; color: #666666; }
.quoteblock.abstract { margin: 0 0 1.25em 0; display: block; }
.quoteblock.abstract blockquote, .quoteblock.abstract blockquote p { text-align: left; word-spacing: 0; }
.quoteblock.abstract blockquote:before, .quoteblock.abstract blockquote p:first-of-type:before { display: none; }
table.tableblock { max-width: 100%; border-collapse: separate; }
table.tableblock td > .paragraph:last-child p > p:last-child, table.tableblock th > p:last-child, table.tableblock td > p:last-child { margin-bottom: 0; }
table.tableblock, th.tableblock, td.tableblock { border: 0 solid rgba(145, 135, 84, 0.15); }
table.grid-all th.tableblock, table.grid-all td.tableblock { border-width: 0 1px 1px 0; }
table.grid-all tfoot > tr > th.tableblock, table.grid-all tfoot > tr > td.tableblock { border-width: 1px 1px 0 0; }
table.grid-cols th.tableblock, table.grid-cols td.tableblock { border-width: 0 1px 0 0; }
table.grid-all * > tr > .tableblock:last-child, table.grid-cols * > tr > .tableblock:last-child { border-right-width: 0; }
table.grid-rows th.tableblock, table.grid-rows td.tableblock { border-width: 0 0 1px 0; }
table.grid-all tbody > tr:last-child > th.tableblock, table.grid-all tbody > tr:last-child > td.tableblock, table.grid-all thead:last-child > tr > th.tableblock, table.grid-rows tbody > tr:last-child > th.tableblock, table.grid-rows tbody > tr:last-child > td.tableblock, table.grid-rows thead:last-child > tr > th.tableblock { border-bottom-width: 0; }
table.grid-rows tfoot > tr > th.tableblock, table.grid-rows tfoot > tr > td.tableblock { border-width: 1px 0 0 0; }
table.frame-all { border-width: 1px; }
table.frame-sides { border-width: 0 1px; }
table.frame-topbot { border-width: 1px 0; }
th.halign-left, td.halign-left { text-align: left; }
th.halign-right, td.halign-right { text-align: right; }
th.halign-center, td.halign-center { text-align: center; }
th.valign-top, td.valign-top { vertical-align: top; }
th.valign-bottom, td.valign-bottom { vertical-align: bottom; }
th.valign-middle, td.valign-middle { vertical-align: middle; }
table thead th, table tfoot th { font-weight: bold; }
tbody tr th { display: table-cell; line-height: 1.5; background: rgba(119, 84, 22, 0.1); }
tbody tr th, tbody tr th p, tfoot tr th, tfoot tr th p { color: rgba(0, 0, 0, 0.8); font-weight: bold; }
p.tableblock > code:only-child { background: none; padding: 0; }
p.tableblock { font-size: 1em; }
td > div.verse { white-space: pre; }
ol { margin-left: 1.75em; }
ul li ol { margin-left: 1.5em; }
dl dd { margin-left: 1.125em; }
dl dd:last-child, dl dd:last-child > :last-child { margin-bottom: 0; }
ol > li p, ul > li p, ul dd, ol dd, .olist .olist, .ulist .ulist, .ulist .olist, .olist .ulist { margin-bottom: 0.625em; }
ul.unstyled, ol.unnumbered, ul.checklist, ul.none { list-style-type: none; }
ul.unstyled, ol.unnumbered, ul.checklist { margin-left: 0.625em; }
ul.checklist li > p:first-child > .fa-square-o:first-child, ul.checklist li > p:first-child > .fa-check-square-o:first-child { width: 1em; font-size: 0.85em; }
ul.checklist li > p:first-child > input[type="checkbox"]:first-child { width: 1em; position: relative; top: 1px; }
ul.inline { margin: 0 auto 0.625em auto; margin-left: -1.375em; margin-right: 0; padding: 0; list-style: none; overflow: hidden; }
ul.inline > li { list-style: none; float: left; margin-left: 1.375em; display: block; }
ul.inline > li > * { display: block; }
.unstyled dl dt { font-weight: normal; font-style: normal; }
ol.arabic { list-style-type: decimal; }
ol.decimal { list-style-type: decimal-leading-zero; }
ol.loweralpha { list-style-type: lower-alpha; }
ol.upperalpha { list-style-type: upper-alpha; }
ol.lowerroman { list-style-type: lower-roman; }
ol.upperroman { list-style-type: upper-roman; }
ol.lowergreek { list-style-type: lower-greek; }
.hdlist > table, .colist > table { border: 0; background: none; }
.hdlist > table > tbody > tr, .colist > table > tbody > tr { background: none; }
td.hdlist1, td.hdlist2 { vertical-align: top; padding: 0 0.625em; }
td.hdlist1 { font-weight: bold; padding-bottom: 1.25em; }
.literalblock + .colist, .listingblock + .colist { margin-top: -0.5em; }
.colist > table tr > td:first-of-type { padding: 0 0.75em; line-height: 1; }
.colist > table tr > td:first-of-type img { max-width: initial; }
.colist > table tr > td:last-of-type { padding: 0.25em 0; }
.thumb, .th { line-height: 0; display: inline-block; border: solid 4px white; -webkit-box-shadow: 0 0 0 1px #dddddd; box-shadow: 0 0 0 1px #dddddd; }
.imageblock.left, .imageblock[style*="float: left"] { margin: 0.25em 0.625em 1.25em 0; }
.imageblock.right, .imageblock[style*="float: right"] { margin: 0.25em 0 1.25em 0.625em; }
.imageblock > .title { margin-bottom: 0; }
.imageblock.thumb, .imageblock.th { border-width: 6px; }
.imageblock.thumb > .title, .imageblock.th > .title { padding: 0 0.125em; }
.image.left, .image.right { margin-top: 0.25em; margin-bottom: 0.25em; display: inline-block; line-height: 0; }
.image.left { margin-right: 0.625em; }
.image.right { margin-left: 0.625em; }
a.image { text-decoration: none; display: inline-block; }
a.image object { pointer-events: none; }
sup.footnote, sup.footnoteref { font-size: 0.875em; position: static; vertical-align: super; }
sup.footnote a, sup.footnoteref a { text-decoration: none; }
sup.footnote a:active, sup.footnoteref a:active { text-decoration: underline; }
#footnotes { padding-top: 0.75em; padding-bottom: 0.75em; margin-bottom: 0.625em; }
#footnotes hr { width: 20%; min-width: 6.25em; margin: -0.25em 0 0.75em 0; border-width: 1px 0 0 0; }
#footnotes .footnote { padding: 0 0.375em 0 0.225em; line-height: 1.3334; font-size: 0.875em; margin-left: 1.2em; text-indent: -1.05em; margin-bottom: 0.2em; }
#footnotes .footnote a:first-of-type { font-weight: bold; text-decoration: none; }
#footnotes .footnote:last-of-type { margin-bottom: 0; }
#content #footnotes { margin-top: -0.625em; margin-bottom: 0; padding: 0.75em 0; }
.gist .file-data > table { border: 0; background: #fff; width: 100%; margin-bottom: 0; }
.gist .file-data > table td.line-data { width: 99%; }
div.unbreakable { page-break-inside: avoid; }
.big { font-size: larger; }
.small { font-size: smaller; }
.underline { text-decoration: underline; }
.overline { text-decoration: overline; }
.line-through { text-decoration: line-through; }
.aqua { color: #00bfbf; }
.aqua-background { background-color: #00fafa; }
.black { color: black; }
.black-background { background-color: black; }
.blue { color: #0000bf; }
.blue-background { background-color: #0000fa; }
.fuchsia { color: #bf00bf; }
.fuchsia-background { background-color: #fa00fa; }
.gray { color: #606060; }
.gray-background { background-color: #7d7d7d; }
.green { color: #006000; }
.green-background { background-color: #007d00; }
.lime { color: #00bf00; }
.lime-background { background-color: #00fa00; }
.maroon { color: #600000; }
.maroon-background { background-color: #7d0000; }
.navy { color: #000060; }
.navy-background { background-color: #00007d; }
.olive { color: #606000; }
.olive-background { background-color: #7d7d00; }
.purple { color: #600060; }
.purple-background { background-color: #7d007d; }
.red { color: #bf0000; }
.red-background { background-color: #fa0000; }
.silver { color: #909090; }
.silver-background { background-color: #bcbcbc; }
.teal { color: #006060; }
.teal-background { background-color: #007d7d; }
.white { color: #bfbfbf; }
.white-background { background-color: #fafafa; }
.yellow { color: #bfbf00; }
.yellow-background { background-color: #fafa00; }
span.icon > .fa { cursor: default; }
.admonitionblock td.icon [class^="fa icon-"] { font-size: 2.5em; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); cursor: default; }
.admonitionblock td.icon .icon-note:before { content: "\f05a"; color: #004060; }
.admonitionblock td.icon .icon-tip:before { content: "\f0eb"; text-shadow: 1px 1px 2px rgba(155, 155, 0, 0.8); color: #111; }
.admonitionblock td.icon .icon-warning:before { content: "\f071"; color: #bf6900; }
.admonitionblock td.icon .icon-caution:before { content: "\f06d"; color: #bf3400; }
.admonitionblock td.icon .icon-important:before { content: "\f06a"; color: #bf0000; }
.conum[data-value] { display: inline-block; color: #fff !important; background-color: rgba(0, 0, 0, 0.8); -webkit-border-radius: 100px; border-radius: 100px; text-align: center; font-size: 0.75em; width: 1.67em; height: 1.67em; line-height: 1.67em; font-family: "Open Sans", "DejaVu Sans", sans-serif; font-style: normal; font-weight: bold; }
.conum[data-value] * { color: #fff !important; }
.conum[data-value] + b { display: none; }
.conum[data-value]:after { content: attr(data-value); }
pre .conum[data-value] { position: relative; top: -0.125em; }
b.conum * { color: inherit !important; }
.conum:not([data-value]):empty { display: none; }
#toc.toc2 ul ul { list-style-type: circle; padding-left: 2em; }
.sect1 { padding-bottom: 0 !important; margin-bottom: 2.5em; }
#header h1 { font-weight: bold; position: relative; left: -0.0625em; }
#content h2, #content h3, #content #toctitle, #content .sidebarblock > .content > .title, #content h4, #content h5, #content #toctitle { position: relative; left: -0.0625em; }
#content h2 { font-weight: bold; }
.paragraph.lead > p, #preamble > .sectionbody > .paragraph:first-of-type p { color: black; }
pre.pygments.highlight { background-color: rgba(16, 195, 196, 0.05); }
.pygments .tok-err { border: none !important; color: #800000 !important; }
.pygments .tok-c { color: #999 !important; }
</style>
</head>
<body class="article toc2 toc-left">
<div id="header">
<h1>JCommander</h1>
<div class="details">
<span id="author" class="author">Cédric Beust</span><br>
<span id="email" class="email"><a href="mailto:cedric@beust.com">cedric@beust.com</a></span><br>
</div>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_overview">1. Overview</a></li>
<li><a href="#_types_of_options">2. Types of options</a>
<ul class="sectlevel2">
<li><a href="#_boolean">2.1. Boolean</a></li>
<li><a href="#_lists">2.2. Lists</a></li>
<li><a href="#_password">2.3. Password</a></li>
<li><a href="#_echo_input">2.4. Echo Input</a></li>
</ul>
</li>
<li><a href="#_custom_types_converters_and_splitters">3. Custom types (converters and splitters)</a>
<ul class="sectlevel2">
<li><a href="#single-value">3.1. Custom types - Single value</a></li>
<li><a href="#list-value">3.2. Custom types - List value</a></li>
<li><a href="#_splitting">3.3. Splitting</a></li>
</ul>
</li>
<li><a href="#_parameter_validation">4. Parameter validation</a>
<ul class="sectlevel2">
<li><a href="#_individual_parameter_validation">4.1. Individual parameter validation</a></li>
<li><a href="#_global_parameter_validation">4.2. Global parameter validation</a></li>
</ul>
</li>
<li><a href="#_main_parameter">5. Main parameter</a></li>
<li><a href="#_private_parameters">6. Private parameters</a></li>
<li><a href="#_parameter_separators">7. Parameter separators</a></li>
<li><a href="#_multiple_descriptions">8. Multiple descriptions</a></li>
<li><a href="#__syntax">9. @ syntax</a></li>
<li><a href="#_arities_multiple_values_for_parameters">10. Arities (multiple values for parameters)</a>
<ul class="sectlevel2">
<li><a href="#_fixed_arities">10.1. Fixed arities</a></li>
<li><a href="#_variable_arities">10.2. Variable arities</a></li>
</ul>
</li>
<li><a href="#_multiple_option_names">11. Multiple option names</a></li>
<li><a href="#_other_option_configurations">12. Other option configurations</a></li>
<li><a href="#_required_and_optional_parameters">13. Required and optional parameters</a></li>
<li><a href="#_default_values">14. Default values</a></li>
<li><a href="#_help_parameter">15. Help parameter</a></li>
<li><a href="#_more_complex_syntaxes_commands">16. More complex syntaxes (commands)</a></li>
<li><a href="#_exception">17. Exception</a></li>
<li><a href="#_usage">18. Usage</a></li>
<li><a href="#_hiding_parameters">19. Hiding parameters</a></li>
<li><a href="#_internationalization">20. Internationalization</a></li>
<li><a href="#_parameter_delegates">21. Parameter delegates</a></li>
<li><a href="#_dynamic_parameters">22. Dynamic parameters</a></li>
<li><a href="#_jcommander_in_other_languages">23. JCommander in other languages</a>
<ul class="sectlevel2">
<li><a href="#_kotlin">23.1. Kotlin</a></li>
<li><a href="#_groovy">23.2. Groovy</a></li>
</ul>
</li>
<li><a href="#_more_examples">24. More examples</a></li>
<li><a href="#_mailing_list">25. Mailing list</a></li>
<li><a href="#_javadocs">26. Javadocs</a></li>
<li><a href="#_license">27. License</a></li>
<li><a href="#_download">28. Download</a></li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div style="float:right">
<div style="display:inline-block">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="cedric@beust.com">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Cedric Beust">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" hidden="" style="display: none !important;">
</form>
</div>
</div>
<div class="paragraph">
<p><em>"Because life is too short to parse command line parameters"</em></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_overview">1. Overview</h2>
<div class="sectionbody">
<div class="paragraph">
<p>JCommander is a very small Java framework that makes it trivial to parse command line parameters.
You annotate fields with descriptions of your options:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">import com.beust.jcommander.Parameter;
public class Args {
@Parameter
private List&lt;String&gt; parameters = new ArrayList&lt;&gt;();
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
private Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
private String groups;
@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>and then you simply ask JCommander to parse:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">Args args = new Args();
String[] argv = { "-log", "2", "-groups", "unit" };
JCommander.newBuilder()
.addObject(args)
.build()
.parse(argv);
Assert.assertEquals(jct.verbose.intValue(), 2);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here is another example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">class Main {
@Parameter(names={"--length", "-l"})
int length;
@Parameter(names={"--pattern", "-p"})
int pattern;
public static void main(String ... argv) {
Main main = new Main();
JCommander.newBuilder()
.addObject(main)
.build()
.parse(argv);
main.run();
}
public void run() {
System.out.printf("%d %d", length, pattern);
}
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -l 512 --pattern 2
512 2</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_types_of_options">2. Types of options</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The fields representing your parameters can be of any type. Basic types (<code>Integer</code>, <code>Boolean</code>, etc&#8230;&#8203;) are supported by default and you can write type converters to support any other type (<code>File</code>, etc&#8230;&#8203;).</p>
</div>
<div class="sect2">
<h3 id="_boolean">2.1. Boolean</h3>
<div class="paragraph">
<p>When a Parameter annotation is found on a field of type <code>boolean</code> or <code>Boolean</code>, JCommander interprets it as an option with an arity of 0:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Such a parameter does not require any additional parameter on the command line and if it&#8217;s detected during parsing, the corresponding field will be set to true. If you want to define a boolean parameter that&#8217;s true by default, you can declare it as having an arity of 1. Users will then have to specify the value they want explicitly:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-debug", description = "Debug mode", arity = 1)
private boolean debug = true;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Invoke with either of:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">program -debug true
program -debug false</code></pre>
</div>
</div>
<div class="paragraph">
<p>When a Parameter annotation is found on a field of type <code>String</code>, <code>Integer</code>, <code>int</code>, <code>Long</code> or <code>long</code>, JCommander will parse the following parameter and it will attempt to cast it to the right type:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-log", description = "Level of verbosity")
private Integer verbose = 1;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">java Main -log 3</code></pre>
</div>
</div>
<div class="paragraph">
<p>will cause the field verbose to receive the value 3. However:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -log test</code></pre>
</div>
</div>
<div class="paragraph">
<p>will cause an exception to be thrown.</p>
</div>
</div>
<div class="sect2">
<h3 id="_lists">2.2. Lists</h3>
<div class="paragraph">
<p>When a Parameter annotation is found on a field of type <code>List</code>, JCommander will interpret it as an option that can occur multiple times:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-host", description = "The host")
private List&lt;String&gt; hosts = new ArrayList&lt;&gt;();</code></pre>
</div>
</div>
<div class="paragraph">
<p>will allow you to parse the following command line:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -host host1 -verbose -host host2</code></pre>
</div>
</div>
<div class="paragraph">
<p>When JCommander is done parsing the line above, the field hosts will contain the strings <code>"host1"</code> and <code>"host2"</code>.</p>
</div>
</div>
<div class="sect2">
<h3 id="_password">2.3. Password</h3>
<div class="paragraph">
<p>If one of your parameters is a password or some other value that you do not wish to appear in your history or in clear, you can declare it of type password and JCommander will then ask you to enter it in the console:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class ArgsPassword {
@Parameter(names = "-password", description = "Connection password", password = true)
private String password;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>When you run your program, you will get the following prompt:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">Value for -password (Connection password):</code></pre>
</div>
</div>
<div class="paragraph">
<p>You will need to type the value at this point before JCommander resumes.</p>
</div>
</div>
<div class="sect2">
<h3 id="_echo_input">2.4. Echo Input</h3>
<div class="paragraph">
<p>In Java 6, by default, you will not be able to see what you type for passwords entered at the prompt (Java 5 and lower will always show the password). However, you can override this by setting echoInput to <code>true</code> (default is <code>false</code> and this setting only has an effect when password is <code>true</code>):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class ArgsPassword {
@Parameter(names = "-password", description = "Connection password", password = true, echoInput = true)
private String password;
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_custom_types_converters_and_splitters">3. Custom types (converters and splitters)</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To bind parameters to custom types or change the way how JCommander splits parameters (default is to split via comma) JCommander provides two
interfaces <code>IStringConverter</code> and <code>IParameterSplitter</code>.</p>
</div>
<div class="sect2">
<h3 id="single-value">3.1. Custom types - Single value</h3>
<div class="paragraph">
<p>Use either the <code>converter=</code> attribute of the <code>@Parameter</code> or implement <code>IStringConverterFactory</code>.</p>
</div>
<div class="sect3">
<h4 id="_by_annotation">3.1.1. By annotation</h4>
<div class="paragraph">
<p>By default, JCommander parses the command line into basic types only (strings, booleans, integers and longs). Very often, your application actually needs more complex types (such as files, host names, lists, etc.). To achieve this, you can write a type converter by implementing the following interface:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IStringConverter&lt;T&gt; {
T convert(String value);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For example, here is a converter that turns a string into a File:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class FileConverter implements IStringConverter&lt;File&gt; {
@Override
public File convert(String value) {
return new File(value);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then, all you need to do is declare your field with the correct type and specify the converter as an attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-file", converter = FileConverter.class)
File file;</code></pre>
</div>
</div>
<div class="paragraph">
<p>JCommander ships with a few common converters (for more info please see the implementations of <code>IStringConverter</code>).</p>
</div>
<div class="sect4">
<h5 id="_note">Note</h5>
<div class="paragraph">
<p>If a converter is used for a <code>List</code> field:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-files", converter = FileConverter.class)
List&lt;File&gt; files;</code></pre>
</div>
</div>
<div class="paragraph">
<p>And the application is called as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java App -files file1,file2,file3</code></pre>
</div>
</div>
<div class="paragraph">
<p>JCommander will split the string <code>file1,file2,file3</code> into <code>file1</code>, <code>file2</code>, <code>file3</code> and feed it one by one to the converter.</p>
</div>
<div class="paragraph">
<p>For an alternative solution to parse a list of values, see <a href="#list-value">Custom types - List value</a>.</p>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_by_factory">3.1.2. By factory</h4>
<div class="paragraph">
<p>If the custom types you use appear multiple times in your application, having to specify the converter in each annotation can become tedious. To address this, you can use an <code>IStringConverterFactory</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IStringConverterFactory {
&lt;T&gt; Class&lt;? extends IStringConverter&lt;T&gt;&gt; getConverter(Class&lt;T&gt; forType);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For example, suppose you need to parse a string representing a host and a port:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java App -target example.com:8080</code></pre>
</div>
</div>
<div class="paragraph">
<p>You define the holder class :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class HostPort {
public HostPort(String host, String port) {
this.host = host;
this.port = port;
}
final String host;
final Integer port;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>and the string converter to create instances of this class:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">class HostPortConverter implements IStringConverter&lt;HostPort&gt; {
@Override
public HostPort convert(String value) {
String[] s = value.split(":");
return new HostPort(s[0], Integer.parseInt(s[1]));
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The factory is straightforward:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class Factory implements IStringConverterFactory {
public Class&lt;? extends IStringConverter&lt;?&gt;&gt; getConverter(Class forType) {
if (forType.equals(HostPort.class)) return HostPortConverter.class;
else return null;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can now use the type <code>HostPort</code> as a parameter without any converterClass attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class ArgsConverterFactory {
@Parameter(names = "-hostport")
private HostPort hostPort;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>All you need to do is add the factory to your JCommander object:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">ArgsConverterFactory a = new ArgsConverterFactory();
JCommander jc = JCommander.newBuilder()
.addObject(a)
.addConverterFactory(new Factory())
.build()
.parse("-hostport", "example.com:8080");
Assert.assertEquals(a.hostPort.host, "example.com");
Assert.assertEquals(a.hostPort.port.intValue(), 8080);</code></pre>
</div>
</div>
<div class="paragraph">
<p>Another advantage of using string converter factories is that your factories can come from a dependency injection framework.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="list-value">3.2. Custom types - List value</h3>
<div class="paragraph">
<p>Use the <code>listConverter=</code> attribute of the <code>@Parameter</code> annotation and assign a custom <code>IStringConverter</code> implementation to convert a <code>String</code> into a <code>List</code> of values.</p>
</div>
<div class="sect3">
<h4 id="_by_annotation_2">3.2.1. By annotation</h4>
<div class="paragraph">
<p>If your application needs a list of complex types, write a list type converter by implementing the same interface as before:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IStringConverter&lt;T&gt; {
T convert(String value);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>where <code>T</code> is a <code>List</code>.</p>
</div>
<div class="paragraph">
<p>For example, here is a list converter that turns a string into a <code>List&lt;File&gt;</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class FileListConverter implements IStringConverter&lt;List&lt;File&gt;&gt; {
@Override
public List&lt;File&gt; convert(String files) {
String [] paths = files.split(",");
List&lt;File&gt; fileList = new ArrayList&lt;&gt;();
for(String path : paths){
fileList.add(new File(path));
}
return fileList;
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then, all you need to do is declare your field with the correct type and specify the list converter as an attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-files", listConverter = FileListConverter.class)
List&lt;File&gt; file;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now if you call for application as in the following example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java App -files file1,file2,file3</code></pre>
</div>
</div>
<div class="paragraph">
<p>The parameter <code>file1,file2,file3</code> is given to the <code>listConverter</code> and will the properly processed.</p>
</div>
<div class="paragraph">
<p>JCommander ships with a default converter for <code>String</code> values.</p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_splitting">3.3. Splitting</h3>
<div class="paragraph">
<p>Use the <code>splitter=</code> attribute of the <code>@Parameter</code> annotation and assign a custom <code>IParameterSplitter</code> implementation to handle how parameters are split in sub-parts.</p>
</div>
<div class="sect3">
<h4 id="_by_annotation_3">3.3.1. By annotation</h4>
<div class="paragraph">
<p>By default, JCommander tries to split parameters for <code>List</code> field types on commas.</p>
</div>
<div class="paragraph">
<p>To split parameters on other characters, you can write a custom splitter by implementing the following interface:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IParameterSplitter {
List&lt;String&gt; split(String value);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>For example, here is a splitter that splits a string on semicolon:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public static class SemiColonSplitter implements IParameterSplitter {
public List&lt;String&gt; split(String value) {
return Arrays.asList(value.split(";"));
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then, all you need to do is declare your field with the correct type and specify the splitter as an attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-files", converter = FileConverter.class, splitter = SemiColonSplitter.class)
List&lt;File&gt; files;</code></pre>
</div>
</div>
<div class="paragraph">
<p>JCommander will split the string <code>file1;file2;file3</code> into <code>file1</code>, <code>file2</code>, <code>file3</code> and feed it one by one to the converter.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_parameter_validation">4. Parameter validation</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Parameter validation can be performed in two different ways: at the individual parameter level or globally.</p>
</div>
<div class="sect2">
<h3 id="_individual_parameter_validation">4.1. Individual parameter validation</h3>
<div class="paragraph">
<p>You can ask JCommander to perform early validation on your parameters by providing a class that implements the following interface:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IParameterValidator {
/**
* Validate the parameter.
*
* @param name The name of the parameter (e.g. "-host").
* @param value The value of the parameter that we need to validate
*
* @throws ParameterException Thrown if the value of the parameter is invalid.
*/
void validate(String name, String value) throws ParameterException;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here is an example implementation that will make sure that the parameter is a positive integer:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class PositiveInteger implements IParameterValidator {
public void validate(String name, String value)
throws ParameterException {
int n = Integer.parseInt(value);
if (n &lt; 0) {
throw new ParameterException("Parameter " + name + " should be positive (found " + value +")");
}
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Specify the name of a class implementing this interface in the <code>validateWith</code> attribute of your <code>@Parameter</code> annotations:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-age", validateWith = PositiveInteger.class)
private Integer age;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Attempting to pass a negative integer to this option will cause a ParameterException to be thrown.</p>
</div>
<div class="paragraph">
<p>Multiple validators may be specified:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-count", validateWith = { PositiveInteger.class, CustomOddNumberValidator.class })
private Integer value;</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_global_parameter_validation">4.2. Global parameter validation</h3>
<div class="paragraph">
<p>After parsing your parameters with JCommander, you might want to perform additional validation across these parameters, such as making sure that two mutually exclusive parameters are not both specified. Because of all the potential combinations involved in such validation, JCommander does not provide any annotation-based solution to perform this validation because such an approach would necessarily be very limited by the very nature of Java annotations. Instead, you should simply perform this validation in Java on all the arguments that JCommander just parsed.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_main_parameter">5. Main parameter</h2>
<div class="sectionbody">
<div class="paragraph">
<p>So far, all the <code>@Parameter</code> annotations we have seen had defined an attribute called <code>names</code>. You can define one (and at most one) parameter without any such attribute. This parameter needs to be a <code>List&lt;String&gt;</code> and it will contain all the parameters that are not options:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(description = "Files")
private List&lt;String&gt; files = new ArrayList&lt;&gt;();
@Parameter(names = "-debug", description = "Debugging level")
private Integer debug = 1;</code></pre>
</div>
</div>
<div class="paragraph">
<p>will allow you to parse:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -debug file1 file2</code></pre>
</div>
</div>
<div class="paragraph">
<p>and the field files will receive the strings <code>"file1"</code> and <code>"file2"</code>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_private_parameters">6. Private parameters</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Parameters can be private:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class ArgsPrivate {
@Parameter(names = "-verbose")
private Integer verbose = 1;
public Integer getVerbose() {
return verbose;
}
}
ArgsPrivate args = new ArgsPrivate();
JCommander.newBuilder()
.addObject(args)
.build()
.parse("-verbose", "3");
Assert.assertEquals(args.getVerbose().intValue(), 3);</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_parameter_separators">7. Parameter separators</h2>
<div class="sectionbody">
<div class="paragraph">
<p>By default, parameters are separated by spaces, but you can change this setting to allow different separators:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -log:3</code></pre>
</div>
</div>
<div class="paragraph">
<p>or</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -level=42</code></pre>
</div>
</div>
<div class="paragraph">
<p>You define the separator with the @Parameters annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameters(separators = "=")
public class SeparatorEqual {
@Parameter(names = "-level")
private Integer level = 2;
}</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_multiple_descriptions">8. Multiple descriptions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can spread the description of your parameters on more than one class. For example, you can define the following two classes:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public class ArgsMaster {
@Parameter(names = "-master")
private String master;
}
public class ArgsSlave {
@Parameter(names = "-slave")
private String slave;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>and pass these two objects to JCommander:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">ArgsMaster m = new ArgsMaster();
ArgsSlave s = new ArgsSlave();
String[] argv = { "-master", "master", "-slave", "slave" };
JCommander.newBuilder()
.addObject(new Object[] { m , s })
.build()
.parse(argv);
Assert.assertEquals(m.master, "master");
Assert.assertEquals(s.slave, "slave");</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="__syntax">9. @ syntax</h2>
<div class="sectionbody">
<div class="paragraph">
<p>JCommander supports the @ syntax, which allows you to put all your options into a file and pass this file as parameter:</p>
</div>
<div id="app-listing" class="listingblock">
<div class="title">/tmp/parameters</div>
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">-verbose
file1
file2
file3</code></pre>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main @/tmp/parameters</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_arities_multiple_values_for_parameters">10. Arities (multiple values for parameters)</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_fixed_arities">10.1. Fixed arities</h3>
<div class="paragraph">
<p>If some of your parameters require more than one value, such as the following example where two values are expected after -pairs:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -pairs slave master foo.xml</code></pre>
</div>
</div>
<div class="paragraph">
<p>then you need to define your parameter with the arity attribute and make that parameter a <code>List&lt;String&gt;</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-pairs", arity = 2, description = "Pairs")
private List&lt;String&gt; pairs;</code></pre>
</div>
</div>
<div class="paragraph">
<p>You don&#8217;t need to specify an arity for parameters of type <code>boolean</code> or <code>Boolean</code> (which have a default arity of 0) and of types <code>String</code>, <code>Integer</code>, <code>int</code>, <code>Long</code> and <code>long</code> (which have a default arity of 1).</p>
</div>
<div class="paragraph">
<p>Also, note that only <code>List&lt;String&gt;</code> is allowed for parameters that define an arity. You will have to convert these values yourself if the parameters you need are of type <code>Integer</code> or other (this limitation is due to Java&#8217;s erasure).</p>
</div>
</div>
<div class="sect2">
<h3 id="_variable_arities">10.2. Variable arities</h3>
<div class="paragraph">
<p>You can specify that a parameter can receive an indefinite number of parameters, up to the next option. For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">program -foo a1 a2 a3 -bar
program -foo a1 -bar</code></pre>
</div>
</div>
<div class="paragraph">
<p>Such a parameter can be parsed in two different ways.</p>
</div>
<div class="sect3">
<h4 id="_with_a_list">10.2.1. With a list</h4>
<div class="paragraph">
<p>If the number of following parameters is unknown, your parameter must be of type <code>List&lt;String&gt;</code> and you
need to set the boolean <code>variableArity</code> to <code>true</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-foo", variableArity = true)
public List&lt;String&gt; foo = new ArrayList&lt;&gt;();</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_with_a_class">10.2.2. With a class</h4>
<div class="paragraph">
<p>Alternatively, you can define a class in which the following parameters will be stored, based on their order
of appearance:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">static class MvParameters {
@SubParameter(order = 0)
String from;
@SubParameter(order = 1)
String to;
}
@Test
public void arity() {
class Parameters {
@Parameter(names = {"--mv"}, arity = 2)
private MvParameters mvParameters;
}
Parameters args = new Parameters();
JCommander.newBuilder()
.addObject(args)
.args(new String[]{"--mv", "from", "to"})
.build();
Assert.assertNotNull(args.mvParameters);
Assert.assertEquals(args.mvParameters.from, "from");
Assert.assertEquals(args.mvParameters.to, "to");
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_multiple_option_names">11. Multiple option names</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can specify more than one option name:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = { "-d", "--outputDirectory" }, description = "Directory")
private String outputDirectory;</code></pre>
</div>
</div>
<div class="paragraph">
<p>will allow both following syntaxes:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ java Main -d /tmp
$ java Main --outputDirectory /tmp</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_other_option_configurations">12. Other option configurations</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can configure how options are looked up in a few different ways:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>JCommander#setCaseSensitiveOptions(boolean)</code>: specify whether options are case sensitive. If you call this method with <code>false</code>, then <code>"-param"</code> and <code>"-PARAM"</code> are considered equal.</p>
</li>
<li>
<p><code>JCommander#setAllowAbbreviatedOptions(boolean)</code>: specify whether users can pass abbreviated options. If you call this method with <code>true</code> then users can pass <code>"-par"</code> to specify an option called <code>-param</code>. JCommander will throw a <code>ParameterException</code> if the abbreviated name is ambiguous.</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_required_and_optional_parameters">13. Required and optional parameters</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If some of your parameters are mandatory, you can use the <code>required</code> attribute (which default to <code>false</code>):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-host", required = true)
private String host;</code></pre>
</div>
</div>
<div class="paragraph">
<p>If this parameter is not specified, JCommander will throw an exception telling you which options are missing.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_default_values">14. Default values</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The most common way to specify a default value for your parameters is to initialize the field at declaration time:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">private Integer logLevel = 3;</code></pre>
</div>
</div>
<div class="paragraph">
<p>For more complicated cases, you might want to be able to reuse identical default values across several main classes or be able to specify these default values in a centralized location such as a <code>.properties</code> or an XML file. In this case, you can use an <code>IDefaultProvider</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">public interface IDefaultProvider {
/**
* @param optionName The name of the option as specified in the names() attribute
* of the @Parameter option (e.g. "-file").
*
* @return the default value for this option.
*/
String getDefaultValueFor(String optionName);
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>By passing an implementation of this interface to your JCommander object, you can now control which default value will be used for your options. Note that the value returned by this method will then be passed to a string converter, if any is applicable, thereby allowing you to specify default values for any types you need.</p>
</div>
<div class="paragraph">
<p>For example, here is a default provider that will assign a default value of 42 for all your parameters except <code>"-debug"</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">private static final IDefaultProvider DEFAULT_PROVIDER = new IDefaultProvider() {
@Override
public String getDefaultValueFor(String optionName) {
return "-debug".equals(optionName) ? "false" : "42";
}
};
// ...
JCommander jc = JCommander.newBuilder()
.addObject(new Args())
.defaultProvider(DEFAULT_PROVIDER)
.build()</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_help_parameter">15. Help parameter</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If one of your parameters is used to display some help or usage, you need use the help attribute:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "--help", help = true)
private boolean help;</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you omit this boolean, JCommander will instead issue an error message when it tries to validate your command and it finds that you didn&#8217;t specify some of the required parameters.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_more_complex_syntaxes_commands">16. More complex syntaxes (commands)</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Complex tools such as <code>git</code> or <code>svn</code> understand a whole set of commands, each of which with their own specific syntax:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">$ git commit --amend -m "Bug fix"</code></pre>
</div>
</div>
<div class="paragraph">
<p>Words such as <code>"commit"</code> above are called "commands" in JCommander, and you can specify them by creating one arg object per command:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameters(separators = "=", commandDescription = "Record changes to the repository")
private class CommandCommit {
@Parameter(description = "The list of files to commit")
private List&lt;String&gt; files;
@Parameter(names = "--amend", description = "Amend")
private Boolean amend = false;
@Parameter(names = "--author")
private String author;
}
@Parameters(commandDescription = "Add file contents to the index")
public class CommandAdd {
@Parameter(description = "File patterns to add to the index")
private List&lt;String&gt; patterns;
@Parameter(names = "-i")
private Boolean interactive = false;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then you register these commands with your JCommander object. After the parsing phase, you call <code>getParsedCommand()</code> 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):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">CommandMain cm = new CommandMain();
CommandAdd add = new CommandAdd();
CommandCommit commit = new CommandCommit();
JCommander jc = JCommander.newBuilder()
.addObject(cm)
.addCommand("add", add);
.addCommand("commit", commit);
.build();
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"));</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_exception">17. Exception</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Whenever JCommander detects an error, it will throw a <code>ParameterException</code>. Note that this is a Runtime Exception, since your application is probably not initialized correctly at this point. Also, <code>ParameterException</code> contains the
<code>JCommander</code> instance and you can also invoke <code>usage()</code> on it if you need to display some help.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_usage">18. Usage</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can invoke <code>usage()</code> on the JCommander instance that you used to parse your command line in order to generate a summary of all the options that your program understands:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">Usage: &lt;main class&gt; [options]
Options:
-debug Debug mode (default: false)
-groups Comma-separated list of group names to be run
* -log, -verbose Level of verbosity (default: 1)
-long A long number (default: 0)</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can customize the name of your program by calling <code>setProgramName()</code> on your JCommander object. Options preceded by an asterisk are required.</p>
</div>
<div class="paragraph">
<p>You can also specify the order in which each option should be displayed when calling <code>usage()</code> by setting the <code>order</code> attribute of the <code>@Parameter</code> annotation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">class Parameters {
@Parameter(names = "--importantOption", order = 0)
private boolean a;
@Parameter(names = "--lessImportantOption", order = 3)
private boolean b;</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_hiding_parameters">19. Hiding parameters</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If you don&#8217;t want certain parameters to appear in the usage, you can mark them as "hidden":</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameter(names = "-debug", description = "Debug mode", hidden = true)
private boolean debug = false;</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_internationalization">20. Internationalization</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can internationalize the descriptions of your parameters. First you use the <code>@Parameters</code> annotation at the top of your class to define the name of your message bundle, and then you use the <code>descriptionKey</code> attribute instead of description on all the <code>@Parameters</code> that require translations. This <code>descriptionKey</code> is the key to the string into your message bundle:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@Parameters(resourceBundle = "MessageBundle")
private class ArgsI18N2 {
@Parameter(names = "-host", description = "Host", descriptionKey = "host")
String hostName;
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Your bundle needs to define this key:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-bash" data-lang="bash">host: Hôte</code></pre>
</div>
</div>
<div class="paragraph">
<p>JCommander will then use the default locale to resolve your descriptions.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_parameter_delegates">21. Parameter delegates</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If you are writing many different tools in the same project, you will probably find that most of these tools can share configurations. While you can use inheritance with your objects to avoid repeating this code, the restriction to single inheritance of implementation might limit your flexibility. To address this problem, JCommander supports parameter delegates.</p>
</div>
<div class="paragraph">
<p>When JCommander encounters an object annotated with <code>@ParameterDelegate</code> in one of your objects, it acts as if this object had been added as a description object itself:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">class Delegate {
@Parameter(names = "-port")
private int port;
}
class MainParams {
@Parameter(names = "-v")
private boolean verbose;
@ParametersDelegate
private Delegate delegate = new Delegate();
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The example above specifies a delegate parameter Delegate which is then referenced in MainParams. You only need to add a <code>MainParams</code> object to your
JCommander configuration in order to use the delegate:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">MainParams p = new MainParams();
JCommander.newBuilder().addObject(p).build()
.parse("-v", "-port", "1234");
Assert.assertTrue(p.isVerbose);
Assert.assertEquals(p.delegate.port, 1234);</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_dynamic_parameters">22. Dynamic parameters</h2>
<div class="sectionbody">
<div class="paragraph">
<p>JCommander allows you to specify parameters that are not known at compile time, such as "-Da=b -Dc=d". Such parameters are specified with the <code>@DynamicParameter</code> annotation and must be of type <code>Map&lt;String, String&gt;</code>. Dynamic parameters are allowed to appear multiple times on the command line:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-java" data-lang="java">@DynamicParameter(names = "-D", description = "Dynamic parameters go here")
private Map&lt;String, String&gt; params = new HashMap&lt;&gt;();</code></pre>
</div>
</div>
<div class="paragraph">
<p>You can specify a different assignment string than = by using the attribute assignment.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_jcommander_in_other_languages">23. JCommander in other languages</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_kotlin">23.1. Kotlin</h3>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-kotlin" data-lang="kotlin">class Args {
@Parameter
var targets: List&lt;String&gt; = arrayListOf()
@Parameter(names = arrayOf("-bf", "--buildFile"), description = "The build file")
var buildFile: String? = null
@Parameter(names = arrayOf("--checkVersions"),
description = "Check if there are any newer versions of the dependencies")
var checkVersions = false
}</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_groovy">23.2. Groovy</h3>
<div class="paragraph">
<p>Courtesy of Paul King:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-groovy" data-lang="groovy">import com.beust.jcommander.*
class Args {
@Parameter(names = ["-f", "--file"], description = "File to load. Can be specified multiple times.")
List&lt;String&gt; file
}
new Args().with {
JCommander.newBuilder().addObject(it).build().parse(argv)
file.each { println "file: ${new File(it).name}" }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_more_examples">24. More examples</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Here are the description files for a few projects that use JCommander:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/CommandLineArgs.java">TestNG</a></p>
</li>
<li>
<p><a href="https://github.com/cbeust/kobalt/blob/master/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt">Kobalt</a></p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_mailing_list">25. Mailing list</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Join the <a href="http://groups.google.com/group/jcommander">JCommander Google group</a> if you are interested in discussions about JCommander.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_javadocs">26. Javadocs</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The Javadocs for JCommander can be found <a href="http://jcommander.org/apidocs/">here</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_license">27. License</h2>
<div class="sectionbody">
<div class="paragraph">
<p>JCommander is released under the <a href="https://github.com/cbeust/jcommander/blob/master/license.txt">Apache 2.0 license</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_download">28. Download</h2>
<div class="sectionbody">
<div class="paragraph">
<p>You can download JCommander from the following locations:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="http://github.com/cbeust/jcommander">Source on github</a></p>
</li>
<li>
<p>Kobalt</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-groovy" data-lang="groovy">compile("com.beust:jcommander:1.71")</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Gradle</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-groovy" data-lang="groovy">compile "com.beust:jcommander:1.71"</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Maven:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code class="language-xml" data-lang="xml">&lt;dependency&gt;
&lt;groupId&gt;com.beust&lt;/groupId&gt;
&lt;artifactId&gt;jcommander&lt;/artifactId&gt;
&lt;version&gt;1.71&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
</div>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2017-05-02 10:07:41 PDT
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script>prettyPrint()</script>
</body>
</html>