Search code examples
thor

Mutually exclusive options with Thor


Is there any way to have two mutually exclusive options with Thor? For example, I need to provide an option of a list of something. I might have an option -l ent1 ent2 ent3 ent67, and I might have an option -f which I pass a file with contents ent1 ent2 ent3 ent67. Can these two options be made mutually exclusive with Thor without writing extra processing code in the method?


Solution

  • I haven't found a built-in way to do this, but you could accomplish it with your own simple check. Here's an example Thor command that does what you want.

    desc "command", "A command that does something."
    option :list, aliases: 'l', type: :array
    option :file, aliases: 'f'
    def list
      if options[:list] && options[:file]
        puts "Use only one, --list(-l) or --file(-f)"
        exit(0)
      end
      # Place the functions of the command here
    end
    

    Hope this helps!