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

Defining flags after or before command #19

Closed waghanza closed 4 years ago

waghanza commented 5 years ago

Hi @jwaldrip,

With 1.8.0 flags should be defined after the called command.

I mean that this command is working

bin/droplet create -l ruby -f sinatra 

but this does not

bin/droplet -l ruby -f sinatra create

in https://github.com/waghanza/http-benchmark/blob/cloudify/tools/providers/digitalocean.cr#L31

Was is a design choice ?

Regards,

FilBot3 commented 5 years ago

Admiral itself can do that. It might be a function of the library you're trying to use?

require "admiral"

# TODO: Write documentation for `AdmiralCliTest`
module AdmiralCliTest
  VERSION = "0.1.0"

  class HelloWorld < Admiral::Command
    define_help description: "This is help"
    define_argument planet : String,
      description: "The name of the planet to say hello to",
      default: "World",
      required: false
    define_flag number_of_greetings : UInt32,
      default: 1_u32,
      long: times,
      short: t,
      description: "The number of times to print the greeting!",
      required: false

    register_sub_command solar : AdmiralCliTest::Planetary,
      description: "Say Hello to everyone on the planet"
    register_sub_command city : AdmiralCliTest::Municipality,
      description: "Say hello to everyone in the city"

    define_version "1.0.0"

    def run
      flags.number_of_greetings.times do
        puts "Hello #{arguments.planet || "World"}!"
      end
    end
  end

  class Planetary < Admiral::Command
    define_help description: "This is a subcommand for planets"

    def run
      puts "Hello Everyone on the Planet!"
    end
  end

  class Municipality < Admiral::Command
    define_help description: "This is a subcommand for cities"

    def run
      puts "Hello everyone in the city!!!"
    end
  end
end

AdmiralCliTest::HelloWorld.run

This works if ran like this...

user01@computer [08:48:17] ~/Documents/development/Crystal/admiral_cli_test
$ ./admiral_cli_test --times 3 Earf
Hello Earf!
Hello Earf!
Hello Earf!
user01@computer [08:50:03] ~/Documents/development/Crystal/admiral_cli_test
$ ./admiral_cli_test Earf --times 3
Hello Earf!
Hello Earf!
Hello Earf!
user01@computer [08:50:08] ~/Documents/development/Crystal/admiral_cli_test
$ ./admiral_cli_test --help
Usage:
  ./admiral_cli_test [flags...] <planet> [arg...]

This is help

Flags:
  --help                        # Displays help for the current command.
  --times, -t (default: 1_u32)  # The number of times to print the greeting!
  --version                     # Displays the version of the current application.

Arguments:
  planet                        # The name of the planet to say hello to

Subcommands:
  city                          # Say hello to everyone in the city
  solar                         # Say Hello to everyone on the planet
jwaldrip commented 4 years ago

@waghanza you can access the parent command flags using the parent keyword.

parent.flags

Additionally, I will be adding support to make all flags from the parent, available to child commands.