Skip to content

Commit

Permalink
Namespace GLI within a class, avoid global scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 18, 2018
1 parent d7252f9 commit a270c1e
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 241 deletions.
79 changes: 41 additions & 38 deletions bin/gli
Expand Up @@ -3,63 +3,66 @@
require 'gli'
require 'gli/commands/scaffold'

include GLI::App
class App
extend GLI::App

program_desc 'create scaffolding for a GLI-powered application'
program_desc 'create scaffolding for a GLI-powered application'

version GLI::VERSION
version GLI::VERSION

# Can't use these without changing the current behavior of gli
# arguments :strict
# subcommand_option_handling :normal
# Can't use these without changing the current behavior of gli
# arguments :strict
# subcommand_option_handling :normal

switch :v, :desc => 'Be verbose'
switch :v, :desc => 'Be verbose'

switch :n, :desc => 'Dry run; don''t change the disk'
switch :n, :desc => 'Dry run; don''t change the disk'

desc 'Root dir of project'
desc 'Root dir of project'
long_desc <<EOS
This is the directory where the project''s directory will be made, so if you
specify a project name ''foo'' and the root dir of ''.'', the directory
''./foo'' will be created'
This is the directory where the project''s directory will be made, so if you
specify a project name ''foo'' and the root dir of ''.'', the directory
''./foo'' will be created'
EOS

flag :r,:root, :default_value => '.'
flag :r,:root, :default_value => '.'

desc 'Create a new GLI-based project'
desc 'Create a new GLI-based project'
long_desc <<EOS
This will create a scaffold command line project that uses GLI
for command line processing. Specifically, this will create
an executable ready to go, as well as a lib and test directory, all
inside the directory named for your project
This will create a scaffold command line project that uses GLI
for command line processing. Specifically, this will create
an executable ready to go, as well as a lib and test directory, all
inside the directory named for your project
EOS
arg :project_name
arg :command_name, [:optional, :multiple]
arg_name "project_name [command_name][, [command_name]]*"
command [:init,:scaffold] do |c|
arg :project_name
arg :command_name, [:optional, :multiple]
arg_name "project_name [command_name][, [command_name]]*"
command [:init,:scaffold] do |c|

c.switch :e,:ext, :desc => 'Create an ext dir'
c.switch :e,:ext, :desc => 'Create an ext dir'

c.switch :notest, :desc => 'Do not create a test or features dir', :negatable => false
c.switch :notest, :desc => 'Do not create a test or features dir', :negatable => false

c.switch :force, :desc => 'Overwrite/ignore existing files and directories'
c.switch :force, :desc => 'Overwrite/ignore existing files and directories'

c.switch :rvmrc, :desc => 'Create an .rvmrc based on your current RVM setup'
c.switch :rvmrc, :desc => 'Create an .rvmrc based on your current RVM setup'

c.action do |g,o,args|
if args.length < 1
raise 'You must specify the name of your project'
c.action do |g,o,args|
if args.length < 1
raise 'You must specify the name of your project'
end
GLI::Commands::Scaffold.create_scaffold(g[:r],!o[:notest],o[:e],args[0],args[1..-1],o[:force],g[:n],o[:rvmrc])
end
GLI::Commands::Scaffold.create_scaffold(g[:r],!o[:notest],o[:e],args[0],args[1..-1],o[:force],g[:n],o[:rvmrc])
end
end

pre do |global,command,options,args|
puts "Executing #{command.name}" if global[:v]
true
end
pre do |global,command,options,args|
puts "Executing #{command.name}" if global[:v]
true
end

post do |global,command,options,args|
puts "Executed #{command.name}" if global[:v]
post do |global,command,options,args|
puts "Executed #{command.name}" if global[:v]
end
end
exit run(ARGV)

exit App.run(ARGV)
46 changes: 42 additions & 4 deletions gli.rdoc
@@ -1,16 +1,16 @@
== gli - create scaffolding for a GLI-powered application

v2.12.1
v2.17.1

=== Global Options
=== -r|--root arg

Root dir of project

[Default Value] .
This is the directory where the project''s directory will be made, so if you
specify a project name ''foo'' and the root dir of ''.'', the directory
''./foo'' will be created'
This is the directory where the project''s directory will be made, so if you
specify a project name ''foo'' and the root dir of ''.'', the directory
''./foo'' will be created'

=== --help
Show this message
Expand All @@ -33,3 +33,41 @@ Display the program version


=== Commands
==== Command: <tt>help command</tt>
Shows a list of commands or help for one command

Gets help for the application or its commands. Can also list the commands in a way helpful to creating a bash-style completion function
===== Options
===== -c
List commands one per line, to assist with shell completion



==== Command: <tt>init|scaffold project_name [command_name][, [command_name]]*</tt>
Create a new GLI-based project

This will create a scaffold command line project that uses GLI
for command line processing. Specifically, this will create
an executable ready to go, as well as a lib and test directory, all
inside the directory named for your project
===== Options
===== -e|--[no-]ext
Create an ext dir



===== --[no-]force
Overwrite/ignore existing files and directories



===== --notest
Do not create a test or features dir



===== --[no-]rvmrc
Create an .rvmrc based on your current RVM setup



1 change: 1 addition & 0 deletions lib/gli.rb
Expand Up @@ -24,6 +24,7 @@

module GLI
include GLI::App

def self.included(klass)
warn "You should include GLI::App instead"
end
Expand Down
96 changes: 49 additions & 47 deletions lib/gli/commands/scaffold.rb
Expand Up @@ -277,85 +277,87 @@ def self.mk_binfile(root_dir,create_ext_dir,force,dry_run,project_name,commands)
exit 64
end
include GLI::App
class App
extend GLI::App
program_desc 'Describe your application here'
program_desc 'Describe your application here'
version #{project_name_as_module_name(project_name)}::VERSION
version #{project_name_as_module_name(project_name)}::VERSION
subcommand_option_handling :normal
arguments :strict
subcommand_option_handling :normal
arguments :strict
desc 'Describe some switch here'
switch [:s,:switch]
desc 'Describe some switch here'
switch [:s,:switch]
desc 'Describe some flag here'
default_value 'the default'
arg_name 'The name of the argument'
flag [:f,:flagname]
desc 'Describe some flag here'
default_value 'the default'
arg_name 'The name of the argument'
flag [:f,:flagname]
EOS
first = true
commands.each do |command|
file.puts <<EOS
desc 'Describe #{command} here'
arg_name 'Describe arguments to #{command} here'
desc 'Describe #{command} here'
arg_name 'Describe arguments to #{command} here'
EOS
if first
file.puts <<EOS
command :#{command} do |c|
c.desc 'Describe a switch to #{command}'
c.switch :s
command :#{command} do |c|
c.desc 'Describe a switch to #{command}'
c.switch :s
c.desc 'Describe a flag to #{command}'
c.default_value 'default'
c.flag :f
c.action do |global_options,options,args|
c.desc 'Describe a flag to #{command}'
c.default_value 'default'
c.flag :f
c.action do |global_options,options,args|
# Your command logic here
# Your command logic here
# If you have any errors, just raise them
# raise "that command made no sense"
# If you have any errors, just raise them
# raise "that command made no sense"
puts "#{command} command ran"
puts "#{command} command ran"
end
end
end
EOS
else
file.puts <<EOS
command :#{command} do |c|
c.action do |global_options,options,args|
puts "#{command} command ran"
command :#{command} do |c|
c.action do |global_options,options,args|
puts "#{command} command ran"
end
end
end
EOS
end
first = false
end
file.puts <<EOS
pre do |global,command,options,args|
# Pre logic here
# Return true to proceed; false to abort and not call the
# chosen command
# Use skips_pre before a command to skip this block
# on that command only
true
end
pre do |global,command,options,args|
# Pre logic here
# Return true to proceed; false to abort and not call the
# chosen command
# Use skips_pre before a command to skip this block
# on that command only
true
end
post do |global,command,options,args|
# Post logic here
# Use skips_post before a command to skip this
# block on that command only
end
post do |global,command,options,args|
# Post logic here
# Use skips_post before a command to skip this
# block on that command only
end
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
end
exit run(ARGV)
exit App.run(ARGV)
EOS
puts "Created #{bin_file}"
end
Expand Down
10 changes: 5 additions & 5 deletions test/apps/todo/bin/todo
Expand Up @@ -7,11 +7,11 @@ $: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
require 'gli'
require 'todo/version'


class App
if ENV['GLI1_COMPATIBILITY']
include GLI
extend GLI
else
include GLI::App
extend GLI::App
end

sort_help (ENV['TODO_SORT_HELP'] || 'alpha').to_sym
Expand Down Expand Up @@ -70,5 +70,5 @@ on_error do |exception|
# return false to skip default error handling
true
end

exit run(ARGV)
end
exit App.run(ARGV)

0 comments on commit a270c1e

Please sign in to comment.