Parameters and types

Parameters are used at two places: as subcommand values and as option values.

Standard value types

By default, args-inject supports:

  • java.lang.String - as you probably expect, it's passed as is
  • all primitive integer types - long, int, short, byte - using parse methods on their objectful cuonterparts
  • char - by taking the first character of the string; fails if there is more or less characters
  • java.util.Date and java.util.Callendar - experimental support, look at Timmy sample for more information

Custom value types

By default, number of common types is converted from commandline String values. Custom types can be enhanced to be automatically translated.

Following sections describe how it can be done.

static valueOf(String) method

Define static valueOf method which:

  • is public
  • returns type of its declaring class
  • takes exactly one argument, which is of type String

    Then this method will be used to produce instance of given class from a String.

    This makes, for instance, all enumerations to be usable out of the box.

public constructor with single argument of type String

Define constructor which

  • is public
  • takes exactly one argument, which is of type String

    This makes some existing value types usable - for instance, BigDecimal etc.

Lists

Working with lists in args-inject is supported two ways:

  • varargs in command parameter list - click the link to read more
  • lists separated by specified char (discussed here)

Lists are useful when you want to specify classpaths, exclussions, set of pages to print etc.

Lists are supported on parameters defined as java array.

You can use the @Param.listSeparator to explicitly set separator on given parameter; however, there is very nice algorithm to compute the default if you don't:

  • for parameters with component type java.io.File, the default equals to platform-dependent property path.separator - which is colon on Linux and semicolon on Windows
  • any other component types default to "," (comma) as the list separator.

Have a look at MyPathTool sample to see it in action.

Note that the above applies to any component type.

Symbolic names

When help prints parameter synopsis, it uses simple heuristic to derive a sensible name placeholder from the value type.

For instance, type java.io.File is printed as file, int is printed as int etc.

In most cases, it will be somewhat helpful to specify the @Param("myname") annottation to explicitly specify something more valid in the specific context.