abstract class util::AbstractMain

sys::Obj
  util::AbstractMain

Source

AbstractMain provides conveniences for writing the main routine of an app. Command line arguments are configured as fields with the Arg facet:

@Arg { help = "source file to process" }
File? src

Arguments are ordered by the field declaration order. The last argument may be declared as a list to handle a variable numbers of arguments.

Command line options are configured as fields with the Opt facet :

@Opt { help = "http port"; aliases=["p"] }
Int port := 8080

Bool fields should always default to false and are considered flag options. All other arg and opt fields must be a Str, File, or have a type which supports a fromStr method.

Option fields may include the "Opt" suffix, and arguments the "Arg" suffix. These suffixes can be used when a field conflicts with an existing slot name.

AbstractMain will automatically implement usage and parseArgs based on the fields which are declared as options and arguments. In general subclasses only need to override run. If writing a daemon main, then you'll probably want to configure your services then call runServices.

See example.

appName

virtual Str appName()

Source

Get the application name. If this is a script it is the name of the script file. For a precompiled class called "Main" this is the pod name, otherwise it is the type name.

argFields

virtual Field[] argFields()

Source

Get all the fields annotated with the @Arg facet.

helpOpt

@Opt { help="Print usage help"; aliases=["?"] }
Bool helpOpt := false

Source

Print usage help.

homeDir

virtual File homeDir()

Source

Home directory for the application. For a script this defaults to directory of the script. For pods the default is "{Env.cur.workDir}/etc/{pod}".

log

virtual Log log()

Source

Log for this application; defaults to appName.

main

virtual Int main(Str[] args := Env.cur().args())

Source

Main performs the following tasks:

  1. Calls parseArgs to parse command line
  2. If args were incomplete or -help was specified then dump usage and return 1
  3. Call run and return 0
  4. If an exception is raised log it and return 1
optFields

virtual Field[] optFields()

Source

Get all the fields annotated with the @Opt facet.

parseArgs

virtual Bool parseArgs(Str[] toks)

Source

Parse the command line and set this instances fields. Return false if not all of the arguments were passed.

printProps

static Void printProps(Str:Obj props, [Str:Obj]? opts := null)

Source

Print a map of key/value pairs to output with values justified. Values that are lists are indented and separated by newlines. Options:

  • out: OutStream to print to (default is Env.cur.out)
  • indent: Str spaces (default is "")
run

abstract Int run()

Source

Run the application. This method is called after the command line has been parsed. See runServices to launch a deamon application. Return status code, zero for success.

runServices

virtual Int runServices(Service[] services)

Source

Run the set of services:

  1. call install on each service
  2. call start on each service
  3. put main thread to sleep.
  4. on shutodwn call stop on each service
  5. then call uninstall on each service
runtimeProps

static Str:Obj runtimeProps([Str:Obj]? acc := null)

Source

Get map of the standard runtime props including:

  • java version info if JVM
  • node version if running in NodeJS
  • fan version Map vals are Strs or Lists to indent. Also see printProps
usage

virtual Int usage(OutStream out := Env.cur().out())

Source

Print usage of arguments and options. Return non-zero.