command_kit.rb icon indicating copy to clipboard operation
command_kit.rb copied to clipboard

Add `CommandKit::Commands::DefaultCommand`

Open postmodern opened this issue 4 years ago • 2 comments

Add a module for defining a default command to run instead of help.

postmodern avatar Dec 16 '21 23:12 postmodern

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 avatar May 25 '25 22:05 javierjulio

@javierjulio there are a few edge-cases with default commands you should be aware of before submitting a PR.

  1. 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.
  2. 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`).

postmodern avatar May 26 '25 02:05 postmodern