Some junk code I reuse in various ruby projects.
FlyingRobots::Application
does a bunch of application-related boilerplate nonsense, like parse command line arguments, print help messages, etc.FlyingRobots::Args
is a simple command line arguments parserFlyingRobots::Log
is a simple logger
Hello world example:
FlyingRobots::Application.new(ARGV).run { |opts|
puts "hello world"
}
FlyingRobots::Application
works like this:
- You create a configuration hash, within which you define the command line options you wanna parse
- You create a new
FlyingRobots::Application
object and initialize it with ARGV and the config hash - It parses ARGV, handling unrecognized and malformed arguments for you by printing a help message
- Passes the parsed arguments to your block when you call
run
Simple as that!
If you use FlyingRobots::Application
, above, you won't need to directly use Args
, but it's still good to know how to describe flags for Args
to parse. Each flag must be defined in a hash containing the following keys:
:name
[required] The option's name (displayed in the help message):short
[optional] The short form of the flag, such as 'f' for '-f', or 'e' for '-e':long
[optional] The long form of the flag, such as 'file' for '--file', or 'example' for '--example':description
[required] The help text to describe the option:default
[optional] Default value:type
[optional] Specifies the type. SeeFlyingRobots::Types
inarg_types.rb
for more information (boolean by default):multi
[optional] True if multiple values are allowed (false by default):required
[optional] True if this flag must appear in the command line arguments (false by default)
For example:
flag_desc = {
:name => "magic",
:description => "Enables magic.",
:required => true
}
A simple volume-based logger. Pretty prints arrays, hashes, and exceptions. Available volumes:
VOLUME_TRACE
Used to log spammy thingsVOLUME_DEBUG
Used to log debug infoVOLUME_INFO
Used to log informative messagesVOLUME_WARN
Used to log warningsVOLUME_ERROR
Used to log errorsVOLUME_MUTE
Used to suppress all messages
When you create a new Log
, you configure its volume. Messages printed with a volume below the log's configured level won't appear. Errors and warnings will be printed to stderr and everything else goes to stdout.