Giter Site home page Giter Site logo

easycli's Introduction

EasyCLI

Build Status Inline docs

A basic command-line parser for Ruby.

Usage

Creating Commands

To create a command, use the Command class, accessible through the EasyCLI module:

command = EasyCLI::Command.new('command')

The new method also takes a block parameter, which takes a Setup object as its parameter:

command = EasyCLI::Command.new('command') do |setup|
  # Place command action here
end

If your command requires arguments, you can set minimum and maximum argument numbers by using the following:

# The minimum number of arguments required
command.required_args = 1
# The maximum number of arguments allowed
command.max_args = 3

To register the command, call the EasyCLI module's register_command method:

EasyCLI.register_command(command)

Creating Options (Flags)

To create an option (also known as flags), use the Option class, accessible through the EasyCLI module:

option = EasyCLI::Option.new('option')

The new method also takes a block parameter, which takes a Setup object as its parameter:

option = EasyCLI::Option.new('option') do |setup|
  # Place option action here
end

To register the option, call the EasyCLI module's register_option method:

EasyCLI.register_option(option)

Using Setup And Combining Options and Commands

The Setup object passed to blocks for commands and actions allows them to store parameters. For instance, an option might set parameter test_param to true, and when the command is finally executed, it will see that test_param is true, and act accordingly:

option = EasyCLI::Option.new('option') do |setup|
  setup.add_param('test_param', true)
end
command = EasyCLI::Command.new('command') do |setup|
  if setup.param?('test_param') && setup.param('test_param') == true
    # Do something
  end
end

EasyCLI.register_option(option)
EasyCLI.register_command(command)

Starting the Engine

To begin parsing the input passed to the command-line, call the EasyCLI module's process method:

EasyCLI.process

Aliases For Commands and Options

Commands

Commands can also be assigned aliases. A command alias works similarly to the command itself. For instance, take the following command:

command = EasyCLI::Command.new('command') do |setup|
  # ...
end

Calling this command is as simple as this:

$ <PROGRAM> command

However, you can also add an alias to your command to create a shortcut version by adding the following to your command:

command = EasyCLI::Command.new('command') do |setup|
  # ...
end
command.alias('c')

Now, you can also call this command like this:

$ <PROGRAM> c

Options

Option aliases are being reworked currently, to allow switches to be added in the future, so they are not included here.

The help Command

By default, EasyCLI contains a help command, that can take one or no arguments. It prints the usage and description information about one or all commands (depending on the arguments passed to it). There is also a default --help option, which performs the same action on the command it is applied to. To fill in this information, the Command class provides several methods and variables.

To start, you can set a description for your command:

command.description = 'Place description here'

Next, you can add argument documentation with the following:

command.add_arg_doc('argument_name', 'Place description here')

You can also add option/flag documentation and describe their specific effect on the current command:

command.add_option_doc(option, 'Place description here')

Copyright

Copyright (c) 2016 Christopher Lutz. See LICENSE.txt for more details.

easycli's People

Contributors

chrisblutz avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.