Last active
February 7, 2018 15:53
-
-
Save jasonroelofs/8117568 to your computer and use it in GitHub Desktop.
Is there any Ruby Command Line Parsing library out there that JUST DOES PARSING?
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
# The ones I've tried (almost all from http://www.awesomecommandlineapps.com/gems.html): | |
# | |
# OptParse -- Doesn't support commands | |
# Commander -- Everything is about procs | |
# Trollop -- Doesn't support commands | |
# GLI -- Relies on calling a proc | |
# Methadone -- See GLI | |
# Main -- Seems to require the use of a #run method or proc | |
# Thor -- Focused on running methods, resulting code very difficult to read | |
# CRI -- Requires #run method or proc (seeing a pattern here...) | |
# Optitron -- Very similar to Thor | |
# Clamp -- Framework, again requires #execute | |
# Escort -- Also a framework, expects a block to execute | |
# Executable -- Works with classes and does reflection | |
# CLI.K -- Everything is a lambda | |
# Slop -- Close, may need some redesign to get it to where I need it | |
# Mixlib-CLI -- It's most of the way there but works via polluting the namespace. | |
# Also, command support is based around the classes configured. | |
# | |
# So yeah the closest I've found is Slop. I'm working on improving it to support all | |
# of the cases I need but I think I've run into some design issues that will require | |
# a rewrite of quite a bit of it. | |
# | |
# Is there anything I've missed? | |
# What is with this fascination with calling procs anyway? | |
# I just want parsing, nothing else! | |
# | |
library = SomeLibrary.new do | |
global_option "--verbose", "-v", "Set logging to verbose" | |
global_option "--log_to FILE", "-L FILE", "Log to a file" | |
command "one" do | |
option "--config FILE", "Specify a config file" | |
end | |
end | |
results = library.parse("--verbose -L logger.log one --config config.yml extra values") | |
# Global options | |
results[:verbose] == true | |
results[:log_to] == "logger.log" | |
# Command options are scoped | |
results[:config] == nil | |
results[:one][:config] == "config.yml" | |
# Anything that isn't directly matched is left alone for further use | |
results.rest == %w(extra values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment