Does mixlib-cli support shared modules?


#1

Hello,

Asking here because I’m not really sure where else to ask.

I’m writing something in ruby that requires a CLI tool component. I like how easy it is to configure Mixlib-cli but there’s a use case which I’d like and I’m not sure is supported.

I’d like to be able to write Mixlib modules in order to enable re-usable code. For example if I’m writing a tool where some operations require interaction with AWS I’d like something like the following module so that I can just include the options and they’re consistent:

require 'mixlib/cli'

module MyCLI
  module AWS
    include Mixlib::CLI

    option :aws_account,
      long: '--aws-account AWS_ACCOUNT',
      description: 'The name of the AWS account to deploy into'

    option :aws_region,
      long: '--aws-region AWS_REGION',
      description: 'The name of the AWS region to deploy into',
      default: 'us-west-2'
  end
end

module MyCLI
  class AwsTool
    include Mixlib::CLI
    include CLI::AWS # This is what I want, or behaviour like it

    options :tool_specific_option,
      short: '-a FOO',
      description: 'Something specific to this tool'
  end
end

cli = MyCLI::AwsTool.new
cli.parse_options # I want to support (-a, --aws--account and --aws-region)

This doesn’t work in with the above code if I include the above module in a class that has Mixlib::CLI included. A cursory glance of the code seems to indicate it could/should work (it seems to just stores options in @options, which the module should share when included), so I was wondering if there was a way to hook this up?


#2

Every module/class/subclass will have its own independent instance of the @options variable, which doesn’t affect the others.

The way we do this, which isn’t the most elegant is this:


#3

Thanks Dan, works well.

Nothing a code comment can’t explain too