Project

tlopo-cli

0.0
No commit activity in last 3 years
No release in over 3 years
A library to speed up CLI apps development
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
= 0.8.19
~> 0.13.0
~> 5.0
~> 10.0
~> 0.52.1
= 0.12.0
 Project Readme

tlopo-cli

Gem Version Build Status Code Climate Dependency Status Coverage Status

A Library to speed up CLI apps development

Installation

Add this line to your application's Gemfile:

gem 'tlopo-cli'

And then execute:

bundle

Or install it yourself as:

gem install tlopo-cli

Usage

Simple usage:

require 'tlopo/cli'

class Command
  def self.run(opts)
    puts opts
  end
end

cfg = {
  'name' =>  'command',
  'banner' => "My ClI\n    Run my-cli with ARGS\nOPTIONS:\n",
  'class' => 'Command',
  'switches' => [{
    'name' => 'filename',
    'short' => '-f',
    'long' => '--filename <Filename>',
    'desc' => 'Sets filename'
  }]
}

Tlopo::Cli.new(config: cfg).run 

In action:

$ ruby /tmp/my-cli.rb --help
My ClI
    Run my-cli with ARGS
OPTIONS:
    -f, --filename <Filename>        Sets filename
$ ruby /tmp/my-cli.rb  -f /etc/hosts
{"filename"=>"/etc/hosts"}

You can have as many subcommands as you want, options will be parsed.

require 'tlopo/cli'

class Command
  def self.run(opts)
    puts opts
  end
end

class SubCommand1
  def self.run(opts)
    puts opts
  end
end

class SubCommand2
  def self.run(opts)
    puts opts
  end
end

cfg = {
  :globals => true, # This will include the options for each 'parent' command
  :usage => true, # This will add _usage in opts commands/subcommands can print it  if needed
  'name' =>  'command',
  'banner' => "command\nOPTIONS:\n",
  'class' => 'Command',
  'switches' => [{ 'name' => 'arg1', 'short' => '-a', 'long' => '--arg1 <arg1>', 'desc' => 'Sets arg1'}],
  'subcommands' => [
    { 
      'name' =>  'subcommand1',
      'banner' => "Subcommand1\nOPTIONS:\n",
      'class' => 'SubCommand1',
      'switches' => [{ 'name' => 'arg1', 'short' => '-a', 'long' => '--arg1 <arg1>', 'desc' => 'Sets subcommand1 arg1'} ],
      'subcommands' => [
        { 
          'name' =>  'subcommand2',
          'banner' => "Subcommand2\nOPTIONS:\n",
          'class' => 'SubCommand2',
          'switches' => [{ 'name' => 'arg1', 'short' => '-a', 'long' => '--arg1 <arg1>', 'desc' => 'Sets subcommand2 arg1'}]
        }
      ]
    }
  ]
}

Tlopo::Cli.new(config: cfg).run 

In action:

$ ruby /tmp/my-cli.rb -a 1  subcommand1 -a 2  subcommand2 -a 3
{"arg1"=>"3", "_globals"=>{"command"=>{"class"=>"Command", "arg1"=>"1"}, "command::subcommand1"=>{"class"=>"SubCommand1", "arg1"=>"2"}}}

The configuration can also be a yaml or json file:

Tlopo::Cli.new(config_file: './cli-config.yml')

Contributing

  1. Fork it ( https://github.com/[my-github-username]/kubeclient/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Test your changes with rake test rubocop, add new tests if needed.
  4. If you added a new functionality, add it to README
  5. Commit your changes (git commit -am 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create a new Pull Request

Tests

This library is tested with Minitest. Please run all tests before submitting a Pull Request, and add new tests for new functionality.

Running tests:

rake test