moss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 54f9b22e47e4bc28a34896635f08e794b8f9154c
parent f321e8c8b647749916e6a6ffa18bfe5f43b95b6f
Author: Daniel Barlow <dan@telent.net>
Date:   Fri, 13 Aug 2021 14:24:13 +0100

add comments to remind me what it does

Diffstat:
Mmoss.rb | 21++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/moss.rb b/moss.rb @@ -164,7 +164,12 @@ end class CLI class << self + # This would be more robust with class instance variables instead + # of class variables as used here, because different subclasses + # will share this hash. + @@commands = {} + def command(command_name, docstring, &blk) define_method command_name, &blk @@commands[command_name] = { name: command_name, doc: docstring } @@ -186,9 +191,19 @@ class CLI raise UsageError,"moss: unrecognised command. See \"moss help\"" end - arg_signature = method(command[:name]).parameters + method_signature = method(command[:name]).parameters + + return [name, []] if method_signature.empty? + + # The method we dispatch to expects either a varargs (splat) + # array, or a hash in which every argument is named. So if there + # is a method blah(peter:, paul:, mary:) and the user runs + # `blah jones tall --mary=contrary`, first we look for + # the named arguments (mary => contrary), then we assign the remaining + # parameters (peter => jones, paul => tall) from the argument array + # in the order they appear in the method signature - return [name, []] if arg_signature.empty? + # (I've never used JCL, but I was once an Amiga programmer) flags = argv.reduce({}) {|m, flag| key, val= flag.match(/\A--(\w+)(?:=(.+))?/)&.captures @@ -197,7 +212,7 @@ class CLI m } - payload = arg_signature.reduce({}) { |m, (type, name)| + payload = method_signature.reduce({}) { |m, (type, name)| case type when :rest args