No commit activity in last 3 years
No release in over 3 years
Though Trollop has the ability to support subcommands, I find myself implementing the same logic repeatedly. The abstraction of this logic is now in trollop-subcommands. This provides a framework for parsing command line options for ruby scripts that have subcommands. The format is 'script_name [global_options] subcommand [subcommand_options]'. The framework supports all the typical scenarios around these type of command line scripts. All that need to be specified are the trollop configurations for the global options and each subcommand options. See the readme for more information.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 3.3

Runtime

~> 2.1
 Project Readme

Trollop::Subcommands

This gem is an extension to Trollop. It provides a framework for commandline scripts that use the following subcommand pattern

my_script [global options] command [command options]

Usage

The framework abstracts setting up the Trollop parser to the sub command pattern displayed above. Just provide the Trollop options blocks for the global options and subcommands and you are good to go!

#!/usr/bin/env ruby
require 'trollop/subcommands'

# do not specify in the Trollop block `stop_on` or `stop_on_unknown`
# as the subcommand framework will do that for you
Trollop::Subcommands::register_global do
  banner <<-END
Usage
  my_script [global options] COMMAND [command options]

COMMANDS
  list               List stuff
  create             Create stuff

Additional help
  my_script COMMAND -h

Options
  END
  opt :some_global_option, 'Some global option', type: :string, short: :none
end

Trollop::Subcommands::register_subcommand('list') do
  banner <<-END
Usage
  #{File.basename($0)} list [options]

This is the list command...

Options
  END
  opt :all, 'All the things', type: :boolean
end

Trollop::Subcommands::register_subcommand('create') do
  banner <<-END
Usage
  #{File.basename($0)} create [options]

This is the create command...

Options
  END
  opt :partial, 'blah blah', type: :boolean
  opt :name,    'blah blah', type: :string
end

result = Trollop::Subcommands::parse!


puts "Command was #{result.subcommand}"
puts "Global options were #{result.global_options}"
puts "Subcommand options were #{result.subcommand_options}"

The here documents for the banners can distract the code a bit. It can help to abstract them to methods.

See the scenarios covered by the spec.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jwliechty/trollop-subcommands.

License

The gem is available as open source under the terms of the MIT License