Command line option parser like heroku cli

Hi,

I would like to build a cli tool with commands/subcommands in the
style of the heroku cli, for example:

cli.rb command:subcommand

so like heroku cli that would be

heroku auth:token

commands such as:

cli.rb command

should also be possible.

Is there any gem for that? or is this maybe possible with Ruby's
integrated OptionParser?

Best regards,
J.

I guess you’re searching for Thor: http://whatisthor.com/

  ngw

···

On 17 Mar 2017, at 09:34, John Naggets <hostingnuggets@gmail.com> wrote:

Hi,

I would like to build a cli tool with commands/subcommands in the
style of the heroku cli, for example:

cli.rb command:subcommand

so like heroku cli that would be

heroku auth:token

commands such as:

cli.rb command

should also be possible.

Is there any gem for that? or is this maybe possible with Ruby's
integrated OptionParser?

You can do that with OptionParser although I am not sure there is a
mechanism for hirarchical option processing built in. Basically you
can start with a global OptionParser, then identify the command and
then use a per command option parser. Roughly like

OptionParser.new do |opts|
...
end.parse!

command = ARGV.shift

case command
when "save"
  OptionParser.new do |opts|
    ...
  end.parse!

  # execute save...
  ARGV.each {|file| save file}
when "load"
  OptionParser.new do |opts|
    ...
  end.parse!

  # execute load...
  load ARGV
else
  abort "ERROR: unknown command: #{command}"
end

Just to give you some idea.

Kind regards

robert

···

On Fri, Mar 17, 2017 at 10:34 AM, John Naggets <hostingnuggets@gmail.com> wrote:

Hi,

I would like to build a cli tool with commands/subcommands in the
style of the heroku cli, for example:

cli.rb command:subcommand

so like heroku cli that would be

heroku auth:token

commands such as:

cli.rb command

should also be possible.

Is there any gem for that? or is this maybe possible with Ruby's
integrated OptionParser?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Thanks to both of you for your input, I will try out the trick with
the OptionParser as soon as I have time. I have also found a gem
called cmdparser which looks easier than thor and should do the job.

···

On Fri, Mar 17, 2017 at 12:48 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Fri, Mar 17, 2017 at 10:34 AM, John Naggets <hostingnuggets@gmail.com> wrote:

Hi,

I would like to build a cli tool with commands/subcommands in the
style of the heroku cli, for example:

cli.rb command:subcommand

so like heroku cli that would be

heroku auth:token

commands such as:

cli.rb command

should also be possible.

Is there any gem for that? or is this maybe possible with Ruby's
integrated OptionParser?

You can do that with OptionParser although I am not sure there is a
mechanism for hirarchical option processing built in. Basically you
can start with a global OptionParser, then identify the command and
then use a per command option parser. Roughly like

OptionParser.new do |opts|
...
end.parse!

command = ARGV.shift

case command
when "save"
  OptionParser.new do |opts|
    ...
  end.parse!

  # execute save...
  ARGV.each {|file| save file}
when "load"
  OptionParser.new do |opts|
    ...
  end.parse!

  # execute load...
  load ARGV
else
  abort "ERROR: unknown command: #{command}"
end

Just to give you some idea.

Kind regards

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;