What arguments can an EK9 program accept?

← Syntax and Structure Rules · Ref: Q889

EK9 programs support two argument styles, but you cannot mix them in a single program.

STYLE 1: C-STYLE LIST

  MyProgram()
    -> argv as List of String
    // Parse arguments manually

STYLE 2: TYPED PARAMETERS

  MyProgram()
    ->
      name as String
      count as Integer
    // EK9 parses from command-line strings automatically

CANNOT MIX

  MyProgram()
    ->
      count as Integer
      argv as List of String    // ERROR: E07610

Mixing typed parameters with List of String is ambiguous.

ALLOWED TYPED PARAMETERS

Only types that can be parsed from command-line strings: String, Integer, Float, Boolean, Date, DateTime, Duration, Millisecond, Colour, Dimension, Money, Path, RegularExpression.

See Q871 for hello world. See Q888 for program return type.

Example

defines module qa.syntaxrules.programarguments

  defines program

    <?-
      Program using typed arguments.
      EK9 automatically parses command-line strings to the declared types.
    -?>
    ProgramArgumentsDemo()
      ->
        userName as String
        repeatCount as Integer

      stdout <- Stdout()

      for i in 1 ... repeatCount
        stdout.println(`${i}: Hello, ${userName}`)

Common mistakes

E07610 — You cannot mix typed parameters with List of String in a program. Choose one style: either a single List of String parameter, or typed parameters that EK9 parses automatically. See ek9 -h E07610 for details.

Incorrect:

      argv as List of String

Correct:

      userName as String
Other ways to ask this
  • What triggers E07610 PROGRAM_ARGUMENTS_INAPPROPRIATE?
  • Why can't I mix List of String with typed parameters?
  • How do EK9 program argument styles work?

Coming from another language?

Java: String[] only, all parsing manual. Python: sys.argv is list of strings, argparse for typed. Go: os.Args is []string, flag for typed. Rust: std::env::args(), clap for typed. EK9: two styles — C-style List of String or compiler-parsed typed parameters, but not both.

Keywords: mix, String, parameter, line, List, argument, command, program, E07610, typed