tlopo-cli
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
- Fork it ( https://github.com/[my-github-username]/kubeclient/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Test your changes with
rake test rubocop
, add new tests if needed. - If you added a new functionality, add it to README
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - 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