Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a different name for calling a ruby gem from the command line

Is there any way to change the name that the user has to use when calling from the command line? For example, I have a Thor command line app called super_awesome_gem. I want my gem to be called super_awesome_gem, but when the user calls it from the command line I just want them to be able to call sup_awe or something.

I've tried editing the gemspec file and the file and folder names, but I can't figure out what the proper way to do this would be, or even if there is one.

Is there a way to name a gem one way and have the command line call be a different name?

like image 541
snowe Avatar asked Dec 03 '25 10:12

snowe


2 Answers

Your gem name and the executables it bundles don't have to be the same at all. In your gemspec, you can define a list of executables via executables:

Gem::Specification.new do |s|
  s.name = "super_awesome_gem"
  # other gemspec stuff
  s.executables = ["sup_awe"]
end

As long as sup_awe is listed in the gemspec's files list, and is executable, that will be in the user's path after they install your gem. Bundler, when bootstrapping your gemspec, makes this even simpler

s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

Anything in bin/ will be treated as an executable.

That is the long way of saying that your exectuable/bin file can be named whatever you want, and doesn't have to be named for your gem.

like image 109
davetron5000 Avatar answered Dec 04 '25 23:12

davetron5000


Another way to achieve this is an alias:

alias my_command=original_command

Just place it where it fits you best.

A third way is to use a wrapper_script, which is a script with the desired name which then calls the original command and passes it all arguments it got:

#!/bin/sh
original_command $@

or in cmd.exe on windows:

@echo off
original_command %*
like image 24
tvw Avatar answered Dec 04 '25 22:12

tvw