From 0e091ee1c538bc4095dc2078bf1e17c1db41bf2a Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 11 Aug 2025 17:22:48 +0000 Subject: [PATCH 1/4] Initial plan From bf4b3848f1f7335d2367de5e7ee9b44679feecbe Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 11 Aug 2025 17:33:11 +0000 Subject: [PATCH 2/4] Refactor CLI to use Ruby stdlib OptionParser Co-authored-by: markets <576701+markets@users.noreply.github.com> --- bin/timeago | 78 ++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/bin/timeago b/bin/timeago index 29a8581..9c0cf02 100755 --- a/bin/timeago +++ b/bin/timeago @@ -1,58 +1,56 @@ #!/usr/bin/env ruby require_relative "../lib/jekyll-timeago" +require "optparse" -help_message = <<~HELP_MESSAGE - Usage: +options = {} +show_help = false +show_version = false +start_console = false - timeago [to_date] +parser = OptionParser.new do |opts| + opts.banner = "Usage:\n\n timeago [to_date]\n\nNotes:\n [to_date] Optional, defaults to current date\n\nOptions:" - Notes: - [to_date] Optional, defaults to current date + opts.on("-h", "--help", "Prints this message") do + show_help = true + end - Options: - --help, -h Prints this message - --version, -v Prints the current version - --console, -c Starts an interactive IRB session with jekyll-timeago included - --locale, -l Uses the provided locale - --style, -s Uses the provided style (short) -HELP_MESSAGE + opts.on("-v", "--version", "Prints the current version") do + show_version = true + end -if ARGV.empty? || ARGV.include?("--help") || ARGV.include?("-h") - puts help_message -elsif ARGV.include?("--version") || ARGV.include?("-v") - puts "v#{Jekyll::Timeago::VERSION}" -elsif ARGV.include?("--console") || ARGV.include?("-c") - require "irb" - include Jekyll::Timeago + opts.on("-c", "--console", "Starts an interactive IRB session with jekyll-timeago included") do + start_console = true + end - ARGV.clear - IRB.start -else - options = {} - - # Handle locale option - custom_locale = "--locale" if ARGV.include?("--locale") - custom_locale = "-l" if ARGV.include?("-l") - - if custom_locale - index = ARGV.index(custom_locale) - locale = ARGV.delete_at(index + 1) - ARGV.delete_at(index) + opts.on("-l", "--locale LOCALE", "Uses the provided locale") do |locale| options[:locale] = locale end - # Handle style option - custom_style = "--style" if ARGV.include?("--style") - custom_style = "-s" if ARGV.include?("-s") - - if custom_style - index = ARGV.index(custom_style) - style = ARGV.delete_at(index + 1) - ARGV.delete_at(index) + opts.on("-s", "--style STYLE", "Uses the provided style (short)") do |style| options[:style] = style end +end + +begin + parser.parse! +rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e + puts "Error! #{e}" + puts parser + exit 1 +end +if show_help + puts parser +elsif show_version + puts "v#{Jekyll::Timeago::VERSION}" +elsif start_console + require "irb" + include Jekyll::Timeago + IRB.start +elsif ARGV.empty? + puts parser +else ARGV << options if !options.empty? begin From a6b3312bd988a612a003292ba17ac53a9061cf03 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Mon, 11 Aug 2025 20:32:38 +0200 Subject: [PATCH 3/4] small tweaks --- bin/timeago | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bin/timeago b/bin/timeago index 9c0cf02..c037d1b 100755 --- a/bin/timeago +++ b/bin/timeago @@ -9,7 +9,16 @@ show_version = false start_console = false parser = OptionParser.new do |opts| - opts.banner = "Usage:\n\n timeago [to_date]\n\nNotes:\n [to_date] Optional, defaults to current date\n\nOptions:" + opts.banner = <<~BANNER + Usage: + + timeago [to_date] + + Notes: + [to_date] Optional, defaults to current date + + Options: + BANNER opts.on("-h", "--help", "Prints this message") do show_help = true @@ -27,7 +36,7 @@ parser = OptionParser.new do |opts| options[:locale] = locale end - opts.on("-s", "--style STYLE", "Uses the provided style (short)") do |style| + opts.on("-s", "--style STYLE", "Uses the provided style (short, array)") do |style| options[:style] = style end end From 70ff8c7451df22b652b318b3451acd69e9328166 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Mon, 11 Aug 2025 20:33:27 +0200 Subject: [PATCH 4/4] rename to old name --- bin/timeago | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/timeago b/bin/timeago index c037d1b..45761da 100755 --- a/bin/timeago +++ b/bin/timeago @@ -9,7 +9,7 @@ show_version = false start_console = false parser = OptionParser.new do |opts| - opts.banner = <<~BANNER + opts.banner = <<~HELP_MESSAGE Usage: timeago [to_date] @@ -18,7 +18,7 @@ parser = OptionParser.new do |opts| [to_date] Optional, defaults to current date Options: - BANNER + HELP_MESSAGE opts.on("-h", "--help", "Prints this message") do show_help = true