Adding a --version option to a Ruby Thor CLI
Asked Answered
A

3

16

How do I add a --version option to my Ruby Thor command line interface application.

For example I want to be able to run

$ thor_app --version
> thor_app version 1.0.0

This question is related to Run a CLI Thor app without arguments or task name, but specifically to add a --version option which does not require a task.

Note
This was written following the self-answer format. Addition answers and updates are encouraged

Alius answered 2/4, 2014 at 11:28 Comment(0)
P
35

I had some luck with this approach:

class CLI < Thor
  map %w[--version -v] => :__print_version

  desc "--version, -v", "print the version"
  def __print_version
    puts FooBar::VERSION
  end
end

The leading underscores ensures there isn't a command like yourapp version, and forces yourapp --version or yourapp -v. The desc content will allow it to show up as -v, --version without exposing __print_version.

Philbert answered 6/1, 2015 at 18:37 Comment(3)
This is kind of what bundler does. only they just call the task version and let people call it with bundler version too (because why not?) github.com/bundler/bundler/blob/…Factorize
This should be submitted back to the Thor devs for a documentation fix.Trixie
Just a note, you'll have to reinstall the gem to get the new action(s). If you want to test it dynamically, you can run: bundle exec bin/gem_nameMarje
C
3

I didn't love the accepted solution; it ends up listing version as a command, listing --version and --no-version as global options, and if the script is run with no options it's silent rather than giving help.

The best I've been able to come up with is to do it outside Thor:

class CLI < Thor
   .
   .
   .
end

if ARGV[0] == "--version"
    puts "MyApp #{MyApp::VERSION}"
    exit
end

CLI.start

This has the minor drawback that --version isn't documented anywhere.

Caritacaritas answered 30/12, 2014 at 11:7 Comment(1)
"I didn't love the accepted solution" — just for posterity, this originally referred to Morgan's answer (https://mcmap.net/q/717704/-adding-a-version-option-to-a-ruby-thor-cli).Philbert
A
1

So far the best option I have come up with is to create a boolean class option, which does not belong to a task, which can be referenced by other tasks. The often used example for a class option is -v verbose, as all tasks can use this to determine how noisy they should be.

Then create a 'version' task and make it the default task, so when no task is defined the version task is ran and can react to the --version flag (class option).

class CLI < Thor
  #include Thor::Actions
  class_option :version, :type => :boolean

  desc "version", "Show thor_app version"
  def version
    if options[:version]
      puts "thor_app version #{find_version}"
    end
  end
  default_task :version

  no_tasks do
    def find_version
      ## Method can be replaced to look up VERSION
      '1.0.0'
    end
  end
end
Alius answered 2/4, 2014 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.