How Wheaties reads commands typed in chat, as a Ruby gem, blolol-chat_command_syntax. blolol-web's dispatcher runs chat's commands with it, and the command runner uses it for v2 commands that re-parse their arguments and for command tests, which go through the same steps as chat.
Message.parse(event, nick:, discord_user_id:)finds what a chat event asks for: anInvocation(.weather Seattle, or!weatherfor a debug invocation) or anAssignment(Wheaties: foo is bar, or a Discord mention of Wheaties).Resolver#resolve(name, regexes: true)finds the command a name refers to: by its name, then a chat alias, then a name it had before a rename, then the first regex trigger, by priority, that matches the name.Resolveris the order. A subclass says where commands come from by implementingfind_by_name,find_by_alias,find_by_previous_name,regex_triggersandregex_command: blolol-web looks them up in Postgres, and command tests in their own set of commands.Resolver.compilecompiles a trigger's pattern with a timeout.Syntax.parse(text)parses a command's arguments into a tree, andEvaluatorturns the tree intoTokens, calling its block to run each[command args], innermost first. The syntax is Wheaties v2's (the Harby gem's grammar): words,"double quotes"(with brackets run inside them),'single quotes', numbers,/regexps/i,(lists)and[brackets]. It never fails: anything that doesn't parse is part of a word.Arguments.bind(tokens, schema, positionals: true)splits options (--name value,--name=value,-n value,--flag,--to end them) from positional arguments by the command's signature, converts them to the types it asks for, and checks them against it withSignature#bind.Signature#usage(name)says how to call the command, likeforecast [-u|--units <metric|imperial>] <place...>, for usage replies.
A signature can have subcommands (x-subcommands), each with its own signature, aliases (x-aliases) and description, plus a fallback for when the first word isn't one (x-fallback: main.rb or a subcommand) and Vim-style abbreviations (x-abbreviations). Signature#subcommand(name) is the signature a subcommand is called with, top-level options included, and Signature#usage(name) lists the subcommands, or shows one with subcommand:. Arguments.bind runs the subcommand the first bare word names (a name, an alias, then an abbreviation if they're on), or the fallback, and says which in its result's subcommand (and subcommand_word, the word that named it, or nil for the fallback); options before the subcommand's name consume their values. Code that invokes a command names its subcommand exactly, through Signature#target. Signature#usage_reply(name, result) is what chat is told when a result isn't valid.
A signature is a subset of JSON Schema with chat syntax in x- keywords (x-position, x-rest, x-short). blolol-web's Command::Signature defines the format and checks that a signature is valid when a command is saved.
It's a Git gem, pinned to a tag:
gem 'blolol-chat_command_syntax', git: 'https://github.com/blolol/chat-command-syntax.git', tag: 'v1.4.1'require 'blolol/chat_command_syntax'
include Blolol::ChatCommandSyntax
request = Message.parse(event, nick: 'Wheaties')
nodes = Syntax.parse(request.arguments)
tokens = Evaluator.new { |name, args| run(name, args) }.evaluate(nodes)
result = Arguments.bind(tokens, signature)
result.valid? # => true, or see result.errorsbundle install
bundle exec rake testA change that changes what chat means changes Wheaties for everyone, so release it as a new tag and update blolol-web and the command runner together.