Project

cli-parser

0.0
No commit activity in last 3 years
No release in over 3 years
A command-line parser. Extracts arguments, switches and options.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

cli-parser

Description

The cli-parser parse command-line arguments and returns the information. It respects flags/switches, arguments and (quoted) options.

Installation

gem install cli-parser

Usage

require 'cli-parser'

arguments, options = CliParser.parse(flags, opts, cmd)

Input

  • flags: An array of flags, like -w -i -s --debug. These are just switches and aren't followed with a value.
  • opts: An array of options with arguments, like -t --style. These options should be followed with a value.
  • cmd: A optional string of command-line arguments, like 'arg1 -f -a test arg2'. If none is given ARGV is used.

Output

  • arguments: An array of arguments
  • options: A dict containing all flags and options

Examples

require 'cli-parser'

cli_flags = %w(-i -c -t)
cli_options = %w(-s, -a)
arguments, options = CliParse.parse(cli_flags, cli_options)

By default it parses the command-line options from ARGV, but the parser also accepts an optional third parameter, which contain all arguments as a string.

require 'cli-parser'

cmd = '-i arg1 -c -s Test -a "Hello World" arg2'
arguments, options = CliParser.parse(%w(-i -c -t), %w(-s -a), cmd)

puts arguments # ["arg1", "arg2"]
puts options # { '-i' => true, '-c' => true, '-s' => 'Test', 'a' => 'Hello World' }