Skip to content

Commit cd1737b

Browse files
committed
Documented new short option aggregation feature
1 parent 80dfd9b commit cd1737b

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

README.adoc

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ If the found argumemt is detected as command, than CmdOptions switches into the
145145
After CmdOption switched into command mode once, all succeeding arguments are only parsed into the scope of that command.
146146

147147
If the application supports parameters (non-options, declared with a `@CmdOption` annotation without a `names` parameter)
148-
the parser will scan all commandline arguemnts that are not detected as options or commands into that parameter.
148+
the parser will scan all commandline arguments that are not detected as options or commands into that parameter.
149149

150150
=== Stop option detecting with `--`
151151

@@ -166,6 +166,57 @@ If desired, you can change the prefix with `CmdlineParser.setReadArgsFromFilePre
166166
The given string must be at least one character long.
167167
With an empty string or `null` you can disable that feature completely.
168168

169+
=== Aggregation of short options
170+
171+
_By principle, CmdOption does not enforce any type of option format.
172+
But nevertheless, the most common variants for Java applications are the Java-style options (starting with a single dash ("-")) and GNU-style options (long options starting with a double dash ("--") and short options starting with a single dash ("-"))._
173+
174+
_A typical convenience feature of GNU-style parsers is to support aggreated short options.
175+
That means, instead of declaring each option separately as in `ls -l -a` you can write them as one `ls -la`.
176+
You can do the same with CmdOption._
177+
178+
If you tell CmdOption which prefix starts a short option (an option with consists of only a single character after the prefix), CmdOption can parse all those option also when given in an aggreated way.
179+
By default, this feature is disabled.
180+
181+
To enable aggregation of short options use `CmdlineParser.setAggregateShortOptionsWithPrefix(String)`.
182+
An argument of `null` or the empty string disables this feature.
183+
184+
.Example for aggregated options
185+
[source,java]
186+
----
187+
import de.tototec.cmdoption.CmdOption;
188+
import de.tototec.cmdoption.CmdlineParser;
189+
190+
public class Config {
191+
@CmdOption(names = { "-f", "--file" }, args = { "FILE" })
192+
String file = null;
193+
194+
@CmdOption(names = { "-l" })
195+
boolean formatLong = false;
196+
197+
@CmdOption(names = { "-s", "--size" })
198+
boolean showSize = false;
199+
}
200+
201+
public class Main {
202+
public static void main(String[] args) {
203+
final Config config = new Config();
204+
final CmdlineParser cp = new CmdlineParser(config);
205+
206+
cp.setAggregateShortOptionsWithPrefix("-"); //<1>
207+
208+
// demo of parsing aggregated options
209+
cp.parse(new String[] { "-lfs", "file.txt" });
210+
assert config.formatLong == true;
211+
assert "file.txt".equals(config.file);
212+
assert config.showSize == true;
213+
}
214+
}
215+
----
216+
<1> This line enabled aggregation of short options starting with a dash ("-").
217+
218+
As you can see, aggregated short options can also have any number of arguments.
219+
169220
== Options and Parameters
170221

171222
The `@CmdOption` annotation can be used to declare fields and methods as options.
@@ -612,6 +663,7 @@ Have a look at some other projects I'm involved with:
612663
* New `UsageFormatter2` interface that accepts a `PrintStream` and default implementation `DefaultUsageFormatter2`.
613664
* Deprecated interface `UsageFormatter` and methods `CmdlineParser.usage(StringBuilder)` and CmdlineParser.setUsageFormatter(UsageFormatter)
614665
* Use Polyglot Maven (Scala) as build system
666+
* Added support for aggregated short options via new method `CmdlineParser.setAggregateShortOptionsWithPrefix(String)`
615667

616668
=== CmdOption 0.4.2 - 2015-06-02
617669

0 commit comments

Comments
 (0)