jwaldrip / admiral.cr

A robust DSL for writing command line interfaces written in Crystal.
https://jwaldrip.github.com/admiral.cr
MIT License
135 stars 14 forks source link

Flags inheritance with subcommands #11

Closed anykeyh closed 4 years ago

anykeyh commented 6 years ago

Hello,

I'm unhappy about how to keep active some "global" flags when using subcommands

This is an example where a --verbose flag is setup on top of every subcommands:

require "admiral"

module Log
  class_property verbose = false
end

class HelloWorld < Admiral::Command
  def run
    Log.verbose = self.flags.verbose

    puts "Hello World"
  end

  class SubCommand < Admiral::Command
    def run
      Log.verbose = self.parent.as(HelloWorld).flags.verbose

      puts "SubCommand"
    end
  end

  define_flag verbose : Bool, default: false, short: v

  register_sub_command sub_command, type: SubCommand
end

HelloWorld.run

My main problem is I need to repeat the code Log.verbose = self.parent.as(HelloWorld).flags.verbose on every run for each subcommands or the flag will be ignored.

Would it be great if we can put a block on define_flag like this:

define_flag(verbose : Bool, default: false, short: v){ |flag| Log.verbose = flag }

This code would be triggered even if run of the command is not called due to subcommand

jwaldrip commented 6 years ago

Good suggestion @anykeyh, Ill see about adding that to the next version