Add `CommandKit::Commands::DefaultCommand`
Add a module for defining a default command to run instead of help.
I've been reading the codebase and I've figured out that the help default is called in the Commands module. I'd like to contribute this to help out.
This could be implemented as a method, e.g. default_command "foo" in that module to support it. Or if a new module is preferred, I guess it would just follow same format as CommandName so I can do that.
This is what I was thinking roughly. Would this be acceptable? If so, I will submit a PR.
module ClassMethods
def default_command(new_default_command=nil)
if new_default_command
@default_command = new_default_command
else
@default_command
end
end
end
def run(command=nil,*argv)
if command
exit invoke(command,*argv)
elsif default_command
exit invoke(default_command,*argv)
else
help
exit(1)
end
end
@javierjulio there are a few edge-cases with default commands you should be aware of before submitting a PR.
- What happens when no subcommand is given, but options are given (ex:
mycli --foo)? A. The option is a global option defined in the main CLI class. B. The option is an option defined in the default sub-command. - What happens when the first argument is not a sub-command name but some other argument value, such as a file path?
For edge-case 1, we'd need a way to terminate the option parsing on the first unknown option flag and treat everything at that point and beyond as options/arguments for the default command.
For edge-case 2, we'd need to check if the first argument is a sub-command name (ex: mycli subcommand ...) vs. some other value (ex: mycli file.txt`).