-
Type:
Suggestion
-
Resolution: Answered
-
Component/s: None
-
None
NOTE: This suggestion is for JIRA Cloud. Using JIRA Server? See the corresponding suggestion.
Currently args4j does not support default values. This can be very handy especially if used in conjunction with a special handler.
An example:
@Option(name="--index" , aliases="-i" , usage="index bean (index)" , metaVar="<index>", defaults="index", handler=OptionHandlerBean.class)
private Index index;
The OptionHandlerBean accesses an application context to receive an Index instance. If it is not specified, the OptionHandlerBean is invoked as if "index" would be given as an option. Of course you could give a default value for the index variable but this does not address a situation where the default value must be closed.
An other example is an OutputStream:
@Option(name="--out", handler=OptionHandlerOutputStream.class)
private OutputStream outputStream = new FileOutputStream("fufu");
here, the benefit of a default value becomes obvious: the OutputStream to "fufu" will not be closed once the option is defined.
Right now, I am using a patched version of args4j to address this situation:
Option.java:
/**
- Defines a default value that will be applied if this option was not set
- explicitely.
*/
String[] defaults() default { };
OptionDef.java:
private final String[] defaults;
public OptionDef(Argument a, boolean forceMultiValued)
{ this(a.usage(), a.metaVar(), a.required(), a.hidden(), a.handler(), a.multiValued() || forceMultiValued, null);//XXX overlay } protected OptionDef(String usage, String metaVar, boolean required,
boolean hidden, Class<? extends OptionHandler> handler,
boolean multiValued, String[] aDefaults)
public String[] getDefaults()
{ return Arrays.clone(defaults); }NamedOptionDef.java:
public NamedOptionDef(Option o) {
super(o.usage(),o.metaVar(),o.required(),o.hidden(),o.handler(), false, o.defaults());
...
and I am using an extended CmdLineParser that handles the defaults values. Unfortunately I am forced to use Reflection to access the "options" value in CmdLineParser.java (since it is private).
If you are interested I can provide the full source of my patch.
- is related to
-
JRASERVER-34305 Default values
- Closed