davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

Define "default" (and only) command to execute #209

Closed igormoochnick closed 3 months ago

igormoochnick commented 9 years ago

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

davetron5000 commented 9 years ago

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.

bj-mcduck commented 3 years ago

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?

davetron5000 commented 3 years ago

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.

bj-mcduck commented 3 years ago

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

davetron5000 commented 3 years ago

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 commented 3 months ago

closing as not a bug in GLI (though I totally understand the identified need - GLI just isn't the right lib for this)