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

Availability of tool's private methods in accept (acceptor) #103

Open
AlexWayfer opened this issue Nov 11, 2020 · 1 comment
Open

Availability of tool's private methods in accept (acceptor) #103

AlexWayfer opened this issue Nov 11, 2020 · 1 comment

Comments

@AlexWayfer
Copy link
Contributor

AlexWayfer commented Nov 11, 2020

Hello.

I want to define option with a processing in specific cases, like:

required_arg :new_version, accept: (lambda do |value|
  case value
  when 'major'
    "#{Integer(current_version.split('.').first) + 1}.0.0"
  else value
  end
end)

And current_version is a private method inside tool. And I can't access it, sadly.

How to better handle this case?

Also, I figured out that handler is unavailable for arguments.

Also I've tried to add attr_writer :new_version, but new_version getter is not regular, so it's not synchronized (sadly, again).

@dazuma
Copy link
Owner

dazuma commented Jan 3, 2021

Regarding current_version

You can't access methods of your tool from an accept function because it is called while command line arguments are being parsed, and the tool hasn't been constructed yet at that point. However, you should be able to call a class method of the tool, because, just like any class definition, self is pointing at the class.

tool "my-tool" do
  def self.current_version
    "1.2.3"
  end

  required_arg :new_version, accept: (lambda do |value|
    case value
    when 'major'
      "#{Integer(current_version.split('.').first) + 1}.0.0"
    else value
    end
  end)
end

Regarding handler for arguments

We could add a handler for arguments, but I didn't think there was a use case for it. It's generally useful if something could be set more than once (e.g. you have a flag that is given multiple times, and you use a handler to combine the values). But an argument can never be given more than once. Can you describe why you would use handler for an argument?

Regarding attr_writer

That I agree could be confusing, if you assume that the field is an attr_reader and uses an instance variable for its value. As you noted, that's not true. Fields are intentionally separate from instance variables. That was a design decision because I needed to support fields whose names are arbitrary objects (for special fields, so they don't have a chance of colliding with user-defined fields) and it would be even more confusing for some fields to be backed by instance variables while others are not.

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