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
defines program MyProgram() -> argv as List of String stdout <- Stdout() stdout.println(`Arguments: ${argv}`)
STYLE 2: TYPED PARAMETERS (EK9 parses them from the command-line strings automatically)
defines program MyProgram() -> name as String count as Integer stdout <- Stdout() stdout.println(`${name} ${count}`)
CANNOT MIX
defines program MyProgram() -> count as Integer argv as List of String // ERROR: E07610 stdout <- Stdout() stdout.println(`${count} ${argv}`)
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, Character, Bits, Date, Time, DateTime, Duration, Millisecond, Colour, Resolution, Dimension, Money and RegEx (plus the single List of String of style 1). Anything else - Path included - is E07600.
See Q3 for printing output. 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: command, argument, List, line, parameter, String, mix, typed, E07610, program