gli icon indicating copy to clipboard operation
gli copied to clipboard

Define "default" (and only) command to execute

Open igormoochnick opened this issue 10 years ago • 5 comments

It'll be nice to have a "default" global command that will be execute, if there are no other commands defined in the interface.

We have ton of utilities that are a "single purpose" ones. They receive a bunch of flags but have only one command to execute

igormoochnick avatar Dec 24 '14 03:12 igormoochnick

GLI isn't for that sort of app. GLI is for an app that has a series of subcommands. If you just want a simple command line app without subcommands, OptionParser is pretty good, though my other lib davetron5000/optparse-plus makes it really easy to make an app like that.

davetron5000 avatar Dec 24 '14 20:12 davetron5000

I personally want a mix of the two. Ideally I want to be able to take in a wildcard argument without passing anything else in. Like I'm defining shortcuts that can be used later as a wildcard.

gli WILDCARD
gli standard-cmd

Is something like that possible in GLI?

bj-mcduck avatar Nov 23 '20 06:11 bj-mcduck

What does WILDCARD mean in this context? Can you give a more specific example?

All that said, GLI really is designed to always be given at least one subcommand.

davetron5000 avatar Nov 23 '20 14:11 davetron5000

WILDCARD would be an argument, any text that is passed in that doesn't match a predefined command

bj-mcduck avatar Feb 09 '21 22:02 bj-mcduck

Ah, ok. It's not really designed for that but you might be able to implement it yourself. For reference, here is how GLI locates a command based on the command line:

https://github.com/davetron5000/gli/blob/dd79e12a041aed8791a4ba9c8ec9a875075914d1/lib/gli/command_finder.rb#L15-L36

You could rescue UnknownCommand and do something else. If you implement the on_error handler, you can check to see what exception you got. If you can handle whatever the value is, kick off your code and return false.

The one thing you couldn't do easily is detect the name of the unknown command without parsing the message of that exception. I suppose UnknownException could be modified to include an attribute that held the command name it could not find.

davetron5000 avatar Feb 09 '21 23:02 davetron5000