Created
June 17, 2015 20:58
-
-
Save abargnesi/d678e35635dde1a677e6 to your computer and use it in GitHub Desktop.
Using trollop for subcommands; delegates to other command script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'bel' | |
| require 'bel/vendor/trollop' | |
| VERSION_BANNER = <<-VERSION | |
| bel #{BEL::VERSION} | |
| Copyright (C) 2015 OpenBEL | |
| Apache License, Version 2.0, January 2004 | |
| http://www.apache.org/licenses/ | |
| VERSION | |
| module BEL | |
| module Commands | |
| # Defines the outer command. Stops on unknown arguments which may | |
| # be a sub command. | |
| class BelCommand | |
| def run | |
| Trollop::options do | |
| synopsis "A set of commands to process BEL knowledge." | |
| usage "Usage: #{__FILE__} [OPTIONS]... COMMAND" | |
| version ::VERSION_BANNER | |
| opt :verbose, 'Verbose output.', | |
| :long => :verbose, | |
| :short => :v | |
| stop_on_unknown | |
| end | |
| BelCommand.fail_unless_subcommand | |
| end | |
| # If +ARGV+ is empty after the call to +Trollop::options+ then a | |
| # sub command argument was not given. +Trollop::educate+ will then | |
| # show help. | |
| def self.fail_unless_subcommand | |
| Trollop::educate if ARGV.empty? | |
| end | |
| end | |
| # The +parse+ command. | |
| class ParseCommand | |
| def run | |
| # Parse the expected options. | |
| parsed_options = | |
| Trollop::options do | |
| synopsis "Shows parse of BEL script." | |
| usage "Usage: #{__FILE__} [OPTIONS]... parse --bel [FILE]" | |
| opt :bel, 'The BEL script to parse and output. If not provided the default is to read from standard in (STDIN).', | |
| :type => String, | |
| :long => :bel, | |
| :short => :b, | |
| :default => nil | |
| end | |
| # If the --bel option was specified, unshift back to ARGV | |
| # and call +bel_parse.rb+. | |
| if parsed_options[:bel] | |
| binding.pry | |
| ARGV.concat(['--bel', parsed_options[:bel]]) | |
| else | |
| Trollop::educate if !ARGV.empty? | |
| end | |
| require_relative 'bel_parse' | |
| end | |
| end | |
| end | |
| end | |
| # ARGV, now chewed in sequence. | |
| # First break off the global options; stopping on unknown args. | |
| BEL::Commands::BelCommand.new.run | |
| # Assume unknown arg is a sub command and compute sub command | |
| # class constant. | |
| sub_command = ARGV.shift | |
| sub_class = :"#{sub_command.capitalize}Command" | |
| # Call sub command if the constant exists otherwise provide help. | |
| if BEL::Commands.const_defined?(sub_class) | |
| BEL::Commands.const_get(sub_class).new.run | |
| else | |
| $stderr.puts "error: The command #{sub_command} is not valid.\n\n" | |
| Trollop::educate | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice example. Do you consume any of the BelCommand class global options in another command?