Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read options.default from anothe class? #60

Open
xbelanch opened this issue Dec 26, 2017 · 4 comments
Open

Read options.default from anothe class? #60

xbelanch opened this issue Dec 26, 2017 · 4 comments

Comments

@xbelanch
Copy link

xbelanch commented Dec 26, 2017

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
Copy link
Author

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 xbelanch reopened this Dec 26, 2017
@xbelanch
Copy link
Author

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
Copy link
Author

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
Copy link
Member

ggilder commented Jan 8, 2018

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants