From 7c1ab2f3cc200a526729fb16630d9142e491b047 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sun, 25 Oct 2020 02:28:59 +0200 Subject: [PATCH] Baguette::Configuration.option_parser --- src/baguette-crystal-base.cr | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/baguette-crystal-base.cr b/src/baguette-crystal-base.cr index f115fc6..7d58c84 100644 --- a/src/baguette-crystal-base.cr +++ b/src/baguette-crystal-base.cr @@ -39,6 +39,58 @@ class Baguette::Configuration end end end + + # Read options from the CLI. + # We currently want to know: + # - the program verbosity + # - the configuration file to use + # - if the configuration files should be ignored + # - if this is just a simulation (used to print configuration then quit) + def self.option_parser + simulation = no_configuration = configuration_file = nil + + help = false + + OptionParser.parse do |parser| + parser.banner = "usage: #{PROGRAM_NAME} [-ns][-c configuration-file]" + + parser.on "-s", "--simulation", + "Print configuration then quit." do + simulation = true + end + + parser.on "-n", "--no-configuration", + "No configuration file should be read." do + no_configuration = true + end + + parser.on "-v verbosity", "--verbosity level", + "Verbosity level. From 0 to 3. Default: 1" do |v| + Baguette::Context.verbosity = v.to_i + end + + parser.on "-c file", "--configuration file", + "Configuration file." do |file| + configuration_file = file + end + + parser.invalid_option do |arg| + # Do not print anything: we only check for configuration file stuff. + end + + parser.on "-h", "--help", "Show this help" do + Baguette::Log.warning "for the first option parsing!" + puts parser + help = true + end + end + + # Options are removed once read from the ARGV array, but we want to propagate + # this particular option to print the second set of options. + ARGV.push "-h" if help + + return simulation, no_configuration, configuration_file + end end class Baguette::Log