Class ArgsVar


public class ArgsVar extends DefaultingVar
JVM args variable resolver.

The format for this var is "$A{arg[,default]}".

The argument can be either a positional index (e.g. "$A{0}") or a named option (e.g. "$A{port}").

This variable resolver requires that the command-line arguments be made available through any of the following:

  • "sun.java.command" system property.
  • "juneau.args" system property.
  • The instance was created via create(Supplier).
Example:

// Create an args object from the main(String[]) method. Args args = new Args(argv); // Create a variable resolver that resolves JVM arguments (e.g. "$A{1}") VarResolver varResolver = VarResolver.create().vars(ArgsVar.create(() -> args)).build(); // Use it! System.out.println(varResolver.resolve("Arg #1 is set to $A{1}"));

Since this is a SimpleVar, any variables contained in the result will be recursively resolved. Likewise, if the arguments contain any variables, those will be resolved before they are passed to this var.

See Also:
  • Field Details

  • Constructor Details

    • ArgsVar

      public ArgsVar()
      Constructor.
  • Method Details

    • create

      public static ArgsVar create(Supplier<Args> supplier)
      Creates an ArgsVar bound to a per-instance Supplier of Args.

      The supplier is invoked on every resolve, so callers can update the underlying Args between resolves if needed.

      Parameters:
      supplier - The supplier of Args for this var instance. Must not be null.
      Returns:
      A new ArgsVar instance backed by the supplier.
    • resolve

      public String resolve(VarResolverSession session, String key)
      Description copied from class: Var
      The interface that needs to be implemented for subclasses of SimpleVar.
      Specified by:
      resolve in class Var
      Parameters:
      session - The session object used for a single instance of a var resolution.
      key - The inside argument of the variable.
      Returns:
      The resolved value.
    • isStable

      protected boolean isStable()
      Process command-line args are immutable for the lifetime of the JVM, so the no-arg constructor opts in to compile-time stable-value folding.

      The create(Supplier) variant returns false since the supplier may return updated args between resolves.

      Overrides:
      isStable in class Var
      Returns:
      true for the default no-arg constructor; false for the create(Supplier) variant.