1

I'd like to define a command line app "cla" with commands "foo", "bar", and "find" such that:

./cla foo and ./cla fo both invoke the method defined for foo

./cla find and ./cla fi both invoke find

./cla bar and ./cla b both invoke the method defined for bar

./cla f throws an error

Or something reasonably similar. Hopefully this is clear.

It's not obvious that this can be done in Thor or Commander. I haven't looked at Slop. Do any of the ARGV processor libraries have this ability? Can somebody tell me which libraries, and point me to documentation of the feature?

EDIT: To be clear, I'm not asking for help solving the specific problem above. I'm asking whether it is possible (and not too difficult) to do it with one of the standard command line app builder libraries, which provide many useful features I want besides the ability to invoke a command by a prefix of its name.

2 Answers 2

2

Actually, it seems that thor automatically does what you want out of the box. Specifically, it executes the appropriate command if the shortened form unambiguously matches.

Given this trivial example:

#!/usr/bin/env ruby

require "thor"

class CLA < Thor
  desc "foo", 'Says "foo"'
  def foo
    puts "foo"
  end

  desc "find", 'Says "find"'
  def find
    puts "find"
  end

  desc "bar", 'Says "bar"'
  def bar
    puts "bar"
  end
end

CLA.start

Then:

$ ./cla fo
foo
$ ./cla f
Ambiguous command f matches [find, foo]
$ ./cla fi
find
$ ./cla b
bar

If you really want "f" to always map to "foo" and not give the ambiguous command error, use map like this:

desc "foo", 'Says "foo"'
def foo
  puts "foo"
end
map "f" => :foo

And then this now works:

$ ./cla f
foo
3
  • Thanks. Maybe it's just my reading comprehension, but this seems like a bug in the feature documentation of Thor. I'll raise it with them. Commented Jun 28, 2015 at 0:33
  • How did you know this by the way? Did you just try it? Did you read it somewhere? If the latter, can you recall where? I didn't want to edit the Thor wiki entry for fear of getting the specifics wrong, but if there are existing docs, they should be linked from there. Commented Jun 28, 2015 at 0:59
  • I searched through GitHub and found this commit that introduced the feature: github.com/erikhuda/thor/commit/… . It was definitely not obvious from the documentation. Commented Jun 28, 2015 at 4:12
0

I don’t see any reason to look for a library when everything requires a three lines of code:

meths = %w(foo find bar) # or take from self.methods(false)

variants = meths.select { |m| m =~ /\A#{ARGV[1]}/ }
case variants.size
when 0 then raise 'Unknown method.'
when 1 then public_send variants.first # succeeded, call it
else raise "Umbiguity. Variants: #{variants}."
end
4
  • I'm not looking for a library to do this. I can write code to check if a string matches exactly one of a set of regexen too. I'm looking for a library which does the many things done by Thor, Commander, etc, and which also allows you do to the thing described in the OP. Commented Jun 26, 2015 at 9:39
  • What’s the problem with monkeypatching Thor/Commander with the code above? Commented Jun 26, 2015 at 9:44
  • Have to agree with mudasobwa. While I understand the convenience of libraries, it's just 7 lines of code, even less if compressed appropriately, and easily wrapped in a method parse_command or something similar.
    – EdvardM
    Commented Jun 26, 2015 at 12:01
  • 2
    I haven't looked carefully, but it looked like I might have to define a command keyword for each prefix of a command, which would mean the autogenerated usage output would be full of garbage. IIRC, Thor appeared to support hiding options to keywords from usage output, but not keywords themselves. If it is possible to avoid this, it's likely to involve familiarizing myself with the design of Thor more than makes sense. Seriously, guys, I know how to write code. That's not the problem here. Commented Jun 26, 2015 at 22:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.