3

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?

2
  • Gem name is not a command. You cannot call it from the command line. Do you mean calling some gem commands with different gem names as arguments? Or, are mentioning the gems that have binaries with the same name as the gem? Commented Apr 26, 2013 at 7:23
  • sorry yes, I am meaning the gems that have binaries with the same name as the gem. So is that all I need to change then? Then bin\super_awesome_gem binary name? Commented Apr 26, 2013 at 7:40

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually used bundler to create the gemspec, but for some reason it wasn't working at all to pick up any executables. As you might recall, I have windows and for some reason a ton of things just don't work the same way as Mac and Linux. ;)
2

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 %*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.