/j-arguments

java-core-library

Primary LanguageJava

j-arguments

java-core-library

About

This module contains classes for representing, building, creating, parsing and formatting options and arguments.

Documentation

This section provides documentation for the com.github.wnebyte.jarguments.util.ArgumentFactory class.

create parameters

name

If name is set to null and required is set to true a positional argument will be created.
If name is not null then required will determine whether an optional or required argument is created.
If name is not null and required is false and type is set to boolean, a flag argument (special optional argument) will be created.
Multiple names can be specified by separating them with a ','.

Create a positional argument of type String:

factory.create(null, null, true,
        null, null, null, String.class);

Create an optional argument of type String:

factory.create("-f, --foo", null, false,
        null, null, null, String.class);

Create a required argument of type String:

factory.create("-f, --foo", null, true,
        null, null, null, String.class);

Create a flag argument:

factory.create("-f, --foo", null, false,
        null, null, null, boolean.class);

description

The description value is a string containing a brief description of the given argument. When a user requests help (), these descriptions will be displayed with each argument.

factory.create("-f, --foo", "this is a brief description", false,
        null, null, null, String.class);

required

Required arguments have to be included at the command line. Optional arguments on the other hand can be omitted.

factory.create("-f, --foo", null, true,
        null, null, null, String.class);

choices

Is used to constrain the value of an argument to a set of string values. Will not be applied on default values or on flag arguments.

factory.create(null, null, true,
        String[]{"rock", "paper", "scissors"}, null, null, String.class);

metavar

Is used by the help message formatter when referencing the given argument.

factory.create(null, null, true,
        null, null, "FOO", String.class);

defaultValue

All optional arguments may be omitted at the command line. If omitted the argument will be initialized using a default value.
If this flag is not set; a default value returned by typeAdapter will be used instead.

type

The type of the argument.

typeAdapter

Is an interface used to convert a string value into an instance of type. The ArgumentFactory class's default AbstractTypeAdapterRegistry has implementations for and can convert the following types:

  • primitive types
  • wrapper classes
  • arrays where the component type is a primitive type
  • arrays where the component type is a wrapper class

constraints

The Constraint interface can be used to apply arbitrary constraints on an initialized argument. Will not be applied on default values or on flag arguments.

factory.create("-f, --foo", null, true,
        null, null, null, String.class, new ConstraintCollectionBuilder<String>()
            .addConstraint(new Constraint<String>() {
                @Override
                public boolean test(String s) {
                    return (s.length() == 3);
                }
                @Override
                public String errorMessage() {
                    return "length is not equal to 3."
                }
            })
            .build());