### Define CLI commands and options with Drydock Source: https://github.com/delano/drydock/blob/main/README.md This example demonstrates how to initialize a Drydock application, define a default command, set up command-line options, and implement custom command classes by inheriting from Drydock::Command. ```ruby require 'drydock' extend Drydock default :welcome before do # You can execute a block before the requests command is executed. end about "A friendly welcome to the Drydock" command :welcome do puts "Welcome to Drydock." puts "For available commands:" puts "#{$0} show-commands" end usage "USAGE: #{$0} laugh [-f]" about "The captain commands his crew to laugh" option :f, :faster, "A boolean value. Go even faster!" command :laugh do |obj| answer = !obj.option.faster ? "Sort of" : "Yes! I'm literally laughing as fast as possible." puts "Captain Stubing: Are you laughing?" puts "Dr. Bricker: " << answer end class JohnWestSmokedOysters < Drydock::Command def ahoy!; p "matey"; end end about "Do something with John West's Smoked Oysters" command :oysters => JohnWestSmokedOysters do |obj| p obj end about "My way of saying hello!" command :ahoy! => JohnWestSmokedOysters Drydock.run! ``` -------------------------------- ### Implement Lifecycle Hooks with before and after Source: https://context7.com/delano/drydock/llms.txt The before and after methods define blocks that execute around command execution. These are ideal for setup tasks like logging or resource initialization and teardown tasks like cleanup. ```ruby require 'drydock' extend Drydock before do |obj| @start_time = Time.now puts "[#{@start_time}] Starting command: #{obj.cmd}" end after do |obj| elapsed = Time.now - @start_time puts "[DONE] Command '#{obj.cmd}' completed in #{elapsed.round(2)}s" end about 'Perform a task' command :task do |obj| puts "Executing task..." sleep 0.5 end Drydock.run! ``` -------------------------------- ### Implementing Command-Specific Options Source: https://context7.com/delano/drydock/llms.txt Shows how to define options for specific commands using the 'option' method. Includes examples of type coercion and using blocks for custom validation or transformation of option values. ```ruby require 'drydock' extend Drydock about 'Process a file with custom settings' option :f, :file, String, 'Path to the input file' option :n, :count, Integer, 'Number of iterations' option :v, :verbose, 'Enable verbose output' option :t, :timeout, Float, 'Timeout in seconds' do |v| v = 30.0 if v > 30.0 # Limit max timeout v end command :process do |obj| puts "Processing: #{obj.option.file}" if obj.option.file puts "Iterations: #{obj.option.count || 1}" puts "Verbose mode enabled" if obj.option.verbose puts "Timeout: #{obj.option.timeout || 5.0}s" end Drydock.run! ``` -------------------------------- ### Get Current Cursor Position (Drydock DSL) Source: https://context7.com/delano/drydock/llms.txt Retrieves the current X and Y coordinates of the cursor. This allows for reading the cursor's position for subsequent logic. ```drydock # Get current cursor position x, y = Cursor.position ``` -------------------------------- ### Defining CLI Commands with Drydock Source: https://context7.com/delano/drydock/llms.txt Demonstrates how to define a command using the 'command' method. It shows how to provide metadata like 'about' and 'usage' and access parsed options within the command block. ```ruby require 'drydock' extend Drydock about 'Display a greeting message' usage "#{$0} greet [--loud]" command :greet do |obj| message = "Hello, welcome to Drydock!" message = message.upcase if obj.option.loud puts message end Drydock.run! ``` -------------------------------- ### Setting a Default Command Source: https://context7.com/delano/drydock/llms.txt Demonstrates how to configure a default command that executes when no specific command is provided by the user. This can be an existing command or an inline block definition. ```ruby require 'drydock' extend Drydock default :help # Set existing command as default about 'Show help information' command :help do puts "Available commands:" puts " help - Show this help" puts " version - Show version" puts " run - Execute main task" end about 'Show version' command :version do puts "MyApp v1.0.0" end Drydock.run! ``` -------------------------------- ### Configuring Global Options Source: https://context7.com/delano/drydock/llms.txt Explains how to define global options that are accessible across all commands. These are defined before command definitions and are accessed via the 'obj.global' object. ```ruby require 'drydock' extend Drydock global_usage "USAGE: #{$0} [global options] command [command options]" global :q, :quiet, 'Suppress output' global :v, :verbose, 'Increase verbosity (use -vvv for max)' do |_v| @verbosity ||= 0 @verbosity += 1 end global :c, :config, String, 'Path to configuration file' about 'Run the main task' command :run do |obj| if obj.global.quiet # Silent mode elsif obj.global.verbose && obj.global.verbose >= 2 puts "High verbosity mode" puts "Config: #{obj.global.config}" if obj.global.config else puts "Running task..." end end Drydock.run! ``` -------------------------------- ### Console Styling with String Extensions Source: https://context7.com/delano/drydock/llms.txt Drydock extends the String class with methods for ANSI color, background, and text attributes. It also provides utilities to strip formatting or disable styling globally for log compatibility. ```ruby require 'drydock' require 'drydock/mixins' puts "Success!".colour(:green) puts "WARNING".colour(:yellow, :red, :bright) puts "Important".att(:underline) styled = "Colored".colour(:red) puts styled.noatt.size ``` -------------------------------- ### Move Cursor (Drydock DSL) Source: https://context7.com/delano/drydock/llms.txt Provides methods to move the cursor to an absolute position or a relative offset. Supports moving to specific X, Y coordinates and relative movements up, down, left, and right. ```drydock # Move to x=10, y=5 Cursor.move = [10, 5] # Move up 2 lines Cursor.up(2) # Move down 1 line Cursor.down(1) # Move left 5 columns Cursor.left(5) # Move right 3 columns Cursor.right(3) ``` -------------------------------- ### Create Custom Command Classes Source: https://context7.com/delano/drydock/llms.txt Custom command classes inherit from Drydock::Command to provide object-oriented command implementations. This allows for shared state and helper methods within specific command logic. ```ruby require 'drydock' extend Drydock class DatabaseCommand < Drydock::Command def init @connection = "db://localhost:5432" end def migrate puts "Running migrations on #{@connection}..." puts "Verbose: #{global.verbose}" if global.verbose end def seed puts "Seeding database at #{@connection}..." end end global :v, :verbose, 'Enable verbose output' about 'Run database migrations' command :migrate => DatabaseCommand about 'Seed the database' command :seed => DatabaseCommand Drydock.run! ``` -------------------------------- ### Execute Drydock Script Manually Source: https://github.com/delano/drydock/blob/main/CHANGES.txt Since the removal of the at_exit hook, Drydock scripts must be executed explicitly. This snippet demonstrates how to trigger the command runner using the current arguments and standard input. ```ruby Drydock.run!(ARGV, STDIN) if Drydock.run? ``` -------------------------------- ### Terminal Cursor and Positioning Source: https://context7.com/delano/drydock/llms.txt The Console and Cursor modules allow for precise terminal manipulation, including clearing the screen, querying terminal dimensions, and printing text at specific coordinates or alignments. ```ruby require 'drydock/console' Console.clear width = Console.width Console.print_at("Status: OK", x: 0, y: 0) Console.print_center("Centered text", y: 10) ``` -------------------------------- ### Map Named Arguments with argv Source: https://context7.com/delano/drydock/llms.txt The argv method maps positional command-line arguments to named fields on the obj.argv object. The final argument can capture remaining inputs as an array. ```ruby require 'drydock' extend Drydock about 'Copy files to destination' argv :source, :destination command :copy do |obj| puts "Source: #{obj.argv.source}" puts "Destination: #{obj.argv.destination}" end about 'Process multiple input files' argv :output, :inputs command :merge do |obj| puts "Output file: #{obj.argv.output}" puts "Input files: #{obj.argv.inputs.join(', ')}" if obj.argv.inputs end Drydock.run! ``` -------------------------------- ### Thread-Safe Screen Output Source: https://context7.com/delano/drydock/llms.txt The Drydock::Screen module provides a mutex-protected buffer for thread-safe printing in concurrent CLI applications. Output is collected and can be flushed to the terminal on demand. ```ruby require 'drydock' require 'drydock/screen' threads = [] 5.times do |i| threads << Thread.new do Drydock::Screen.puts "Thread #{i}: Processing..." end end threads.each(&:join) Drydock::Screen.flush ``` -------------------------------- ### Save and Restore Cursor Position (Drydock DSL) Source: https://context7.com/delano/drydock/llms.txt Saves the current cursor position for later restoration. This is useful for temporary cursor movements that need to be reverted. ```drydock # Save current position Cursor.save # Restore saved position Cursor.restore ``` -------------------------------- ### Capture Command Output Source: https://context7.com/delano/drydock/llms.txt The capture method redirects STDOUT or STDERR to a buffer, which can be retrieved after command execution using Drydock.captured. This is useful for logging command results to files or processing output programmatically. ```ruby require 'drydock' extend Drydock capture :stdout command :generate do |obj| puts "Line 1: Generated content" end after do |obj| output = Drydock.captured File.write('output.log', output) if output end Drydock.run! ``` -------------------------------- ### Define Command Aliases Source: https://context7.com/delano/drydock/llms.txt Command aliases allow multiple names to trigger the same command. The canonical name is accessed via obj.cmd, while the invoked name is available via obj.alias. ```ruby require 'drydock' extend Drydock about 'Install dependencies' command :install, :i, :add do |obj| puts "Installing dependencies..." puts "(invoked via alias: #{obj.alias})" if obj.alias != obj.cmd end about 'Remove a package' command :remove do |obj| puts "Removing package..." end command_alias :remove, :rm Drydock.run! ``` -------------------------------- ### Ignore Option Parsing with Drydock Source: https://context7.com/delano/drydock/llms.txt The ignore :options directive instructs Drydock to bypass standard option parsing, allowing raw arguments to be accessed directly via obj.argv. This is essential for commands requiring custom argument handling logic. ```ruby require 'drydock' extend Drydock about 'Execute with raw arguments' ignore :options command :raw do |obj| puts "Raw arguments received:" obj.argv.each_with_index do |arg, i| puts " [#{i}] #{arg}" end end Drydock.run! ``` -------------------------------- ### Process STDIN Streams Source: https://context7.com/delano/drydock/llms.txt The stdin block allows pre-processing of piped input before command execution. The processed result is accessible via obj.stdin within the command block. ```ruby require 'drydock' extend Drydock stdin do |stdin, output| unless stdin.tty? until stdin.eof? line = stdin.readline.chomp (output ||= []) << line unless line.empty? end end output end about 'Process input lines' option :u, :uppercase, 'Convert to uppercase' command :process do |obj| if obj.stdin && obj.stdin.any? obj.stdin.each do |line| output = obj.option.uppercase ? line.upcase : line puts output end end end Drydock.run! ``` -------------------------------- ### Handle Unknown Commands with Trawler Source: https://context7.com/delano/drydock/llms.txt The trawler directive defines a fallback command to execute when a user inputs an unrecognized command name. This prevents errors and allows for custom help or suggestion logic. ```ruby require 'drydock' extend Drydock command :catch_all do |obj| puts "Unknown command: '#{obj.alias}'" end trawler :catch_all Drydock.run! ``` -------------------------------- ### Clear Current Line (Drydock DSL) Source: https://context7.com/delano/drydock/llms.txt Clears the content of the current line where the cursor is located. This is a simple utility for cleaning up terminal output. ```drydock # Clear current line Cursor.clear_line ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.