Able to access options set by global_option() outside of the command block
ggilder opened this issue · 8 comments
From @pruan-rht on February 16, 2015 18:0
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
Copied from original issue: tj/commander#94
From @akostadinov on February 16, 2015 18:17
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?
@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.
Also, I had some suggestions for a similar problem on this issue: tj/commander#81
Please take a look and let me know if that addresses your use case or not.
From @pruan-rht on February 16, 2015 22:56
@ggilder, thanks for getting back so fast. Unfortunately, your suggestioin on #81 does not address the issue.
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.
From @pruan-rht on February 16, 2015 23:27
@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!
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.
From @pruan-rht on February 17, 2015 0:59
@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.