From 0c7849dc7ccc58dab299d09ac63da53d54862705 Mon Sep 17 00:00:00 2001 From: Tristan Ancelet Date: Tue, 11 Aug 2026 12:24:30 -0500 Subject: [PATCH] Added options --- src/cligen/app/generate.cr | 3 ++- src/cligen/command/selection.cr | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/cligen/app/generate.cr b/src/cligen/app/generate.cr index 53e30a3..8e2b5a4 100644 --- a/src/cligen/app/generate.cr +++ b/src/cligen/app/generate.cr @@ -28,7 +28,8 @@ module CliGen description: {{ anno[:description] }}, default: {% unless var.default_value.nil? %} {{var.default_value}} {% else %} nil {% end %}, validate: {% if anno[:validation] %} {{anno[:validation]}} {% else %} nil {% end %}, - on_match: {% if anno[:on_match] %} {{anno[:on_match]}} {% else %} nil {% end %} + on_match: {% if anno[:on_match] %} {{anno[:on_match]}} {% else %} nil {% end %}, + options: {% if anno[:options] %} {{anno[:options]}} {% else %} nil {% end %} ) {% debug if env("DEBUG") %} {% end %} diff --git a/src/cligen/command/selection.cr b/src/cligen/command/selection.cr index 85d0352..e2e2c5a 100644 --- a/src/cligen/command/selection.cr +++ b/src/cligen/command/selection.cr @@ -1,17 +1,20 @@ module CliGen class Command - macro selection(variable, short, long, description, options) + macro selection(variable, description, options, short = nil, long = nil, validation = nil, on_match = nil) {% raise "ERROR : CliGen::Command.selection : First selection must be a TypeDeclaration (ex: ' : [= val]')" unless variable.is_a? TypeDeclaration %} - {% raise "ERROR : CliGen::Command.selection : Provided short must be a string" unless short.is_a? StringLiteral || string == nil %} - {% raise "ERROR : CliGen::Command.selection : Provided long must be a string" unless long.is_a? StringLiteral || long == nil %} + {% if short %} + {% raise "ERROR : CliGen::Command.selection : Provided short must be a string" unless short.is_a? StringLiteral %} + {% end %} + {% long = "--#{variable.var}" if long.nil? %} + {% raise "ERROR : CliGen::Command.selection : Provided long must be a flag format" unless long =~ /^--[a-zA-Z0-9-_]+/ %} + {% raise "ERROR : CliGen::Command.selection : Provided long must be a string" unless long.is_a? StringLiteral %} {% raise "ERROR : CliGen::Command.selection : You must provide a short or long" unless long || short %} {% raise "ERROR : CliGen::Command.selection : You must provide a description" unless description %} {% raise "ERROR : CliGen::Command.selection : Provided description must be a String" unless description.is_a? StringLiteral %} {% options = options.resolve if options.is_a? Path %} {% raise "ERROR : CliGen::Command.selection : Provided options must be an ArrayLiteral" unless options.is_a? ArrayLiteral %} - {% raise "ERROR : CliGen::Command.selection : Provided options must be of the same type as the variable" unless options.of == variable.type %} - @[CliGen::Argument(short: {{short}}, long: {{long}}, description: {{description}}, validation: {{validation}}, on_match: {{on_match}})] + @[CliGen::Argument(short: {{short}}, long: {{long}}, description: {{description}}, validation: {{validation}}, on_match: {{on_match}}, options: {{options}})] @{{variable}} end end