Dry-cli global options

Is there a way to specify global options?

Use case:

My tool works with pre-defined dir structures. There can be multiple such dirs. I want an option that can specify this directory and want it influence each subcommand.

Think git and gitdir. You can specify gitdir and it will have effect on all subcommands.

I’ve handled this by providing a “base” command class, which all the specific command classes should inherit from:

class BaseCommand < Hanami::CLI::Command
  def self.inherited(klass)
    super

    klass.option :env, aliases: ["-e"], default: nil, desc: "Application environment"
  end
end
1 Like

I assume this will let me specify options once but have them available in all commands. This is useful.

However, this doesn’t look like I’ll be able to specify any arguments before the subcommand. E.g. command --global-arg subcommand --arg.

Options are unsorted array of strings, there is no difference in which order you type them.
Though OptParser will think of string after option as a value for that option.

–global-arg subcommand # this will be translated in option with the key global-arg and value of subcommand