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

accept Array isn't working #219

Closed
edusantana opened this issue Jun 23, 2015 · 4 comments · Fixed by #220
Closed

accept Array isn't working #219

edusantana opened this issue Jun 23, 2015 · 4 comments · Fixed by #220

Comments

@edusantana
Copy link
Contributor

I'm trying to make a flag that accepts an array splits at dots (.).

$ irb
2.2.1 :001 > 'a,b,c.d,e'.split(/,/)
 => ["a", "b", "c.d", "e"] 
2.2.1 :002 > 'a,b,c.d,e'.split(/\./)
 => ["a,b,c", "d,e"] 

Then looking at examples I have written this command to test:

command :testarray do |c|
    c.accept Array do |value|
      # puts "it doen't get here"
      value.split(/\./).map(&:strip)
    end
    c.flag :names, :desc => "list of names", :type => Array

    c.action do |global_options,options,args|
        puts options[:names]
    end
end

The problem happens when executing it:

$ bundle exec bin/gliarray testarray --names='a,b,c.d,e'
a
b
c.d
e

As you can see it doesn't split as expected. I am doing something wrong?

I'm using gli version 2.13.1.

@edusantana
Copy link
Contributor Author

With :type => Hash isn't working with me. I have copied it from lib/gli/app.rb:

c.accept(Hash) do |value|
  #puts "--- hash ---"
  result = {}
  value.split(/,/) do |pair|
    k,v = pair.split(/:/)
    result[k] = v
  end
  result
end
c.flag :siglas, :desc => "list of siglas", :type => Hash

And the output:

$ bundle exec bin/gliarray testarray --siglas="a:xxx,b:yyy"
error: unsupported argument type: Hash
$ bundle exec bin/gliarray testarray
error: unsupported argument type: Hash

@davetron5000
Copy link
Owner

I think that accept might need to be global, i.e. try

accept(Hash) do |value|
  #puts "--- hash ---"
  result = {}
  value.split(/,/) do |pair|
    k,v = pair.split(/:/)
    result[k] = v
  end
  result
end

before any of your command calls. (sorry, not in a position to verify this right now)

@edusantana
Copy link
Contributor Author

YES! That's it!

I will update the wiki to add a note and the comment inside the code.

The code was missing a each (value.split(/,/).each do |pair|):

accept(Hash) do |value|
  puts "--- hash ---"
  result = {}
  value.split(/,/).each do |pair|
    k,v = pair.split(/:/)
    result[k] = v
  end
  result
end

edusantana added a commit to edusantana/gli that referenced this issue Jun 23, 2015
Fixed commented code, it needs a `.each` to work.

closes davetron5000#219
@edusantana
Copy link
Contributor Author

@davetron5000 thanks for the replay.
I have edited the wiki and did a pull request with comments.

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

Successfully merging a pull request may close this issue.

2 participants