GithubHelp home page GithubHelp logo

jimmycuadra / lita-keyword-arguments Goto Github PK

View Code? Open in Web Editor NEW
13.0 5.0 2.0 174 KB

A Lita extension to extract CLI-style arguments from messages.

License: MIT License

Ruby 100.00%
ruby lita lita-extension chatops

lita-keyword-arguments's Introduction

lita-keyword-arguments

lita-keyword-arguments is an extension for Lita that extracts keyword arguments from messages in the style of command line flags.

Installation

Add lita-keyword-arguments to your Lita plugin's gemspec:

spec.add_runtime_dependency "lita-keyword-arguments"

Require it in your Lita plugin's source:

require "lita-keyword-arguments"

Usage

Define keyword arguments for a route using the kwargs option. The value should be a hash, mapping keywords to a hash detailing the rules about that keyword.

When the route matches, the response object passed to the handler method will have the :kwargs key in its extensions attribute populated with the parsed keyword argument values.

Example:

class MyHandler < Lita::Handler
  route(
    /^my_command/,
    :callback
    command: true,
    kwargs: {
      foo: {},
      bar: {
        short: "b",
        default: "unset"
      },
      verbose: {
        short: "v",
        boolean: true
      }
    }
  )

  def callback(response)
    # response.extensions[:kwargs] will be populated with a hash of keywords and their values.
  end
end

The above :kwargs hash would make lita-keyword-arguments recognize the following in messages:

[--foo VALUE] [-b | --bar VALUE] [-v | --verbose | --no-verbose]

The :bar keyword be set to the string "unset" if no value was provided in the message.

The possible keys for each keyword argument's specification are:

  • :short - A single letter to use for the short flag. Invoked with a single preceeding dash. For example: "-f".
  • :boolean - The kwarg represents a boolean and does not have an argument. Set to true by providing the flag. Set to false by providing the long version of the flag, prefixing the keyword with "no-". For example: "--no-verbose".
  • :default - A default value to give the keyword argument if the flag is not provided in the message.

The long flag (e.g. --foo) is automatically created from the key.

Example messages and their resulting hashes:

# Lita: my_command -b hello
{ bar: "hello" }

# Lita: my_command --foo baz
{ foo: "baz", bar: "unset" }

# Lita: my_command -v
{ bar: "unset", verbose: true }

# Lita: my_command --no-verbose
{ bar: "unset", verbose: false }

License

MIT

lita-keyword-arguments's People

Contributors

jimmycuadra avatar pusewicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

chaner darthmanwe

lita-keyword-arguments's Issues

Provide option to reject unrecognized flags

Would be great if there were the ability to throw an error or provide some sort of signal to the client of this code that there were some arguments that were provided but weren't supplied in the schema.

So for example if I specified that my lita plugin wanted to support --user and the user actually typed in -usr, i'd like to be able to detect that.

Ideally this would be an option we could configure lita-keyword-arguments with so that it would not break existing behavior.

Detect empty argument values

It would be great to be able to detect if a flag that needs an argument got called without one.
Example:
-n is an optional flag that needs an argument to make sense to the command.
So it would be ok to do
command
But I'd like to throw an error in these cases:
command -n (empty flag argument)
command -a -n -b (again empty, in between other flags)

Currently, in all the above cases the value of -n is nil or whatever I specify as default.

It looks like slop released a new version that isn't backwards compatible ...

I use this extension in my rundeck handler and it got the exception below. I happened to notice that the slop rubygem had recently released a 4.0and the lita-keyword-arguments' rubyspec was set to depend on slop >= 3.5. So I pinned slop to 3.5 and all was well again.

[2015-01-12 14:13:32 UTC] ERROR: Lita::Handlers::Rundeck crashed. The exception was:
undefined method `new' for Slop:Module
Full backtrace:
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments/parser.rb:36:in `parser'
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments/parser.rb:32:in `add_option'
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments/parser.rb:15:in `block in parse'
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments/parser.rb:15:in `each'
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments/parser.rb:15:in `parse'
/opt/lita/vendor/ruby/2.1.0/gems/lita-keyword-arguments-0.0.1/lib/lita/extensions/keyword_arguments.rb:9:in `call'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/handler/chat_router.rb:86:in `block in dispatch_to_route'
/usr/lib/ruby/2.1.0/set.rb:263:in `each_key'
/usr/lib/ruby/2.1.0/set.rb:263:in `each'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/handler/chat_router.rb:86:in `dispatch_to_route'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/handler/chat_router.rb:73:in `block in dispatch'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/handler/chat_router.rb:70:in `map'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/handler/chat_router.rb:70:in `dispatch'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/robot.rb:58:in `block in receive'
/usr/lib/ruby/2.1.0/set.rb:263:in `each_key'
/usr/lib/ruby/2.1.0/set.rb:263:in `each'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/robot.rb:55:in `map'
/opt/lita/vendor/ruby/2.1.0/gems/lita-4.0.4/lib/lita/robot.rb:55:in `receive'
/opt/lita/vendor/ruby/2.1.0/gems/lita-hipchat-2.1.1/lib/lita/adapters/hipchat/callback.rb:20:in `block in private_message'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/callbacks.rb:99:in `call'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/callbacks.rb:99:in `block in process'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/callbacks.rb:98:in `each'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/callbacks.rb:98:in `process'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/stream.rb:277:in `receive'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/streamparser.rb:74:in `block in parse'
/usr/lib/ruby/2.1.0/rexml/parsers/sax2parser.rb:142:in `call'
/usr/lib/ruby/2.1.0/rexml/parsers/sax2parser.rb:142:in `block in parse'
/usr/lib/ruby/2.1.0/rexml/parsers/sax2parser.rb:142:in `each'
/usr/lib/ruby/2.1.0/rexml/parsers/sax2parser.rb:142:in `parse'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/streamparser.rb:91:in `parse'
/opt/lita/vendor/ruby/2.1.0/gems/xmpp4r-0.5.6/lib/xmpp4r/stream.rb:75:in `block in start'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.