Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
igormoochnick opened this issue Dec 24, 2014 · 5 comments
Open

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

igormoochnick opened this issue Dec 24, 2014 · 5 comments

Comments

@igormoochnick
Copy link

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
Copy link
Owner

davetron5000 commented Dec 24, 2014

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
Copy link

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
Copy link
Owner

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
Copy link

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

@davetron5000
Copy link
Owner

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:

def find_command(name)
name = String(name || options[:default_command]).strip
raise UnknownCommand.new("No command name given nor default available") if name == ''
command_found = commands_with_aliases.fetch(name) do |command_to_match|
if options[:autocomplete]
found_match = find_command_by_partial_name(commands_with_aliases, command_to_match)
if found_match.kind_of? GLI::Command
if ENV["GLI_DEBUG"] == 'true'
$stderr.puts "Using '#{name}' as it's is short for #{found_match.name}."
$stderr.puts "Set autocomplete false for any command you don't want matched like this"
end
elsif found_match.kind_of?(Array) && !found_match.empty?
raise AmbiguousCommand.new("Ambiguous command '#{name}'. It matches #{found_match.sort.join(',')}")
end
found_match
end
end
raise UnknownCommand.new("Unknown command '#{name}'") if Array(command_found).empty?
command_found
end

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants