davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

How to create subcommand for a flag #271

Closed KINGSABRI closed 3 years ago

KINGSABRI commented 6 years ago

Hello guys How to create a sub-command for a flag, the subcommand might have command, flags and switches as well

# example 1
rubyfu --do-this1 --module MODULENAME --mod-lag --mod-switch 

# example 2
rubyfu --do-this1 --module COMMAND
davetron5000 commented 6 years ago

Each subcommand can have flags—everything nests. See the wiki article and let me know if that helps.

KINGSABRI commented 6 years ago

@davetron5000 thanks for the reply I've checked the wiki. The wiki explains subcommand and sub-subcommand and subcommand's switches and flag.

However, what I'm looking for is having a flag/switch has commands, these commands have flags/switches

Can a flag take a block to contain commands as command takes blocks?

This is a known example for a command has flags and switches

desc 'Describe list here'
arg_name 'Describe arguments to list here'
command :list do |c|
  c.desc 'Describe a switch to list'
  c.switch :s

  c.desc 'Describe a flag to list'
  c.default_value 'default'
  c.flag :f
  c.action do |global_options,options,args|
    puts "list command ran"
  end
end

but I looking to warp the above code in a flag/switch

desc 'module do good things'
arg_name 'module'
flag [:m,:module] do |f|
  f.desc 'Describe the command of module flag here'
  f.arg_name 'MODULENAME'
  f.command :bruteforce do |c|
    c.action do |global_options,options,args|
      puts "bruteforcig......"
    end
  end
end
davetron5000 commented 6 years ago

I'm not sure what you are wanting to do—do you want to execute code whenever someone uses a flag? If so, you can do that by checking the flag in the command's action block.

In general, GLI is based on having flags/switches set options for the command and not to instigate actions themselves.

davetron5000 commented 3 years ago

Closing as old and I think not an issue? Please reopen if you have more info