### Example Command Line Execution and Output (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Demonstrates the execution of a command defined with arguments and options, showing the resulting output. Also illustrates the automatic generation of help messages. ```bash # Command line input: app foo one --baz 11 12 # Output: one [11, 12] # Help message: app foo --help # Output: Usage: app foo [OPTIONS] BAR Run foo command Arguments: BAR The bar argument Options: -b, --baz The baz option -h, --help Print usage ``` -------------------------------- ### Command Definition with Arguments and Options (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Provides an example of defining a command with arguments and options using the 'run' helper block, leveraging tty-option for parsing. This includes defining required arguments and options with specific arity and type conversion. ```ruby on "foo" do run do program "app" command "foo" desc "Run foo command" argument :bar do required desc "The bar argument" end option :baz do short "-b" long "--baz list" arity one_or_more convert :int_list desc "The baz option" end def call puts params["bar"] puts params["baz"] end end end ``` -------------------------------- ### Install TTY::Runner Gem Source: https://context7.com/piotrmurach/tty-runner/llms.txt Instructions for installing the TTY::Runner gem using Bundler or directly with the `gem install` command. ```ruby # Gemfile gem "tty-runner" ``` ```bash bundle install # or gem install tty-runner ``` -------------------------------- ### Run TTY::Runner Application (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md This snippet shows the simplest way to execute a TTY::Runner application. After defining the application class (e.g., `App`), this line invokes the `run` method to start the application and process command-line arguments. ```ruby App.run ``` -------------------------------- ### Define Commands with 'on' and ':run' (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Demonstrates how to use the 'on' method to define commands and associate them with actions. Supported values for ':run' include procs, command objects, or string representations of commands and their actions. ```ruby on "cmd", run: -> { } # a proc to call on "cmd", run: Command # a Command object to instantiate and call on "cmd", run: "command" # invokes 'call' method by default on "cmd", run: "command#action" # specified custom 'action' method on "cmd", run: "command", action: "action" ``` -------------------------------- ### Using ':run' Keyword with 'on' (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Details the various ways to specify a command using the ':run' keyword with the 'on' method. This includes procs, command objects, and string representations. ```ruby on "cmd", run: -> { ... } # a proc object on "cmd", run: ->(argv) { ... } # a proc object with optional unparsed arguments on "cmd", run: FooCommand # a FooCommand object with 'call' method on "cmd", run: "foo_command" # expands name to a FooCommand object on "cmd", run: "foo_command#action" # expands name to a FooCommand object with 'action' method ``` -------------------------------- ### Nesting Commands with 'on' (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Illustrates how to create nested command structures using the 'on' method. This allows for hierarchical command organization, with no limit on nesting depth. ```ruby on "foo", run: FooCommand do on "bar", run: "bar_command" do on "baz" do run "baz_command#execute" end end end ``` -------------------------------- ### Defining Commands with 'run' Helper Block (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Explains how to use the 'run' helper within a block to define commands on-the-fly. This method allows for more dynamic command creation, including defining the 'call' method within the block. ```ruby on "foo" do run do def call(argv) ... end end end ``` -------------------------------- ### Define Basic Command Structure with TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt Demonstrates how to define a simple command-line application structure using `TTY::Runner.commands`. It includes a root command and a subcommand 'greet' with basic argument handling. ```ruby require "tty-runner" class MyCLI < TTY::Runner commands do # Define root command behavior (when no arguments given) run do def call(argv) puts "Welcome to MyCLI! Use --help for available commands." end end # Define subcommands on "greet", "Say hello to someone" do run do def call(argv) name = argv.first || "World" puts "Hello, #{name}!" end end end end end MyCLI.run ``` -------------------------------- ### Define Application Commands with TTY::Runner (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md This snippet demonstrates how to define a command-line application using TTY::Runner. It shows how to declare commands, subcommands, aliases, and how to associate actions with them using blocks, procs, or by referencing other command classes. It also illustrates how to mount other TTY::Runner classes to create nested command structures. ```ruby require "tty-runner" class App < TTY::Runner commands do run do def call(argv) puts "root" end end on "config" do on "add", "Add a new entry", run: "add_command" on "remove", aliases: %w[rm], run: -> { puts "removing from config..." } on "get" do run "get_command#execute" end on "edit" do run do def call(argv) puts "editing with #{argv}" end end end end on "tag" do mount TagCommands end end end class TagCommands < TTY::Runner commands do on "create" do run -> { puts "tag creating..." } end on "delete" do run -> { puts "tag deleting..." } end end end App.run ``` -------------------------------- ### Mounting Subcommands into a Runner Application (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Shows how to group commands into separate runner applications and then mount them into a main application runner. This is useful for organizing complex command structures. ```ruby # foo_subcommands.rb class FooSubcommands < TTY::Runner commands do on "bar", run: -> { puts "run bar" } on "baz", run: -> { puts "run baz" } end end # main_app.rb require_relative "foo_subcommands" class App < TTY::Runner commands do on "foo" do mount FooSubcommands end end end ``` -------------------------------- ### Define Commands within a Block (Ruby) Source: https://github.com/piotrmurach/tty-runner/blob/master/README.md Shows how to define commands using the 'on' method and specify the 'run' command within a block. This allows for more complex command definitions. ```ruby on "cmd" do run "command#action" end ``` -------------------------------- ### Build Git-like CLI with TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt This snippet demonstrates building a `GitLike` CLI application using `TTY::Runner`. It defines top-level commands like `init`, `commit`, `branch`, `tag`, and `remote`, showcasing nested commands and mounting other command classes. ```ruby class GitLike < TTY::Runner program "gitlike" commands do run do def call(argv) puts "GitLike - A git-like CLI example" puts "Run 'gitlike --help' for usage" end end on "init" do run do option :bare do long "--bare" desc "Create a bare repository" end def call type = params["bare"] ? "bare " : "" puts "Initialized #{type}repository in .gitlike/" end end end on "commit" do desc "Record changes" run do program "gitlike" command "commit" option :message do short "-m" long "--message TEXT" required desc "Commit message" end option :all do short "-a" long "--all" desc "Stage all modified files" end def call staged = params["all"] ? " (all files staged)" : "" puts "Committed: #{params['message']}#{staged}" end end end on "branch", "Manage branches" do on "list", run: -> { puts "Branches: main, develop, feature/login" } on "create", run: ->(argv) { puts "Created branch: #{argv.first}" } on "delete", aliases: %w[rm], run: ->(argv) { puts "Deleted branch: #{argv.first}" } end on "tag", "Manage tags" do mount TagCommands end on "remote", "Manage remotes" do on "add", run: ->(argv) { puts "Added remote '#{argv[0]}' -> #{argv[1]}" } on "remove", aliases: %w[rm], run: ->(argv) { puts "Removed remote: #{argv.first}" } on "list", run: -> { puts "Remotes: origin (https://github.com/user/repo)" } end end end GitLike.run ``` -------------------------------- ### Define Command Handlers with TTY::Option Integration Source: https://context7.com/piotrmurach/tty-runner/llms.txt Shows how to define command handlers using `run` with inline blocks, integrating TTY::Option for argument parsing, options, and parameter validation. It also demonstrates string references with custom actions and direct procs. ```ruby require "tty-runner" class App < TTY::Runner commands do # Inline block with TTY::Option integration on "greet" do run do program "app" command "greet" desc "Greet someone with optional enthusiasm" argument :name do required desc "The name to greet" end option :excited do short "-e" long "--excited" desc "Add exclamation marks" end option :count do short "-c" long "--count NUM" convert :int default 1 desc "Number of times to greet" end def call greeting = "Hello, #{params['name']}" greeting += "!" if params["excited"] params["count"].times { puts greeting } end end end # String reference with custom action on "process", run: "processor#execute" # Direct proc on "version", run: -> { puts "v1.0.0" } end end App.run ``` -------------------------------- ### Define Nested Commands and Aliases with TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt Illustrates defining nested commands under a 'config' namespace, using class objects, string references, procs, and command aliases for 'remove'. It also shows inline block definition for 'edit'. ```ruby require "tty-runner" module Config class AddCommand def call puts "Adding to config..." end end class GetCommand def execute puts "Getting from config..." end end end class App < TTY::Runner commands do on "config", "Manage configuration" do # Using a Class object directly on "add", "Add config entry", run: Config::AddCommand # Using string reference (auto-converts to Config::GetCommand) on "get", "Get config value", run: "get_command", action: :execute # Using proc on "list", run: -> { puts "Listing all config..." } # Using command aliases on "remove", "Remove entry", aliases: %w[rm delete], run: -> { puts "Removing..." } # Using inline block definition on "edit" do desc "Edit configuration" run do def call(argv) puts "Editing config with args: #{argv.inspect}" end end end end end end App.run ``` -------------------------------- ### Set Application Name with program - TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt The `program` class method allows you to set the application's name, which is used in help messages and usage output. By default, it uses the script's filename. This is useful for branding and clarity in CLI tools. ```ruby require "tty-runner" class App < TTY::Runner program "mycli" commands do on "deploy" do run do program "mycli" command "deploy" desc "Deploy the application" argument :environment do required desc "Target environment" end def call puts "Deploying to #{params['environment']}..." end end end end end App.run ``` -------------------------------- ### Compose Runners with mount - TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt The `mount` method allows composing complex CLI applications by mounting other TTY::Runner subclasses. This promotes modularity and code reuse. Each mounted runner must be a subclass of TTY::Runner. ```ruby require "tty-runner" # Separate runner for user-related commands class UserCommands < TTY::Runner commands do on "list", "List all users", run: -> { puts "Users: alice, bob, charlie" } on "create", "Create a user" do run do argument :username do required desc "Username for the new user" end def call puts "Created user: #{params['username']}" end end end on "delete", "Delete a user", run: ->(argv) { puts "Deleted user: #{argv.first}" } end end # Separate runner for config commands class ConfigCommands < TTY::Runner commands do on "show", run: -> { puts "Current config: {debug: false}" } on "set", run: ->(argv) { puts "Setting #{argv[0]} = #{argv[1]}" } on "reset", run: -> { puts "Config reset to defaults" } end end # Main application composing the subcommands class App < TTY::Runner commands do on "user", "Manage users" do mount UserCommands end on "config", "Manage configuration" do mount ConfigCommands end on "version", run: -> { puts "MyApp v2.1.0" } end end App.run ``` -------------------------------- ### Auto-load Commands from Directory with commands_dir - TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt The `commands_dir` class method enables automatic loading of command classes from a specified directory. Files are loaded on-demand, and snake_case filenames are converted to CamelCase class names. This simplifies command management in larger applications. ```ruby # File structure: # my_app/ # app.rb # commands/ # config/ # add_command.rb # defines Config::AddCommand # get_command.rb # defines Config::GetCommand # init_command.rb # defines InitCommand # commands/init_command.rb class InitCommand def call puts "Initializing project..." end end # commands/config/add_command.rb module Config class AddCommand def call(argv) puts "Adding config: #{argv.first}" end end end # app.rb require "tty-runner" class App < TTY::Runner # Register commands directory with optional namespace commands_dir "commands" # Or with namespace: commands_dir "commands", namespace: MyApp::Commands commands do # String references auto-load from commands directory on "init", "Initialize project", run: "init_command" on "config" do on "add", run: "add_command" # Loads Config::AddCommand on "get", run: "get_command" # Loads Config::GetCommand end end end App.run ``` -------------------------------- ### Define Tag Management Commands with TTY::Runner Source: https://context7.com/piotrmurach/tty-runner/llms.txt This snippet defines a `TagCommands` class inheriting from `TTY::Runner` to manage tag-related CLI commands. It includes commands for listing, creating, and deleting tags, with support for arguments and options like tag names and messages. ```ruby class TagCommands < TTY::Runner commands do on "list", "List all tags", run: -> { puts "Tags: v1.0, v1.1, v2.0" } on "create" do run do program "gitlike" command "tag create" desc "Create a new tag" argument :name do required desc "Tag name" end option :message do short "-m" long "--message TEXT" desc "Tag message" end def call msg = params["message"] ? " with message: #{params['message']}" : "" puts "Created tag '#{params['name']}'#{msg}" end end end on "delete", run: ->(argv) { puts "Deleted tag: #{argv.first}" } end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.