### Subcommand Implementation Example Source: https://context7.com/piotrmurach/tty/llms.txt Provides the implementation for a subcommand, specifically the 'start' action for a server. This class inherits from a base command class and handles initialization with arguments like port and options, then executes the server start logic. ```ruby # lib/myapp/commands/server/start.rb - Subcommand implementation require_relative "../../command" module Myapp module Commands class Server class Start < Myapp::Command def initialize(port, options) @port = port @options = options end def execute(input: $stdin, output: $stdout) output.puts "Starting server on port #{@port}" if @options[:daemon] output.puts "Running in daemon mode" end end end end end end ``` -------------------------------- ### Install TTY Gem Source: https://github.com/piotrmurach/tty/blob/master/README.md This snippet shows how to add the TTY gem to your Ruby project's Gemfile for installation. It covers both installing all components and specific ones. ```ruby gem 'tty' # or gem 'tty-*' ``` -------------------------------- ### Interactive Prompts with TTY::Prompt Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates how to use TTY::Prompt to get user input through various interactive methods like asking for text, selecting from choices, and confirming actions. Requires the 'tty-prompt' gem. ```ruby require "tty-prompt" prompt = TTY::Prompt.new answer = prompt.ask("What is your name?", default: "Anonymous") choice = prompt.select("Choose role:", %w[Admin User Guest]) confirmed = prompt.yes?("Continue?") ``` -------------------------------- ### Create New CLI Application with teletype new Source: https://context7.com/piotrmurach/tty/llms.txt Scaffolds a new command-line application skeleton, including Gemfile, executable, CLI class, command base class, and test setup. It utilizes Bundler and TTY-specific templates for efficient CLI development. Options allow customization of author, license, and testing framework. ```bash # Create a new CLI application called "myapp" teletype new myapp # Output: # Creating gem 'myapp' # create myapp/Gemfile # create myapp/.gitignore # create myapp/lib/myapp.rb # create myapp/lib/myapp/version.rb # create myapp/lib/myapp/cli.rb # create myapp/lib/myapp/command.rb # create myapp/exe/myapp # create myapp/LICENSE.txt # Initializing git repo in myapp # # Your teletype project has been created successfully in directory "myapp". # Create with specific options teletype new myapp \ --author "John Doe" \ --license apache \ --test minitest \ --ext # Generated structure: # myapp/ # ├── exe/ # │ └── myapp # Executable entry point # ├── lib/ # │ ├── myapp/ # │ │ ├── commands/ # Command implementations # │ │ ├── templates/ # Command-specific templates # │ │ ├── cli.rb # Thor-based CLI class # │ │ ├── command.rb # Base command class # │ │ └── version.rb # │ └── myapp.rb # ├── spec/ # or test/ with minitest # │ ├── integration/ # │ ├── support/ # │ └── unit/ # ├── myapp.gemspec # ├── Gemfile # └── LICENSE.txt ``` -------------------------------- ### Executing Teletype CLI Application Source: https://github.com/piotrmurach/tty/blob/master/README.md The 'start' method within the application's CLI class is responsible for parsing command-line arguments. This is typically called from the application's executable file. ```ruby App::CLI.start ``` -------------------------------- ### Implement Nested Commands with Thor Source: https://context7.com/piotrmurach/tty/llms.txt Shows how to structure nested commands using Thor in Ruby. The parent command class acts as a namespace, dispatching to subcommand classes for specific actions like starting or stopping a server. ```ruby # lib/myapp/commands/server.rb - Subcommand namespace require "thor" module Myapp module Commands class Server < Thor namespace :server desc "start PORT", "Start the server" method_option :daemon, type: :boolean, aliases: "-d", desc: "Run as daemon" def start(port) if options[:help] invoke :help, ["start"] else require_relative "server/start" Myapp::Commands::Server::Start.new(port, options).execute end end desc "stop", "Stop the server" def stop(*) if options[:help] invoke :help, ["stop"] else require_relative "server/stop" Myapp::Commands::Server::Stop.new(options).execute end end end end end ``` -------------------------------- ### Define Command with Variadic Arguments (Bash & Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Illustrates how to define a command (`get`) that accepts any number of arguments (`*names`) by prefixing the argument name with an asterisk in the `--args` flag. The generated Ruby code uses the splat operator (`*`) for the argument. ```bash $ teletype add get --args *names ``` ```ruby module App class CLI < Thor desc 'get NAMES...', 'Get configuration options' def get(*names) ... end end end ``` -------------------------------- ### Access Command Options in Execute Method (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Provides an example of how to access the options passed to a command within its `execute` method. It checks for the presence of an `:edit` option to determine program flow. ```ruby module App module Commands class Config < App::Command def initialize(options) @options = options end def execute if options[:edit] editor.open('path/to/config/file') end end end end end ``` -------------------------------- ### Mix Argument Styles for Command Definition (Bash) Source: https://github.com/piotrmurach/tty/blob/master/README.md Shows an example of combining different argument definition styles (required and variadic) when using the `teletype add` command with the `--args` flag. ```bash $ teletype add config --args file *names ``` -------------------------------- ### Generated CLI Entry Point for 'config' Command (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Illustrates the generated `lib/app/cli.rb` file, which includes the `config` command. This code handles help requests for the command and invokes the actual command logic from `App::Commands::Config`. ```ruby module App class CLI < Thor desc 'config', 'Command description...' def config(*) if options[:help] invoke :help, ['config'] else require_relative 'commands/config' App::Commands::Config.new(options).execute end end end end ``` -------------------------------- ### Implement Subcommand Logic in Ruby Source: https://github.com/piotrmurach/tty/blob/master/README.md This Ruby code defines the `Set` class, inheriting from `App::Command`, which contains the core implementation for the `config set` subcommand. The `initialize` method sets up instance variables for `name`, `value`, and `options`, while the `execute` method is a placeholder for the command's logic. ```ruby # frozen_string_literal: true require_relative '../../command' module App module Commands class Config class Set < App::Command def initialize(name, value, options) @name = name @value = value @options = options end def execute # Command logic goes here ... end end end end end ``` -------------------------------- ### Create New CLI App with Teletype Source: https://github.com/piotrmurach/tty/blob/master/README.md This command-line snippet demonstrates how to use the `teletype` executable to quickly scaffold a new command-line application. It initializes a project with a basic structure. ```bash $ teletype new app ``` -------------------------------- ### Define Multiple Options Shorthand (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Illustrates the use of `method_options` as a concise way to define multiple command options and flags simultaneously, specifying their types and aliases. ```ruby method_options %w(list -l) => :boolean, :system => :boolean, :local => :boolean ``` -------------------------------- ### Teletype New Command Help Options Source: https://github.com/piotrmurach/tty/blob/master/README.md To view all available options for the 'teletype new' command, use the '--help' or '-h' flag. This displays a list of configurable parameters for project generation. ```bash $ teletype new --help $ teletype new -h ``` -------------------------------- ### Enable Executable Binary with Teletype Source: https://github.com/piotrmurach/tty/blob/master/README.md Use the '--ext' flag with the 'teletype new' command to create a binary executable file (e.g., 'exe/GEM_NAME') for your application. This option is disabled by default. ```bash $ teletype new app --ext ``` -------------------------------- ### Manage TTY Plugins Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates how to manage TTY plugins using TTY.plugins. This includes loading plugins from a gemspec, iterating over loaded plugins, activating all plugins, checking for specific plugins, and registering custom plugins. ```ruby require "tty" # Load plugins from gemspec matching pattern TTY.plugins.load_from("myapp.gemspec", /^tty-(.*)/) # Iterate over loaded plugins TTY.plugins.each do |plugin| puts "Plugin: #{plugin.name}, Enabled: #{plugin.enabled?}" end # Activate all plugins (adds to $LOAD_PATH) TTY.plugins.activate # Check specific plugin TTY.plugins.names["tty-prompt"] # => Plugin instance or nil # Register custom plugin gem_spec = Gem::Specification.find_by_name("tty-prompt") custom_plugin = TTY::Plugin.new("tty-prompt", gem_spec) TTY.plugins.register("tty-prompt", custom_plugin) # Plugin class usage plugin = TTY::Plugin.new("tty-command", gem_spec) plugin.enabled? # => false plugin.load! # requires the gem plugin.enabled? # => true ``` -------------------------------- ### Generated Command Implementation for 'Config' (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Presents the generated `lib/app/commands/config.rb` file. This code defines the `Config` class, inheriting from `App::Command`, and includes the `execute` method where the command's core logic should be implemented. ```ruby module App module Commands class Config < App::Command def initialize(options) @options = options end def execute # Command logic goes here ... end end end end ``` -------------------------------- ### Shell Execution with TTY::Command Source: https://context7.com/piotrmurach/tty/llms.txt Shows how to execute shell commands and capture their output using TTY::Command. This allows for programmatic control over external processes. Requires the 'tty-command' gem. ```ruby require "tty-command" cmd = TTY::Command.new(printer: :pretty) result = cmd.run("echo", "Hello World") puts result.output ``` -------------------------------- ### Formatted Tables with TTY::Table Source: https://context7.com/piotrmurach/tty/llms.txt Explains how to create and render formatted tables in the terminal using TTY::Table. This is useful for displaying structured data clearly. Requires the 'tty-table' gem. ```ruby require "tty-table" table = TTY::Table.new(["Header 1", "Header 2"], [["Row 1, Col 1", "Row 1, Col 2"], ["Row 2, Col 1", "Row 2, Col 2"]]) puts table.render(:unicode) ``` -------------------------------- ### Teletype CLI Application Entry Point Source: https://github.com/piotrmurach/tty/blob/master/README.md The 'lib/app/cli.rb' file serves as the main entry point for a Teletype-generated command-line application. It inherits from Thor for command parsing and defines application commands. ```ruby module App class CLI < Thor # Error raised by this runner Error = Class.new(StandardError) desc 'version', 'app version' def version require_relative 'version' puts "v#{App::VERSION}" end map %w(--version -v) => :version end end ``` -------------------------------- ### Thor-Based CLI Class Structure in Ruby Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates the generated CLI class structure which inherits from Thor for command routing, help text, and version display. Commands are defined as methods using Thor's DSL for arguments and options. This snippet also shows the executable entry point. ```ruby # lib/myapp/cli.rb - Generated CLI structure require "thor" module Myapp class CLI < Thor # Error raised by this runner Error = Class.new(StandardError) desc "version", "myapp version" def version require_relative "version" puts "v#{Myapp::VERSION}" end map %w[--version -v] => :version desc "config [FILE]", "Set and get configuration options" method_option :help, aliases: "-h", type: :boolean, desc: "Display usage information" method_option :edit, type: :boolean, aliases: ["-e"], desc: "Opens an editor to modify the config file" method_option :add, type: :array, banner: "name value", desc: "Adds a new line to the config file" def config(file = nil) if options[:help] invoke :help, ["config"] else require_relative "commands/config" Myapp::Commands::Config.new(file, options).execute end end # Subcommand registration require_relative "commands/server" register Myapp::Commands::Server, "server", "server [SUBCOMMAND]", "Server management" end end # exe/myapp - Executable entry point #!/usr/bin/env ruby require "myapp/cli" Myapp::CLI.start ``` -------------------------------- ### Configure Testing Framework with Teletype Source: https://github.com/piotrmurach/tty/blob/master/README.md The '--test' or '-t' flag configures the project to work with either 'rspec' or 'minitest'. The 'GEM_NAME.gemspec' file and testing directories are set up accordingly. 'RSpec' is the default framework. ```bash $ teletype new app --test=minitest $ teletype new app -t=minitest ``` -------------------------------- ### Execute Shell Commands with TTY::Command Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates how to execute shell commands using TTY::Command within a Ruby class. It shows how to initialize the command runner and handle the success or failure of the executed command, printing output to stdout. ```ruby require_relative "../command" module Myapp module Commands class Deploy < Myapp::Command def initialize(environment, version, options) @environment = environment @version = version @options = options end def execute(input: $stdin, output: $stdout) output.puts "Deploying to #{@environment}..." # Use TTY::Command for shell execution result = command(printer: :pretty).run("deploy.sh", @environment) if result.success? output.puts "Deployment complete!" else output.puts "Deployment failed: #{result.err}" end end end end end ``` -------------------------------- ### Implement Subcommand Dispatching in Ruby Source: https://github.com/piotrmurach/tty/blob/master/README.md This Ruby code defines the `Config` class, inheriting from Thor, to handle subcommand dispatching. It sets the namespace to `config` and implements the `set` method, which takes `name` and `value` arguments, handles help invocation, and executes the actual `Set` command logic. ```ruby # frozen_string_literal: true require 'thor' module App module Commands class Config < Thor namespace :config desc 'set NAME VALUE', 'Set configuration option' def set(name, value) if options[:help] invoke :help, ['set'] else require_relative 'config/set' App::Commands::Config::Set.new(name, value, options).execute end end end end end ``` -------------------------------- ### Colored Output with Pastel Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates how to add color and styling (like bold) to terminal output using the Pastel gem. This enhances readability and visual appeal. Requires the 'pastel' gem. ```ruby require "pastel" pastel = Pastel.new puts pastel.green("This is green text.") puts pastel.red.bold("This is bold red text.") ``` -------------------------------- ### Command Class Implementation in Ruby Source: https://context7.com/piotrmurach/tty/llms.txt Illustrates the structure for implementing command logic within the TTY framework. Commands inherit from the application's base Command class and implement an `execute` method. The base class facilitates integration with TTY components. ```ruby # Example command implementation (details not provided in source text) # Commands inherit from the application's base Command class # and implement an `execute` method. # The base class provides helper methods for TTY component integration. ``` -------------------------------- ### Configure Author in Teletype Project Source: https://github.com/piotrmurach/tty/blob/master/README.md The '--author' or '-a' flag allows you to specify the author's name, which will be injected into the generated project's documentation. ```bash $ teletype new app --author 'Piotr Murach' ``` -------------------------------- ### tty-prompt Helper Method (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Shows the `prompt` helper method available within `lib/app/command.rb`. This method facilitates the creation of interactive prompts for gathering user input, leveraging the `tty-prompt` gem. ```ruby # The interactive prompt # # @see http://www.rubydoc.info/gems/tty-prompt # # @api public def prompt(**options) require 'tty-prompt' TTY::Prompt.new(options) end ``` -------------------------------- ### Execute Subcommand from Terminal (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md This command shows how to execute the implemented `config set` subcommand from the terminal. It uses `bundle exec app` followed by the command and subcommand names, along with their arguments (`debug`, `true`). ```bash bundle exec app config set debug true ``` -------------------------------- ### Add Commands to Application with teletype add Source: https://context7.com/piotrmurach/tty/llms.txt Generates new commands, including their class structure, CLI integration, and test files. It supports both top-level and nested subcommands, ensuring proper file organization. Options allow for descriptions, arguments, and forcing overwrites. ```bash # Add a simple command cd myapp teletype add config # Creates: # lib/myapp/commands/config.rb # lib/myapp/templates/config/ # spec/integration/config_spec.rb # spec/unit/config_spec.rb # Add command with arguments and description teletype add deploy \ --desc "Deploy the application" \ --args environment "version = nil" # Add command with variadic arguments teletype add install --args "*packages" # Add a subcommand teletype add config set \ --desc "Set configuration option" \ --args name value # Creates: # lib/myapp/commands/config.rb # Namespace class # lib/myapp/commands/config/set.rb # Subcommand implementation # lib/myapp/templates/config/set/ # spec/integration/config/set_spec.rb # spec/unit/config/set_spec.rb # Force overwrite existing command teletype add config --force ``` -------------------------------- ### Specify License for Teletype Project Source: https://github.com/piotrmurach/tty/blob/master/README.md The '--license' or '-l' flag allows you to choose from a list of popular open-source licenses (e.g., 'mit', 'bsd3', 'gplv3'). The 'mit' license is used by default. ```ruby $ teletype new app --license bsd3 ``` -------------------------------- ### Add Command Description with `desc` (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md The `desc` method is used to provide a short description for a command, including its arguments. This description appears when the command list is displayed. It takes two arguments: the command signature and the description text. ```ruby module App class CLI < Thor desc 'config [FILE]', 'Set and get configuration options' def config(file = nil) ... end end end ``` -------------------------------- ### Generate Basic Command Structure with tty Source: https://github.com/piotrmurach/tty/blob/master/README.md Shows the file structure created when adding a new command, 'config', using `teletype add config`. It highlights the generated files within the `lib` folder, including the CLI entry point and the command's implementation file. ```shell ▾ app/ ├── ▾ commands/ │ └── config.rb ├── ▾ templates/ │ └── ▸ config/ ├── cli.rb ├── command.rb └── version.rb ``` -------------------------------- ### Bordered Boxes with TTY::Box Source: https://context7.com/piotrmurach/tty/llms.txt Shows how to create visually distinct bordered boxes in the terminal using TTY::Box. This can be used for highlighting important information or creating UI elements. Requires the 'tty-box' gem. ```ruby require "tty-box" box = TTY::Box.frame("This is a bordered box.", padding: 1, border: :thick) puts box ``` -------------------------------- ### Base Command Class with TTY Helpers Source: https://context7.com/piotrmurach/tty/llms.txt A base Ruby class providing helper methods for common TTY functionalities like command execution, interactive prompting, file generation, and progress bar creation. It ensures TTY components are required and instantiated when needed. ```ruby # Base command class with TTY helpers module Myapp class Command def execute(*) raise NotImplementedError, "#{self.class}##{__method__} must be implemented" end # External command runner def command(**options) require "tty-command" TTY::Command.new(options) end # Interactive prompt def prompt(**options) require "tty-prompt" TTY::Prompt.new(options) end # File manipulation def generator require "tty-file" TTY::File end # Progress bar def progress_bar(**options) require "tty-progressbar" TTY::ProgressBar.new("[:bar]", options) end end end ``` -------------------------------- ### Indeterminate Progress with TTY::Spinner Source: https://context7.com/piotrmurach/tty/llms.txt Demonstrates TTY::Spinner for indicating that a process is running without a known duration. It provides visual feedback using various predefined formats. Requires the 'tty-spinner' gem. ```ruby require "tty-spinner" spinner = TTY::Spinner.new("[:spinner] Loading...", format: :pulse_2) spinner.auto_spin sleep(2) spinner.stop("Done!") ``` -------------------------------- ### Process Templates with TTY::Templater Source: https://context7.com/piotrmurach/tty/llms.txt Illustrates using TTY::Templater for ERB template processing to generate files. It covers initializing the templater, adding template mappings, creating context data, and generating files with specified options. ```ruby require "tty/templater" require "ostruct" # Initialize with source and target paths templater = TTY::Templater.new("add", "lib/myapp") # Add template mappings templater.add_mapping("command.rb.tt", "commands/deploy.rb") templater.add_mapping("spec/unit/command_spec.rb.tt", "../../spec/unit/deploy_spec.rb") # Add empty directory with .gitkeep templater.add_empty_directory_mapping("templates/deploy") # Create template context context = OpenStruct.new context[:app_name] = "myapp" context[:cmd_name_constantinized] = "Deploy" context[:app_constantinized_parts] = ["Myapp"] context[:cmd_options] = ["environment", "options"] # Generate files from templates file_options = { force: false, color: true } templater.generate(context, file_options) ``` -------------------------------- ### tty-command Helper Method (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Presents the `command` helper method found in `lib/app/command.rb`. This method allows for running external system commands, utilizing the `tty-command` gem for enhanced control and output handling. ```ruby # The external commands runner # # @see http://www.rubydoc.info/gems/tty-command # # @api public def command(**options) require 'tty-command' TTY::Command.new(options) end ``` -------------------------------- ### Add New Command to CLI App Source: https://github.com/piotrmurach/tty/blob/master/README.md This command-line snippet shows how to add a new command, such as 'config', to an existing TTY-generated application. This helps in modularizing your CLI's functionality. ```bash $ cd app $ teletype add config ``` -------------------------------- ### Add a Subcommand using Teletype CLI Source: https://github.com/piotrmurach/tty/blob/master/README.md This command demonstrates how to use the `teletype add` command to create a new subcommand. It specifies the parent command (`config`), the subcommand name (`set`), a description, and its positional arguments (`name`, `value`). ```bash $ teletype add config set --desc 'Set configuration option' --args name value ``` -------------------------------- ### Define Command with Multiple Required Arguments (Bash & Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Shows how to generate a command (`set`) with multiple required arguments (`name`, `value`) by using the `--args` flag with space-delimited variable names. The corresponding Ruby code reflects the method signature with these arguments. ```bash $ teletype add set --args name value ``` ```ruby module App class CLI < Thor desc 'set NAME VALUE', 'Set configuration option' def set(name, value) ... end end end ``` -------------------------------- ### Define Command with Single Required Argument (Bash & Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Demonstrates how to define a command (`config`) that accepts a single required argument (`file`) using the `--args` flag. The generated Ruby code shows the `config` method signature updated to accept the `file` argument. ```bash $ teletype add config --args file ``` ```ruby module App class CLI < Thor desc 'config FILE', 'Set and get configuration options' def config(file) ... end end end ``` -------------------------------- ### Define Boolean Option with Aliases for Command (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Shows how to define a boolean option with aliases for a command using `method_option`. This is useful for flags that toggle a feature, like opening an editor for a configuration file. ```ruby module App class CLI < Thor desc 'config []', 'Set and get configuration options' method_option :edit, type: :boolean, aliases: ['-e'], desc: "Opens an editor to modify the specified config file." def config(*) ... end end end ``` -------------------------------- ### Progress Indication with TTY::ProgressBar Source: https://context7.com/piotrmurach/tty/llms.txt Illustrates the use of TTY::ProgressBar to display progress for long-running tasks. It allows for setting a total number of steps and advancing the bar accordingly. Requires the 'tty-progressbar' gem. ```ruby require "tty-progressbar" bar = TTY::ProgressBar.new(:total => 100) 100.times do bar.advance sleep(0.01) end ``` -------------------------------- ### Force Overwrite Command with tty Source: https://github.com/piotrmurach/tty/blob/master/README.md Demonstrates how to use the `--force` flag with the `teletype add config` command to overwrite an existing command. This is useful when updating or replacing previously implemented commands. ```bash $ teletype add config --force ``` -------------------------------- ### Define Array Option for Command (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Demonstrates how to define an option of type :array for a specific command using `method_option`. This allows the option to accept multiple values, such as a key-value pair for configuration. ```ruby module App class CLI < Thor desc 'config []', 'Set and get configuration options' method_option :add, type: :array, banner: "name value", desc: "Adds a new line the config file. " def config(*) ... end end end ``` -------------------------------- ### Define Command with Optional Argument (Bash & Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Explains how to define an optional argument (`file`) for a command (`config`) by enclosing the argument definition in parentheses within the `--args` flag. The generated Ruby code shows the argument with a default `nil` value. ```bash $ teletype add config --args 'file = nil' ``` ```ruby module App class CLI < Thor desc 'config [FILE]', 'Set and get configuration options' def config(file = nil) ... end end end ``` -------------------------------- ### Define Global Class Option (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md Explains how to define a global option or flag that applies to all commands within a Thor class using `class_option`. This is useful for settings like a debug mode that affects the entire application. ```ruby module App class CLI < Thor class_option :debug, type: :boolean, default: false, desc: 'Run in debug mode' ... end end ``` -------------------------------- ### Add Description to Teletype Command Source: https://github.com/piotrmurach/tty/blob/master/README.md Use the '--desc' flag to provide a custom description for a newly generated command. This improves context and usability when commands are listed or help is requested. ```bash $ teletype add config --desc 'Set and get configuration options' ``` -------------------------------- ### TTY CLI Global Options Source: https://context7.com/piotrmurach/tty/llms.txt Global options for the TTY CLI control output formatting and execution behavior across all commands. These include options for disabling color, performing dry runs, enabling debug mode, and displaying help or version information. ```bash # Disable colored output teletype new myapp --no-color # Dry run - show what would happen without making changes teletype new myapp --dry-run teletype add config -r # Debug mode - verbose output teletype new myapp --debug # Display help teletype --help teletype new --help teletype add --help # Display version teletype --version teletype -v # Command-specific help teletype help new teletype help add # new command options teletype new myapp \ --author "Name1" --author "Name2" \ --license mit \ --test rspec \ --ext \ --coc \ --force # add command options teletype add deploy \ --args "env" "version = nil" "*flags" \ --desc "Deploy application" \ --test rspec \ --force ``` -------------------------------- ### Register Subcommand Namespace in Thor CLI (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md This Ruby code snippet shows how to register a subcommand namespace (`config`) within a Thor-based CLI application. It uses the `register` method to associate the `App::Commands::Config` class with the `config` command, providing its usage and description. ```ruby module App class CLI < Thor require_relative 'commands/config' register App::Commands::Config, 'config', 'config [SUBCOMMAND]', 'Set configuration option' end end ``` -------------------------------- ### Add New Command to Teletype Application Source: https://github.com/piotrmurach/tty/blob/master/README.md The 'teletype add [command-name]' command creates a new command file within the application's 'app/commands/' directory. This allows for modular command creation. ```bash $ teletype add config $ teletype add create ``` -------------------------------- ### Base Command Class with TTY::Cmd Source: https://context7.com/piotrmurach/tty/llms.txt The TTY::Cmd class serves as a base for TTY's internal commands, offering utilities for path manipulation, command execution, and string conversions. It simplifies the creation of new commands by providing common functionalities. ```ruby require "tty/cmd" class MyCommand < TTY::Cmd def execute(input: $stdin, output: $stdout) # Run external commands result = run("bundle", "install") # File generation generator.create_file("config.yml", "key: value") generator.copy_file("template.rb", "output.rb") # Check executable existence if exec_exist?("docker") output.puts "Docker is available" end # String conversion constantinize("my-app") # => "MyApp" constantinize("my_app_name") # => "MyAppName" snake_case("MyApp") # => "my_app" # Path utilities command_path # => Inferred from class name root_path # => Current working directory end end # PathHelpers module methods include TTY::PathHelpers root_path # => Pathname.pwd name_from_path("/foo/bar/myapp") # => "myapp" relative_path_from(root, "/foo") # => Pathname("foo") within_root_path do # Operations executed in root directory end ``` -------------------------------- ### Specify Arguments for Teletype Command Source: https://github.com/piotrmurach/tty/blob/master/README.md The '--args' flag allows you to define arguments for a new command. You can specify required arguments (name), optional arguments ('name = nil'), or variadic arguments (*names). ```bash $ teletype add config --args name $ teletype add config --args "name = nil" $ teletype add config --args *names ``` -------------------------------- ### Manage Gemspec Files with TTY::Gemspec Source: https://context7.com/piotrmurach/tty/llms.txt The TTY::Gemspec class allows reading, modifying, and formatting gemspec files. It helps in managing TTY component dependencies by parsing existing gemspec content and providing methods to inject new dependencies. ```ruby require "tty/gemspec" gemspec = TTY::Gemspec.new # Read existing gemspec gemspec.read("myapp.gemspec") # Access parsed information gemspec.var_name # => "spec" gemspec.pre_var_indent # => 2 gemspec.post_var_indent # => 6 # Format dependencies for injection formatted = gemspec.format_gem_dependencies puts formatted # Modify and write back gemspec.content.gsub!(/TODO: Write description/, "My CLI application") gemspec.write("myapp.gemspec") ``` -------------------------------- ### Add Long Command Description with `long_desc` (Ruby) Source: https://github.com/piotrmurach/tty/blob/master/README.md The `long_desc` method allows for a more detailed, multi-line description of a command. This is useful for explaining complex functionality, usage nuances, and error handling. It is typically used in conjunction with `desc`. ```ruby module App class CLI < Thor desc 'config [FILE]', 'Set and get configuration options' long_desc <<-DESC You can query/set/replace/unset options with this command. The name is an optional key separated by a dot, and the value will be escaped. This command will fail with non-zero status upon error. DESC def config(file = nil) ... end end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.