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

How to build sub-command switches #94

Closed
KINGSABRI opened this issue Mar 8, 2018 · 3 comments
Closed

How to build sub-command switches #94

KINGSABRI opened this issue Mar 8, 2018 · 3 comments

Comments

@KINGSABRI
Copy link

Hello there,
I've requirements here that I hope I explain it well.

here is the case I'm trying to do

ruby cooltool.rb -m COMMAND --cmd-sw
# eg
ruby cooltool.rb -m module1 --list user.txt

since the --list switch belongs to module1 not to cooltool

here's my initial dirty implementation

cooltool.rb

require 'trollop'
require 'pp'

module CoolTool
  class OptionParser
    attr_reader :main_opts

    def initialize
      @main_opts = Trollop::options do
        banner "Cool tool."
        banner "Options:"
        opt :parse, "Parse a string", :short => :p, :type => :string
        opt :module, "Use module", :short => :m, :default => ''
      end
    end

    def modules_list
      @modules_list = %w(module1 module2 module3)
    end
  end
end

opts = CoolTool::OptionParser.new
case
  when opts.main_opts[:parse]
    puts "Parsing!!"
  when opts.main_opts[:module]
    mod = opts.main_opts[:module]
    require "./#{mod}.rb"
    pp CoolTool::CoolToolModule.const_get(mod.capitalize).new
  else
    puts opts.main_opts
end

module1.rb

module CoolTool
  module CoolToolModule
    class Module1
      attr_accessor :opts
      def initialize
        @opts = Trollop::Parser.new do
          banner "Module1."
          banner "Options:"
          opt :list, "The user list.", :short => :l, :type => :string
        end
        @opts.parse ARGV
      end

      # make module read the file if -l user.txt
      def run
        if @opts[:list]
          File.readlines(@opts[:list])
        else
          puts 'Some Error'
        end
      end
    end
  end
end

I need each module to have its own commands and/or switches. Those modules called by --module/-m switch. The problem is, when I use --list switch trollop sees it as an argument for cooltool since its cooltool's stdin

 ruby cooltool.rb -m module1 --list user.txt

Error: unknown argument '-l'.
Try --help for help.

Thanks

@Fryguy
Copy link
Member

Fryguy commented Mar 9, 2018

This is already handled using Commands. However, commands don't use a literal switch. You might be able to use a switch in a manner similar to the example, but I'm not sure. If you don't really need the switch, see if you can get it to work like the example.

@Fryguy
Copy link
Member

Fryguy commented Mar 9, 2018

Closing this for now as the feature exists, but you can reopen if you feel strongly about supporting it via a switch as well.

@Fryguy Fryguy closed this as completed Mar 9, 2018
@Fryguy
Copy link
Member

Fryguy commented Mar 9, 2018

You may also be interested in trollop-subcommands as discussed in #88

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