From bf2075be9e0db14ecae9be258d66aade68528d36 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Fri, 25 Oct 2019 13:10:20 +0200 Subject: [PATCH] get-port has a proper CLI. --- src/get-port.cr | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/get-port.cr b/src/get-port.cr index 9757484..d1d7b7f 100644 --- a/src/get-port.cr +++ b/src/get-port.cr @@ -1,11 +1,33 @@ require "file_utils" +require "option_parser" + require "./config.cr" START_PORT = 49152 PORTS_CACHE_DIRECTORY = "#{CACHE_DIRECTORY}/ports/" -service = ARGV[0] -wanted_default_port = ARGV[1]?.try &.to_i +service = "" +wanted_default_port : String? = nil + +parser = OptionParser.parse do |parser| + parser.banner = "usage: get-post [default-port] [options]\n" + + "options:\n" + + parser.on "-h", "--help", "Prints this help message." do + puts parser + exit 0 + end + + parser.unknown_args do |arg| + if arg.size < 1 || arg.size > 2 + puts parser + exit 1 + end + + service = arg[0] + wanted_default_port = arg[1]? + end +end service_port_file = "#{PORTS_CACHE_DIRECTORY}/#{service.gsub /\//, ":"}"