summaryrefslogtreecommitdiff
path: root/lib/options.rb
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-01 18:38:49 +0200
committerPaul Buetow <paul@buetow.org>2026-03-01 18:38:49 +0200
commit08129b43792eeec92eacb9f47ce3f879489aa03c (patch)
treef527ba7c71c58536fa68b0acaef41dcd56a3cfc6 /lib/options.rb
parent861e526d9ad9228f2b740dbcf00cf39d8fe652f2 (diff)
make rcm usable as a gem from any directory, fix bugs and tests
- Add lib/rcm.rb entry point and bin/rcm CLI executable - Update gemspec: v0.1.0, proper files list, executables, runtime deps - Support standalone arg parsing and --hosts filtering in options - Fix inverted logic and typo in FileBackup#different? (== vs !=, cecksum_b) - Fix unqualified File.directory? resolving to RCM::File in Directory - Fix test_chown assertions running before evaluate! creates files - Add setup to file tests to prevent order-dependent failures Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'lib/options.rb')
-rw-r--r--lib/options.rb33
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/options.rb b/lib/options.rb
index 401d135..6ce2479 100644
--- a/lib/options.rb
+++ b/lib/options.rb
@@ -1,16 +1,33 @@
require 'optparse'
module RCM
- # Command line options
+ # Command line options, supports both Rake mode (args after --)
+ # and standalone mode (direct args). Unknown options are ignored
+ # so that test runners and other tools can pass their own flags.
module Options
- @@options = { debug: false, dry: false }
+ @@options = { debug: false, dry: false, hosts: [] }
- if (after_double_dash = ARGV.slice_before('--').to_a.last&.drop(1))
- OptionParser.new do |opts|
- opts.banner = 'Usage: rake [task] -- [options]'
- opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v }
- opts.on('-d', '--dry', 'dry mode') { |v| @@options[:dry] = v }
- end.parse!(after_double_dash)
+ parser = OptionParser.new do |opts|
+ opts.banner = 'Usage: rake [task] -- [options] OR ruby config.rb [options]'
+ opts.on('-v', '--[no-]debug', 'debug output') { |v| @@options[:debug] = v }
+ opts.on('-d', '--dry', 'dry mode') { |v| @@options[:dry] = v }
+ opts.on('--hosts HOSTS', 'comma-separated list of target hostnames') do |v|
+ @@options[:hosts] = v.split(',').map(&:strip)
+ end
+ end
+
+ # Rake passes args after '--'; standalone scripts pass args directly.
+ args = if ARGV.include?('--')
+ ARGV.slice_before('--').to_a.last.drop(1)
+ else
+ ARGV.dup
+ end
+
+ # Ignore unknown options (e.g. from test runners or other tools)
+ begin
+ parser.parse!(args)
+ rescue OptionParser::InvalidOption
+ retry
end
def option(key)