Skip to content

Commit 5d1d967

Browse files
committed
CLI-267: Add an addRequiredOption method to Options. Thanks to Ricardo Ribeiro. This also closes apache#7 from GitHub.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@1754329 13f79535-47bb-0310-9956-ffa450edef68
1 parent afc13c4 commit 5d1d967

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<body>
2424

2525
<release version="1.4" date="tba" description="tba">
26+
<action type="add" dev="britter" issue="CLI-267" due-to="Ricardo Ribeiro">
27+
Add an addRequiredOption method to Options
28+
</action>
2629
<action type="fix" dev="britter" issue="CLI-266" due-to="Ravi Teja">
2730
HelpFormatter.setOptionComparator(null) doesn't display the values in inserted order
2831
</action>

src/main/java/org/apache/commons/cli/Options.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ public Options addOption(String opt, String longOpt, boolean hasArg, String desc
142142
return this;
143143
}
144144

145+
/**
146+
* Add an option that contains a short-name and a long-name.
147+
* This option is set as required.
148+
* It may be specified as requiring an argument.
149+
*
150+
* @param opt Short single-character name of the option.
151+
* @param longOpt Long multi-character name of the option.
152+
* @param hasArg flag signally if an argument is required after this option
153+
* @param description Self-documenting description
154+
* @return the resulting Options instance
155+
*/
156+
public Options addRequiredOption(String opt, String longOpt, boolean hasArg, String description)
157+
{
158+
Option option = new Option(opt, longOpt, hasArg, description);
159+
option.setRequired(true);
160+
addOption(option);
161+
return this;
162+
}
163+
145164
/**
146165
* Adds an option instance
147166
*

0 commit comments

Comments
 (0)