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

Able to access options set by global_option() outside of the command block #94

Closed
pruan-rht opened this issue Feb 16, 2015 · 9 comments
Closed

Comments

@pruan-rht
Copy link

Sometime, it's useful to be able to access options that at set in the global_option() be accessible outside of the command block so that I can do initialization of codes that are global to all sub commands. For example

global_option('-p', '--plan_id PLAN_ID_OR_NAME', 'specify the plan name or plan id')

command :rename do |c|
...
c.action do |arg, options|
...

I want to check the option plan_id outside of the c.action block for the various commands that this global_option applies to

@akostadinov
Copy link

I am thinking that a separate initialization block could be defined by user. And if that block is present, then it should be executed before the block of the command is called. Would such modification to commander be possible?

@ggilder
Copy link
Collaborator

ggilder commented Feb 16, 2015

@pruan-rht Can you put together a code sample for how you would want to use the global option? I'm not sure why you need to access the option outside of the command block.

@akostadinov your suggestion seems to address a different issue; and I'm not really sure what use case that helps with. Again, a code sample of intended usage would be great for discussion.

@ggilder
Copy link
Collaborator

ggilder commented Feb 16, 2015

Also, I had some suggestions for a similar problem on this issue: #81

Please take a look and let me know if that addresses your use case or not.

@pruan-rht
Copy link
Author

@ggilder, thanks for getting back so fast. Unfortunately, your suggestioin on #81 does not address the issue.

@ggilder
Copy link
Collaborator

ggilder commented Feb 16, 2015

Sorry to hear that. If you can provide a code sample to show what you'd like to have, I'm sure we can figure something out.

@pruan-rht
Copy link
Author

@ggilder, below is an example. Without being able to access the global_option outside of the sub commands, I would need to add the line 'update_plan_id(options) if options.plan_id' to each of the supported commands. I deally, I want to have that line executed globally once, so I don't need to add that line for all of the sub-commands. Hope that clarifies the issue at hand. thanks in advance

def run
....
global_option('-p', '--plan_id PLAN_ID_OR_NAME', 'specify the plan name or plan id')

  default_command :add
  command :add do |c|
      ...
    c.action do |args, options|
      ...
      update_plan_id(options) if options.plan_id
      ...
      res = @tcms.add_testcase_tags(case_ids, tags)
    end
  end

  command :remove do |c|
     ...
    c.action do |arg, options|
      ...
      update_plan_id(options) if options.plan_id
      ...
      res = @tcms.remove_testcase_tags(case_ids, tags)
    end
  end

  command :rename do |c|
     ...
      update_plan_id(options) if options.plan_id
     ...
      res = @tcms.add_testcase_tags(case_ids, options.new_tag)
      raise "Add tag operation failed for cases #{casd_ids}" unless res.nil?
      res = @tcms.remove_testcase_tags(case_ids, options.old_tag)
      ...
    end
  end
  run!

@ggilder
Copy link
Collaborator

ggilder commented Feb 17, 2015

Thanks, I think this and @akostadinov 's comments on #81 have clarified the use case. In your case, though, could you just use the existing facility to provide a block for a global option? I'm wondering if something like this would work:

def run
  # ...
  global_option('-p', '--plan_id PLAN_ID_OR_NAME', 'specify the plan name or plan id') do |plan_id|
    update_plan_id(plan_id)
  end
  # commands etc.
end

That wouldn't work, however, if update_plan_id needs other global options.

@pruan-rht
Copy link
Author

@ggilder, you are correct, that will work for my particular case fortunately since the update_plan method doesn't need other global_options. Thank you for the pointer...I think this would be a good example to put into the README/example section to cut down the noise to you. Anyway, I still think it would be good to have a way to access the global_options for the other use case.

@ggilder
Copy link
Collaborator

ggilder commented Apr 1, 2015

This issue was moved to commander-rb/commander#7

@ggilder ggilder closed this as completed Apr 1, 2015
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

3 participants