argos 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +22 -0
- data/README.md +97 -0
- data/Rakefile +64 -0
- data/example/args.rb +39 -0
- data/lib/argos.rb +98 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: db10ec4b4ba706521829ff94256f6aeeb535583d
|
4
|
+
data.tar.gz: 9a05741bec97fcbe78f1a895ece95f82722c7cb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2de75d97e7d47de815118087efd18b254c6e763be3825a7beb70aa3c4521cca54ff0334911bf3b14955f6152699f75195c23197f2fb1fb0af0807019b5a041f6
|
7
|
+
data.tar.gz: a3ae1af737afd333e7d8a8f14f15cce213ee94f9d41e8c573d748fe78bad76df77e74c008881852f69a1c6ed76877a6285b523893120556ceccc434756fe5a98
|
data/COPYING
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2006-2014, Joel VanderWerf, vjoel@users.sourceforge.net
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
14
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
15
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
16
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
17
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
18
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
19
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
20
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
21
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
22
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
argos
|
2
|
+
====
|
3
|
+
|
4
|
+
A slim command-line parser that does one thing well: turn an array of
|
5
|
+
strings, such as ARGV, into a hash of recognized options and their
|
6
|
+
arguments, leaving unrecognized strings in the original array.
|
7
|
+
|
8
|
+
Argos was Odysseus' faithful dog, who was good at recognizing ;)
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Install as gem:
|
14
|
+
|
15
|
+
gem install 'argos'
|
16
|
+
|
17
|
+
or simply copy argos.rb into your project's lib dir.
|
18
|
+
|
19
|
+
Synopsis
|
20
|
+
--------
|
21
|
+
|
22
|
+
require 'argos'
|
23
|
+
|
24
|
+
optdef = {
|
25
|
+
"v" => true,
|
26
|
+
"n" => proc {|arg| Integer(arg)}
|
27
|
+
}
|
28
|
+
|
29
|
+
argv = %w{-v -n10 filename}
|
30
|
+
opts = Argos.parse_options(argv, optdef)
|
31
|
+
p opts # ==> {"v"=>true, "n"=>10}
|
32
|
+
p argv # ==> ["filename"]
|
33
|
+
|
34
|
+
See also [example dir](example/).
|
35
|
+
|
36
|
+
Features
|
37
|
+
--------
|
38
|
+
|
39
|
+
- Operates on ARGV or any given array of strings.
|
40
|
+
|
41
|
+
- Output is a hash of {option => value, ...}.
|
42
|
+
|
43
|
+
- You can merge this hash on top of a hash of defaults if you want.
|
44
|
+
|
45
|
+
- Supports both long ("--foo") and short ("-f") options.
|
46
|
+
|
47
|
+
- A long option with an argument is --foo=bar or --foo bar.
|
48
|
+
|
49
|
+
- A short option with an argument is -fbar or -f bar.
|
50
|
+
|
51
|
+
- The options -x and --x are synonymous.
|
52
|
+
|
53
|
+
- Short options with no args can be combined as -xyz in place of -x -y -z.
|
54
|
+
|
55
|
+
- If -z takes an argument, then -xyz foo is same as -x -y -z foo.
|
56
|
+
|
57
|
+
- The string "--" terminates option parsing, leaving the rest untouched.
|
58
|
+
|
59
|
+
- The string "-" is not considered an option.
|
60
|
+
|
61
|
+
- ARGV (or other given array) is modified: it has all parsed options
|
62
|
+
and arguments removed, so you can use ARGF to treat the rest as input files.
|
63
|
+
|
64
|
+
- Unrecognized arguments are left in the argument array. You can catch them
|
65
|
+
with grep(/^-./), in case you want to pass them on to another program or
|
66
|
+
warn the user.
|
67
|
+
|
68
|
+
- Argument validation and conversion are in terms of an option definition
|
69
|
+
hash, which specifies which options are allowed, the number of arguments
|
70
|
+
for each (0 or 1), and how to generate the value from the argument, if any.
|
71
|
+
|
72
|
+
- Repetition of args ("-v -v", or "-vv") can be handled by closures. See
|
73
|
+
the example below.
|
74
|
+
|
75
|
+
- Everything is ducky. For example, handlers only need an #arity method
|
76
|
+
and a #[] method to be recognized as callable. Otherwise they are treated
|
77
|
+
as static objects.
|
78
|
+
|
79
|
+
|
80
|
+
Limitations
|
81
|
+
-----------
|
82
|
+
|
83
|
+
- A particular option takes either 0 args or 1 arg. There are no optional
|
84
|
+
arguments, in the sense of both "-x" and "-x3" being accepted.
|
85
|
+
|
86
|
+
- Options lose their ordering in the output hash (but they are parsed in
|
87
|
+
order and you can keep track using state in the handler closures).
|
88
|
+
|
89
|
+
- There is no usage/help output.
|
90
|
+
|
91
|
+
|
92
|
+
About
|
93
|
+
-----
|
94
|
+
|
95
|
+
Copyright (C) 2006-2014 Joel VanderWerf, mailto:vjoel@users.sourceforge.net.
|
96
|
+
|
97
|
+
License is BSD. See [COPYING](COPYING).
|
data/Rakefile
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
PRJ = "argos"
|
5
|
+
|
6
|
+
def version
|
7
|
+
@version ||= begin
|
8
|
+
require 'argos'
|
9
|
+
warn "Argos::VERSION not a string" unless Argos::VERSION.kind_of? String
|
10
|
+
Argos::VERSION
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def tag
|
15
|
+
@tag ||= "#{PRJ}-#{version}"
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Run tests"
|
19
|
+
Rake::TestTask.new :test do |t|
|
20
|
+
t.libs << "lib"
|
21
|
+
t.libs << "ext"
|
22
|
+
t.test_files = FileList["test/**/*.rb"]
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Commit, tag, and push repo; build and push gem"
|
26
|
+
task :release => "release:is_new_version" do
|
27
|
+
require 'tempfile'
|
28
|
+
|
29
|
+
sh "gem build #{PRJ}.gemspec"
|
30
|
+
|
31
|
+
file = Tempfile.new "template"
|
32
|
+
begin
|
33
|
+
file.puts "release #{version}"
|
34
|
+
file.close
|
35
|
+
sh "git commit --allow-empty -a -v -t #{file.path}"
|
36
|
+
ensure
|
37
|
+
file.close unless file.closed?
|
38
|
+
file.unlink
|
39
|
+
end
|
40
|
+
|
41
|
+
sh "git tag #{tag}"
|
42
|
+
sh "git push"
|
43
|
+
sh "git push --tags"
|
44
|
+
|
45
|
+
sh "gem push #{tag}.gem"
|
46
|
+
end
|
47
|
+
|
48
|
+
namespace :release do
|
49
|
+
desc "Diff to latest release"
|
50
|
+
task :diff do
|
51
|
+
latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
|
52
|
+
sh "git diff #{latest}"
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Log to latest release"
|
56
|
+
task :log do
|
57
|
+
latest = `git describe --abbrev=0 --tags --match '#{PRJ}-*'`.chomp
|
58
|
+
sh "git log #{latest}.."
|
59
|
+
end
|
60
|
+
|
61
|
+
task :is_new_version do
|
62
|
+
abort "#{tag} exists; update version!" unless `git tag -l #{tag}`.empty?
|
63
|
+
end
|
64
|
+
end
|
data/example/args.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'argos'
|
2
|
+
|
3
|
+
v = 0
|
4
|
+
defaults = {
|
5
|
+
"v" => v,
|
6
|
+
"port" => 4000,
|
7
|
+
"host" => "localhost"
|
8
|
+
}
|
9
|
+
|
10
|
+
optdef = {
|
11
|
+
"x" => true,
|
12
|
+
"y" => "y",
|
13
|
+
"z" => 3,
|
14
|
+
"v" => proc {v+=1}, # no argument, but call the proc to get the value
|
15
|
+
"port" => proc {|arg| Integer(arg)},
|
16
|
+
"n" => proc {|arg| Integer(arg)},
|
17
|
+
"t" => proc {|arg| Float(arg)},
|
18
|
+
"cmd" => proc {|arg| arg.split(",")}
|
19
|
+
}
|
20
|
+
|
21
|
+
ARGV.replace %w{
|
22
|
+
-xyzn5 somefile --port 5000 -t -1.23 -vv -v --unrecognized-option
|
23
|
+
--cmd=ls,-l otherfile -- --port
|
24
|
+
}
|
25
|
+
|
26
|
+
begin
|
27
|
+
cli_opts = Argos.parse_options(ARGV, optdef)
|
28
|
+
rescue Argos::OptionError => ex
|
29
|
+
$stderr.puts ex.message
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
|
33
|
+
opts = defaults.merge cli_opts
|
34
|
+
|
35
|
+
p opts
|
36
|
+
p ARGV
|
37
|
+
unless ARGV.empty?
|
38
|
+
puts "Some arg-looking strings were not handled:", *ARGV.grep(/^-./)
|
39
|
+
end
|
data/lib/argos.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module Argos
|
2
|
+
VERSION = "1.0"
|
3
|
+
|
4
|
+
module_function
|
5
|
+
|
6
|
+
# Raised (a) when an option that takes an argument occurs at the end of the
|
7
|
+
# argv list, with no argument following it, or (b) when a handler barfs.
|
8
|
+
class OptionError < ArgumentError; end
|
9
|
+
|
10
|
+
# Called when an option that takes an argument occurs at the end of the
|
11
|
+
# argv list, with no argument following it.
|
12
|
+
def argument_missing opt
|
13
|
+
raise OptionError, "#{opt}: no argument provided."
|
14
|
+
end
|
15
|
+
|
16
|
+
def handle opt, handler, *args # :nodoc
|
17
|
+
args.empty? ? handler[] : handler[args[0]]
|
18
|
+
rescue => ex
|
19
|
+
raise OptionError, "#{opt}: #{ex}"
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the hash of parsed options and argument values. The +argv+ array
|
23
|
+
# is modified: every recognized option and argument is deleted.
|
24
|
+
#
|
25
|
+
# The +optdef+ hash defines the options and their arguments.
|
26
|
+
#
|
27
|
+
# Each key is an option name (without "-" chars).
|
28
|
+
#
|
29
|
+
# The value for a key in +optdef+
|
30
|
+
# is used to generate the value for the same key in the options hash
|
31
|
+
# returned by this method.
|
32
|
+
#
|
33
|
+
# If the value has an #arity method and arity > 0, the value is considered to
|
34
|
+
# be a handler; it is called with the argument string to return the value
|
35
|
+
# associated with the option in the hash returned by the method.
|
36
|
+
#
|
37
|
+
# If the arity <= 0, the value is considered to be a handler for an option
|
38
|
+
# without arguments; it is called with no arguments to return the value of
|
39
|
+
# the option.
|
40
|
+
#
|
41
|
+
# If there is no arity method, the object itself is used as the value of
|
42
|
+
# the option.
|
43
|
+
#
|
44
|
+
# Only one kind of input will cause an exception (not counting exceptions
|
45
|
+
# raised by handler code or by bugs):
|
46
|
+
#
|
47
|
+
# - An option is found at the end of the list, and it requires an argument.
|
48
|
+
# This results in a call to #argument_missing, which by default raises
|
49
|
+
# OptionError.
|
50
|
+
#
|
51
|
+
def parse_options argv, optdef
|
52
|
+
orig = argv.dup; argv.clear
|
53
|
+
opts = {}
|
54
|
+
|
55
|
+
loop do
|
56
|
+
case (argstr=orig.shift)
|
57
|
+
when nil, "--"
|
58
|
+
argv.concat orig
|
59
|
+
break
|
60
|
+
|
61
|
+
when /^(--)([^=]+)=(.*)/, /^(-)([^-])(.+)/
|
62
|
+
short = ($1 == "-"); opt = $2; arg = $3
|
63
|
+
unless optdef.key?(opt)
|
64
|
+
argv << argstr
|
65
|
+
next
|
66
|
+
end
|
67
|
+
handler = optdef[opt]
|
68
|
+
arity = (handler.arity rescue nil)
|
69
|
+
opts[opt] =
|
70
|
+
case arity
|
71
|
+
when nil; orig.unshift("-#{arg}") if short; handler
|
72
|
+
when 0,-1; orig.unshift("-#{arg}") if short; handle(opt, handler)
|
73
|
+
else handle(opt, handler, arg)
|
74
|
+
end
|
75
|
+
|
76
|
+
when /^--(.+)/, /^-(.)$/
|
77
|
+
opt = $1
|
78
|
+
unless optdef.key?(opt)
|
79
|
+
argv << argstr
|
80
|
+
next
|
81
|
+
end
|
82
|
+
handler = optdef[opt]
|
83
|
+
arity = (handler.arity rescue nil)
|
84
|
+
opts[opt] =
|
85
|
+
case arity
|
86
|
+
when nil; handler
|
87
|
+
when 0,-1; handle(opt, handler)
|
88
|
+
else handle(opt, handler, orig.shift || argument_missing(opt))
|
89
|
+
end
|
90
|
+
|
91
|
+
else
|
92
|
+
argv << argstr
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
opts
|
97
|
+
end
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: argos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel VanderWerf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: 'A slim command-line parser that does one thing well: turn an array of
|
14
|
+
arguments into a hash of recognized options and their arguments.'
|
15
|
+
email: vjoel@users.sourceforge.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
- COPYING
|
21
|
+
files:
|
22
|
+
- COPYING
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- example/args.rb
|
26
|
+
- lib/argos.rb
|
27
|
+
homepage: https://github.com/vjoel/argos
|
28
|
+
licenses:
|
29
|
+
- BSD
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- "--quiet"
|
34
|
+
- "--line-numbers"
|
35
|
+
- "--inline-source"
|
36
|
+
- "--title"
|
37
|
+
- argos
|
38
|
+
- "--main"
|
39
|
+
- README.md
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.2.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Minimal command-line parser
|
58
|
+
test_files: []
|
59
|
+
has_rdoc:
|