commander-rb / commander

The complete solution for Ruby command-line executables
MIT License
822 stars 74 forks source link

Read options.default from anothe class? #60

Open xbelanch opened 6 years ago

xbelanch commented 6 years ago

Maybe that's a dumb question, but firs of all take a look at this minimal working example:

#!/usr/bin/env ruby

module Minimal
    require 'rubygems'
    require 'commander/import'
    class CLI
        include Commander::Methods
        def self.parse(args)
            program :name, 'MWE'
            program :version, '0.0.1'
            program :description, 'Minimal Working Example'

            command :hello do |c|
                c.syntax = 'mwe hello'
                c.description =  'Say hello to everyone'
                c.option '--spanish', 'Say Hello in Spanish'
                c.option '--french', 'Say Hello in French'
                c.action do |args, options|
                    say options.default
                    options.default
                end
            end
        end
    end
end

module Minimal
    class Working
        def self.example(args)
            option = CLI.parse args
            if option.name = 'hello'
                #=> How can I check if spanish of french option is true?
            end
        end
    end
end

Minimal::Working.example ARGV

Well actually I'm trying to acces to options.default value outside the Commander space object, but I failed. What am I missing?

Thanks in advance

xbelanch commented 6 years ago

That was easy:

module Minimal

    class Working
        def self.example(args)
            option = CLI.parse args
            if option.name = 'hello'
                if option.spanish 
                    warn "Hola"
                end
                if option.french
                    warn "Bonjour"
                end
            end
            #puts "from CLI: #{options.name}" 
        end
    end
end
xbelanch commented 6 years ago

Sorry, there was a mistake in the last post:

if option.name = 'hello' must be if option.name == 'hello'

I played a little bit with the code to find a workaround and I found this that works I expected (but sure there must be another fine way to do it!)

#!/usr/bin/env ruby
# https://github.com/commander-rb/commander/issues/60
module Minimal
    require 'rubygems'
    require 'commander/import'
    class CLI
        include Commander::Methods
        def self.parse(args)
            program :name, 'MWE'
            program :version, '0.0.1'
            program :description, 'Minimal Working Example'

            command :hello do |c|
                c.syntax = 'mwe hello'
                c.description =  'Say hello to everyone'
                c.option '--spanish', 'Say Hello in Spanish'
                c.option '--french', 'Say Hello in French'
                c.action do |args, options|
                    #say options.default
                    return c.name, options
                end
            end
            run!
        end
    end
end

module Minimal
    class Working
        def self.example(args)
            command, option = CLI.parse args
            if command == 'hello'
                if option.spanish 
                    warn "Hola"
                end
                if option.french
                    warn "Bonjour"
                end
            end
        end
    end
end

Minimal::Working.example ARGV
xbelanch commented 6 years ago

Another question: If you comment the "run!" method the options are not processed (and they are not passed on the return). Uncommented it you get twice output if you asking for --version option

> ruby mwe.rb --version
MWE 0.0.1
MWE 0.0.1
ggilder commented 6 years ago

Hi, I'm not sure exactly what the issue is here, or what you're trying to do that's not working... can you provide a more concise example or explain what is not working? Thanks!