Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Advanced command parser and builder #58

@TheElectronWill

Description

@TheElectronWill

Draft

Format

command param1 -flag --explicit value --multiple v1 v2 v3

Parameters types

  • flags: boolean value present/absent
  • named arguments: arbitrary value, ex. param1
  • explicit arguments: --name value with one or two dashes
  • multivalued arguments: --multi v1 v2 v3 ..., each value is separated by a space

Quotes

Values can be quoted to include spaces.

"single value"
'another value'
"you can use single 'quotes' in double-quoted strings and vice-versa"

Subsitution

Use $var or ${expr} or $(expr) in your command and it will be replaced by the corresponding value.

Escapements

  • \n: newline
  • \t: tab
  • \* where * is any other character: this character as-is, not interpreted by the parser

Example

warp [set|del] home [x,y,z[,world]] [--public]

Command definition

createCommand("warp", new ManagedCommand {
  optArg("instruction")
  arg("name")
  optArg("location", LocationParser)
  optFlag("public")
  
  def execute(by: Actor, args: ArgsMap, unnamed: Seq[String]) = {
    val name = args("name") // String
    val location = args.get("location") // Option[Location]
    val isPublic = args("public") // Boolean
    val warp = findWarp(by, name)

    args("instruction") match { // Option[String]
      case None => by ! Teleport(warp.location)
      case Some("set") => ...
      case Some("del") => ...
    }
})

Implementation notes

To handle an optional optArg preceding a required arg, the parser should count the number of parameters and fill the optional arguments, in the definition order, only if there are more arguments than required.

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions