small medium large xlarge

Lef_linkedin_pragsmall
01 Nov 2014, 18:34
Leif Eric Fredheim (3 posts)

On page 26, following the installation of GLI, we start building an app from scaffolding, and then we start modifying the scaffolding code on page 28.

It seems like the scaffolding code has changed a lot in recent versions of GLI, as compared to the scaffolding code presented in the book for modification.

Since the install command earlier in the chapter (when we install the GLI gem, version 2.8.0) does not include a specific version of GLI. This may be confusing for some readers, as the scaffolding code that one needs to modify looks very different from what you’re seeing in the book.

This is the scaffolded code, as presented in the book (direct link):

#!/usr/bin/env ruby

$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
#!/usr/bin/env ruby

require 'gli'
include GLI::App

switch  :s
flag   [:f,:filename]

command :new do |c|
  c.switch :s
  c.flag   :f
  c.action do |global_options,options,args|
    # Your command logic here
     
    # If you have any errors, just raise them
    # raise "that command made no sense"
  end
end

command :list do |c|
  c.action do |global_options,options,args|
  end
end
command :done do |c|
  c.action do |global_options,options,args|
  end
end

exit run(ARGV)

This is the actual code I got from following the instructions, scaffolding with GLI (version 2.12.2):

#!/usr/bin/env ruby
require 'gli'
begin # XXX: Remove this begin/rescue before distributing your app
require 'todo'
rescue LoadError
  STDERR.puts "In development, you need to use `bundle exec bin/todo` to run your app"
  STDERR.puts "At install-time, RubyGems will make sure lib, etc. are in the load path"
  STDERR.puts "Feel free to remove this message from bin/todo now"
  exit 64
end

include GLI::App

program_desc 'Describe your application here'

version Todo::VERSION

subcommand_option_handling :normal
arguments :strict

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 new here'
arg_name 'Describe arguments to new here'
command :new do |c|
  c.desc 'Describe a switch to new'
  c.switch :s

  c.desc 'Describe a flag to new'
  c.default_value 'default'
  c.flag :f
  c.action do |global_options,options,args|

    # Your command logic here
     
    # If you have any errors, just raise them
    # raise "that command made no sense"

    puts "new command ran"
  end
end

desc 'Describe list here'
arg_name 'Describe arguments to list here'
command :list do |c|
  c.action do |global_options,options,args|
    puts "list command ran"
  end
end

desc 'Describe done here'
arg_name 'Describe arguments to done here'
command :done do |c|
  c.action do |global_options,options,args|
    puts "done command ran"
  end
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

on_error do |exception|
  # Error logic here
  # return false to skip default error handling
  true
end

exit run(ARGV)

The easiest way to fix this in the book might be to change the gem installation command on the beginning of page 26 to include the specific version used in the book, rather than installing the newest version. In other words, change the gem installation command to:

gem install gli -v 2.8.0
Lef_linkedin_pragsmall
01 Nov 2014, 18:43
Leif Eric Fredheim (3 posts)

Interesting, after uninstalling GLI version 2.12.2 and installing only version 2.8.0, the scaffolding code still seems to be very different from what is shown in the book, so maybe it’s just me doing something wrong.

Lef_linkedin_pragsmall
01 Nov 2014, 21:21
Leif Eric Fredheim (3 posts)

Well, now I feel silly. After reading the next couple of chapters, I can see that the code is slowly evolving into something that resembles the code generated by the scaffolding, realising that the the code shown earlier was likely just an excerpt of that code. My apologies for the confusion.

Generic-user-small
16 Dec 2014, 12:48
Mariah Albert (1 post)

Yeah Leif, When you move on to next chapters, you will easily understand the code. I also felt the same in the beginning.

Mariah Albert developer at Appliconic.com

You must be logged in to comment