Class: Synthra::CLI::Commands::Base
- Inherits:
-
Object
- Object
- Synthra::CLI::Commands::Base
- Defined in:
- lib/synthra/cli/commands/base.rb
Overview
Base class for all CLI commands
Provides common functionality for command execution including option parsing, error handling, and registry management.
Constant Summary collapse
- EXIT_SUCCESS =
Exit codes
0- EXIT_PARSE_ERROR =
1- EXIT_RUNTIME_ERROR =
2
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#call ⇒ Integer
Execute the command.
-
#error(message) ⇒ Object
protected
Print error message.
-
#info(message) ⇒ Object
protected
Print info message.
-
#initialize(args, options) ⇒ Base
constructor
Create a new command instance.
-
#load_path(registry, path, validate: false) ⇒ Object
protected
Load from path (file or directory).
-
#load_schemas_from_dir(registry, dir, validate: true) ⇒ void
protected
Load schemas from a directory.
-
#success(message) ⇒ Object
protected
Print success message.
Constructor Details
#initialize(args, options) ⇒ Base
Create a new command instance
24 25 26 27 |
# File 'lib/synthra/cli/commands/base.rb', line 24 def initialize(args, ) @args = args @options = end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
17 18 19 |
# File 'lib/synthra/cli/commands/base.rb', line 17 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/synthra/cli/commands/base.rb', line 17 def @options end |
Instance Method Details
#call ⇒ Integer
Execute the command
33 34 35 |
# File 'lib/synthra/cli/commands/base.rb', line 33 def call raise NotImplementedError, "#{self.class} must implement #call" end |
#error(message) ⇒ Object (protected)
Print error message
91 92 93 |
# File 'lib/synthra/cli/commands/base.rb', line 91 def error() $stderr.puts "✗ #{}" end |
#info(message) ⇒ Object (protected)
Print info message
99 100 101 |
# File 'lib/synthra/cli/commands/base.rb', line 99 def info() puts end |
#load_path(registry, path, validate: false) ⇒ Object (protected)
Load from path (file or directory)
71 72 73 74 75 76 77 |
# File 'lib/synthra/cli/commands/base.rb', line 71 def load_path(registry, path, validate: false) if File.directory?(path) load_schemas_from_dir(registry, path, validate: validate) else registry.load_file(path, validate: validate) end end |
#load_schemas_from_dir(registry, dir, validate: true) ⇒ void (protected)
This method returns an undefined value.
Load schemas from a directory
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/synthra/cli/commands/base.rb', line 46 def load_schemas_from_dir(registry, dir, validate: true) dir = File.(dir) raise Errno::ENOENT, "Directory not found: #{dir}" unless Dir.exist?(dir) pattern = File.join(dir, "*.dsl") files = Dir.glob(pattern) if files.empty? $stderr.puts "No .dsl files found in #{dir}" return end files.each do |f| file_path = File.(f) raise ArgumentError, "Invalid file path: #{f}" unless file_path.start_with?(dir) registry.load_file(f, validate: validate) end end |
#success(message) ⇒ Object (protected)
Print success message
83 84 85 |
# File 'lib/synthra/cli/commands/base.rb', line 83 def success() puts "✓ #{}" end |