Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to define parameters in module? #44

Closed
thedeeno opened this issue Apr 30, 2014 · 1 comment
Closed

Is it possible to define parameters in module? #44

thedeeno opened this issue Apr 30, 2014 · 1 comment

Comments

@thedeeno
Copy link

Thanks for your work on this project mdub! I really like it. Question: Is it possible to share parameters via modules. I've tried the following but the parameter doesn't appear in the help for my command nor does its attribute get set. What am I missing?

module SharedParameters
  extend Clamp::Parameter::Declaration
  parameter "NAME", "name of thing"
  def thing
     System.get(name).status 
  end
end

class StatusCommand
  include ShareParameters
  def execute
     puts thing
  end 
end

class MainCommand < Clamp::Command
  subcommand "status", "show status of thing", StatusCommand
end
@mdub
Copy link
Owner

mdub commented Apr 30, 2014

No, it's not possible to include parameter declarations via a module, in the way you'd include options.

You'll have to use a different trick to declare shared parameters, e.g.

Clamp do

  class << self
    def declare_name_parameter
      parameter "NAME", "name of thing"
    end
  end

  subcommand "foo", "Do foo" do
    declare_name_parameter
    def execute
      puts "foo"
    end
  end

end

Another alternative is to declare the parameter before subcommands, if it applies to all of them. This is the approach I prefer, though it does alters the usage:

Clamp do

  parameter "NAME", "name of thing"

  subcommand "foo", "Do foo" do
    def execute
      puts "foo"
    end
  end

end

@mdub mdub closed this as completed Apr 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants