17 Feb 2012, 14:22
Generic-user-small

Taro Fukunaga (8 posts)

Hi, I’m trying to rewrite my option parser app in GLI and I’m not sure how to do it.

What I want for the user to type is:

sysinfo get cpu sysinfo set hostname

In the generated scaffold code I tried adding the following:

c.desc ‘Get CPU info c.arg_name ‘cpu’

But I’m not sure how to check whether this argument was passed.

I tried this in the c.action block:

if args == “cpu” puts ‘cpu’ end

But nothing is printed.

17 Feb 2012, 15:43
Dmfcb_pragsmall

David Copeland (98 posts)

It looks like you’re wanting a multi-level command suite. GLI doesn’t support this directly (though it’s planned for GLI 2.0), but you could do it like so:

desc 'get info'
arg_name 'attribute_to_get'
command :get do |c|
  c.action do |global_options,options,args|
    raise "Argument required" if args.empty?
    case args[0]
      when 'cpu' # => whatever
      when 'hostname' # => whatever
    end
  end
end

And similar for @set@.

Another option would be to make your commands like @get_cpu@ and @set_hostname@, so you’d do

command :get_cpu do |c|
  c.action do |global,options,args|
    # => get the CPU
  end
end

command :set_hostname do |c|
  c.action do |global,options,args|
    # => set hostname
  end
end
17 Feb 2012, 16:35
Generic-user-small

Taro Fukunaga (8 posts)

David, thank you very much for a quick reply. I’ll explore these two options.

07 Dec 2012, 05:33
Generic-user-small

Dennis Sutch (13 posts)

Does GLI 2.5 support multi-level command suites? If so, are any examples on how to use this feature available?

08 Dec 2012, 00:31
Dmfcb_pragsmall

David Copeland (98 posts)

Yes! GLI2 does support what you are trying to do. You basically nest @command@ blocks inside one another. This code walktrough: http://davetron5000.github.com/gli/todo.html is a decent example, but post back here if you aren’t sure how to proceed.

01 Jan 2013, 02:21
Generic-user-small

Dennis Sutch (13 posts)

Thanks for the helpful documentation. Nested command blocks work as advertised.

  You must be logged in to comment