Module: GLI::App
Overview
A means to define and parse a command line interface that works as Git’s does, in that you specify global options, a command name, command specific options, and then command arguments.
Instance Method Summary collapse
-
#accept(object, &block) ⇒ Object
Configure a type conversion not already provided by the underlying OptionParser.
-
#around(&a_proc) ⇒ Object
This inverts the pre/post concept.
-
#commands_from(path) ⇒ Object
Loads ruby files in the load path that start with
path
, which are presumed to be commands for your executable. -
#config_file(filename) ⇒ Object
Sets that this app uses a config file as well as the name of the config file.
-
#default_command(command) ⇒ Object
Sets a default command to run when none is specified on the command line.
-
#exit_now!(message, exit_code = 1) ⇒ Object
Simpler means of exiting with a custom exit code.
-
#help_now!(message = nil) ⇒ Object
Exit now, showing the user help for the command they executed.
-
#on_error(&a_proc) ⇒ Object
Define a block to run if an error occurs.
-
#post(&a_proc) ⇒ Object
Define a block to run after the command was executed, only if there was not an error.
-
#pre(&a_proc) ⇒ Object
Define a block to run after command line arguments are parsed but before any command is run.
-
#preserve_argv(preserve = true) ⇒ Object
By default, GLI mutates the argument passed to it.
-
#program_desc(description = nil) ⇒ Object
Describe the overall application/programm.
-
#program_long_desc(description = nil) ⇒ Object
Provide a longer description of the program.
-
#program_name(override = nil) ⇒ Object
:nodoc:.
-
#skips_around ⇒ Object
Use this if the following command should not have the around block executed.
-
#skips_post ⇒ Object
Use this if the following command should not have the post block executed.
-
#skips_pre ⇒ Object
Use this if the following command should not have the pre block executed.
-
#sort_help(sort_type) ⇒ Object
Control how commands and options are sorted in help output.
-
#use_openstruct(use_openstruct) ⇒ Object
Call this with
true
will cause theglobal_options
andoptions
passed to your code to be wrapped in Options, which is a subclass ofOpenStruct
that adds[]
and[]=
methods. -
#version(version) ⇒ Object
Indicate the version of your application.
-
#wrap_help_text(wrap_type) ⇒ Object
Set how help text is wrapped.
Methods included from AppSupport
#accepts, #around_blocks, #clear_nexts, #commands, #commands_declaration_order, #config_file_name, #context_description, #copy_options_to_aliased_versions, #error_device=, #flags, #get_default_command, #help_sort_type, #help_text_wrap_type, included, #override_command_defaults, #override_default, #override_defaults_based_on_config, #parse_config, #post_block, #pre_block, #reset, #run, #stderr, #switches, #version_string
Methods included from DSL
#arg_name, #clear_nexts, #command, #default_value, #desc, #flag, #flags_declaration_order, #long_desc, #switch, #switches_declaration_order
Methods included from CopyOptionsToAliases
Instance Method Details
#accept(object, &block) ⇒ Object
Configure a type conversion not already provided by the underlying OptionParser. This works more or less like the OptionParser version.
- object
-
the class (or whatever) that triggers the type conversion
- block
-
the block that will be given the string argument and is expected to return the converted value
Example
accept(Hash) do |value|
result = {}
value.split(/,/) do |pair|
k,v = pair.split(/:/)
result[k] = v
end
result
end
flag :properties, :type => Hash
201 202 203 |
# File 'lib/gli/app.rb', line 201 def accept(object,&block) accepts[object] = block end |
#around(&a_proc) ⇒ Object
This inverts the pre/post concept. This is useful when you have a global shared resource that is governed by a block instead of separate open/close methods. The block you pass here will be given four parameters:
- global options
-
the parsed global options
- command
-
The GLI::Command that the user is going to invoke
- options
-
the command specific options
- args
-
unparsed command-line args
- code
-
a block that you must
call
to execute the command.
#help_now! and #exit_now! work as expected; you can abort the command call by simply not calling it.
You can declare as many #around blocks as you want. They will be called in the order in which they are defined.
Note that if you declare #around blocks, #pre and #post blocks will still work. The #pre is called first, followed by the around, followed by the #post.
Call #skips_around before a command that should not have this hook fired
137 138 139 140 |
# File 'lib/gli/app.rb', line 137 def around(&a_proc) @around_blocks ||= [] @around_blocks << a_proc end |
#commands_from(path) ⇒ Object
Loads ruby files in the load path that start with path
, which are presumed to be commands for your executable. This is useful for decomposing your bin file into different classes, but can also be used as a plugin mechanism, allowing users to provide additional commands for your app at runtime. All that being said, it’s basically a glorified require
.
- path
-
a path from which to load
.rb
files that, presumably, contain commands. If this is an absolute path, any files in that path are loaded. If not, it is interpretted as relative to somewhere in theLOAD_PATH
.
Example:
# loads *.rb from your app's install - great for decomposing your bin file
commands_from "my_app/commands"
# loads *.rb files from the user's home dir - great and an extension/plugin mechanism
commands_from File.join(ENV["HOME"],".my_app","plugins")
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gli/app.rb', line 33 def commands_from(path) if Pathname.new(path).absolute? and File.exists?(path) load_commands(path) else $LOAD_PATH.each do |load_path| commands_path = File.join(load_path,path) load_commands(commands_path) end end end |
#config_file(filename) ⇒ Object
Sets that this app uses a config file as well as the name of the config file.
filename
-
A String representing the path to the file to use for the config file. If it’s an absolute path, this is treated as the path to the file. If it’s not, it’s treated as relative to the user’s home directory as produced by
File.expand_path('~')
.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gli/app.rb', line 92 def config_file(filename) if filename =~ /^\// @config_file = filename else @config_file = File.join(File.(ENV['HOME']),filename) end commands[:initconfig] = InitConfig.new(@config_file,commands,flags,switches) @commands_declaration_order << commands[:initconfig] @config_file end |
#default_command(command) ⇒ Object
Sets a default command to run when none is specified on the command line. Note that if you use this, you won’t be able to pass arguments, flags, or switches to the command when run in default mode. All flags and switches are treated as global, and any argument will be interpretted as the command name and likely fail.
command
-
Command as a Symbol to run as default
259 260 261 |
# File 'lib/gli/app.rb', line 259 def default_command(command) @default_command = command.to_sym end |
#exit_now!(message, exit_code = 1) ⇒ Object
Simpler means of exiting with a custom exit code. This will raise a CustomExit with the given message and exit code, which will ultimatley cause your application to exit with the given exit_code as its exit status Use #help_now! if you want to show the help in addition to the error message
- message
-
message to show the user
- exit_code
-
exit code to exit as, defaults to 1
212 213 214 |
# File 'lib/gli/app.rb', line 212 def exit_now!(,exit_code=1) raise CustomExit.new(,exit_code) end |
#help_now!(message = nil) ⇒ Object
Exit now, showing the user help for the command they executed. Use #exit_now! to just show the error message
- message
-
message to indicate how the user has messed up the CLI invocation or nil to just simply show help
219 220 221 222 223 224 225 |
# File 'lib/gli/app.rb', line 219 def help_now!(=nil) exception = OptionParser::ParseError.new() class << exception def exit_code; 64; end end raise exception end |
#on_error(&a_proc) ⇒ Object
Define a block to run if an error occurs. The block will receive any Exception that was caught. It should evaluate to false to avoid the built-in error handling (which basically just prints out a message). GLI uses a variety of exceptions that you can use to find out what errors might’ve occurred during command-line parsing:
-
GLI::CustomExit
-
GLI::UnknownCommandArgument
-
GLI::UnknownGlobalArgument
-
GLI::UnknownCommand
-
GLI::BadCommandLine
152 153 154 |
# File 'lib/gli/app.rb', line 152 def on_error(&a_proc) @error_block = a_proc end |
#post(&a_proc) ⇒ Object
Define a block to run after the command was executed, only if there was not an error. The block will receive the global-options,command,options, and arguments
116 117 118 |
# File 'lib/gli/app.rb', line 116 def post(&a_proc) @post_block = a_proc end |
#pre(&a_proc) ⇒ Object
Define a block to run after command line arguments are parsed but before any command is run. If this block raises an exception the command specified will not be executed. The block will receive the global-options,command,options, and arguments If this block evaluates to true, the program will proceed; otherwise the program will end immediately
109 110 111 |
# File 'lib/gli/app.rb', line 109 def pre(&a_proc) @pre_block = a_proc end |
#preserve_argv(preserve = true) ⇒ Object
By default, GLI mutates the argument passed to it. This is consistent with OptionParser
, but be less than ideal. Since that value, for scaffolded apps, is ARGV
, you might want to refer to the entire command-line via ARGV
and thus not want it mutated.
168 169 170 |
# File 'lib/gli/app.rb', line 168 def preserve_argv(preserve=true) @preserve_argv = preserve end |
#program_desc(description = nil) ⇒ Object
Describe the overall application/programm. This should be a one-sentence summary of what your program does that will appear in the help output.
description
-
A String of the short description of your program’s purpose
48 49 50 51 52 53 |
# File 'lib/gli/app.rb', line 48 def program_desc(description=nil) if description @program_desc = description end @program_desc end |
#program_long_desc(description = nil) ⇒ Object
Provide a longer description of the program. This can be as long as needed, and use double-newlines for paragraphs. This will show up in the help output.
- description
-
A String for the description
59 60 61 62 63 64 |
# File 'lib/gli/app.rb', line 59 def program_long_desc(description=nil) if description @program_long_desc = description end @program_long_desc end |
#program_name(override = nil) ⇒ Object
:nodoc:
248 249 250 |
# File 'lib/gli/app.rb', line 248 def program_name(override=nil) #:nodoc: warn "#program_name has been deprecated" end |
#skips_around ⇒ Object
Use this if the following command should not have the around block executed. By default, the around block is executed, but for commands that might not want the setup to happen, this can be handy
83 84 85 |
# File 'lib/gli/app.rb', line 83 def skips_around @skips_around = true end |
#skips_post ⇒ Object
Use this if the following command should not have the post block executed. By default, the post block is executed after each command. Using this will avoid that behavior for the following command
76 77 78 |
# File 'lib/gli/app.rb', line 76 def skips_post @skips_post = true end |
#skips_pre ⇒ Object
Use this if the following command should not have the pre block executed. By default, the pre block is executed before each command and can result in aborting the call. Using this will avoid that behavior for the following command
69 70 71 |
# File 'lib/gli/app.rb', line 69 def skips_pre @skips_pre = true end |
#sort_help(sort_type) ⇒ Object
Control how commands and options are sorted in help output. By default, they are sorted alphabetically.
- sort_type
-
How you want help commands/options sorted:
:manually
-
help commands/options are ordered in the order declared.
:alpha
-
sort alphabetically (default)
232 233 234 |
# File 'lib/gli/app.rb', line 232 def sort_help(sort_type) @help_sort_type = sort_type end |
#use_openstruct(use_openstruct) ⇒ Object
Call this with true
will cause the global_options
and options
passed to your code to be wrapped in Options, which is a subclass of OpenStruct
that adds []
and []=
methods.
use_openstruct
-
a Boolean indicating if we should use OpenStruct instead of Hashes
178 179 180 |
# File 'lib/gli/app.rb', line 178 def use_openstruct(use_openstruct) @use_openstruct = use_openstruct end |
#version(version) ⇒ Object
Indicate the version of your application
version
-
String containing the version of your application.
159 160 161 162 |
# File 'lib/gli/app.rb', line 159 def version(version) @version = version switch :version, :negatable => false end |
#wrap_help_text(wrap_type) ⇒ Object
Set how help text is wrapped.
- wrap_type
-
Symbol indicating how you’d like text wrapped:
:to_terminal
-
Wrap text based on the width of the terminal (default)
:verbatim
-
Format text exactly as it was given to the various methods. This is useful if your output has formatted output, e.g. ascii tables and you don’t want it messed with.
:one_line
-
Do not wrap text at all. This will bring all help content onto one line, removing any newlines
:tty_only
-
Wrap like
:to_terminal
if this output is going to a TTY, otherwise don’t wrap (like:one_line
)
244 245 246 |
# File 'lib/gli/app.rb', line 244 def wrap_help_text(wrap_type) @help_text_wrap_type = wrap_type end |