help_parser 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/History.txt +1 -0
- data/Manifest.txt +23 -0
- data/README.rdoc +72 -0
- data/Rakefile +94 -0
- data/TODO.txt +0 -0
- data/examples/command_suite +130 -0
- data/examples/implementation_error_duplicate_key +30 -0
- data/examples/implementation_error_redefinition +29 -0
- data/examples/implementation_error_terminal +30 -0
- data/examples/simple +29 -0
- data/features/edge.feature +30 -0
- data/features/simple.feature +125 -0
- data/features/step_definitions/help_parser_steps.rb +80 -0
- data/features/suite.feature +131 -0
- data/help_parser.gemspec +52 -0
- data/lib/help_parser.rb +2 -0
- data/lib/help_parser/help_parser.rb +285 -0
- data/lib/help_parser/version.rb +3 -0
- data/tasks/gemspecker.rb +60 -0
- data/tasks/manifester.rb +13 -0
- data/tasks/syntaxer.rb +25 -0
- data/tasks/unit_tests.rb +18 -0
- data/test/help_parser.rb +250 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9580ecb3144e3be88b6961efc2e1edae53964c01
|
4
|
+
data.tar.gz: 8924fbd437d234ab59ce1f1bc319be6f9287b93d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d4f0bc72cefd932f76302436d2f898267169a7fc847af683d225333d8d2a7423e9919266b69e03e3168604b6683c4ee1fedbd930d90c837b914f6a601203dac3
|
7
|
+
data.tar.gz: e0af58d61f6825f2e72615a3fafc352a3f8b7296b5cfa0f559e3ed5092eaaf8ac9fc2b9306146e292200e9fd77281d2dd0e6595f2d582635b23e9a3f3064e75b
|
data/History.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
=== 0.0.0 / 2013-12-04
|
data/Manifest.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
TODO.txt
|
6
|
+
examples/command_suite
|
7
|
+
examples/implementation_error_duplicate_key
|
8
|
+
examples/implementation_error_redefinition
|
9
|
+
examples/implementation_error_terminal
|
10
|
+
examples/simple
|
11
|
+
features/edge.feature
|
12
|
+
features/simple.feature
|
13
|
+
features/step_definitions/help_parser_steps.rb
|
14
|
+
features/suite.feature
|
15
|
+
help_parser.gemspec
|
16
|
+
lib/help_parser.rb
|
17
|
+
lib/help_parser/help_parser.rb
|
18
|
+
lib/help_parser/version.rb
|
19
|
+
tasks/gemspecker.rb
|
20
|
+
tasks/manifester.rb
|
21
|
+
tasks/syntaxer.rb
|
22
|
+
tasks/unit_tests.rb
|
23
|
+
test/help_parser.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
= HelpParser
|
2
|
+
|
3
|
+
github :: https://www.github.com/carlosjhr64/help_parser
|
4
|
+
rubygems :: https://rubygems.org/gems/help_parser
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
_HelpParser_ - Parses your help text to define your command line options.
|
9
|
+
|
10
|
+
== FEATURES/PROBLEMS:
|
11
|
+
|
12
|
+
* long to short option mapping
|
13
|
+
* --no-lopt sets lopt to false
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
require 'help_parser'
|
18
|
+
include HELP_PARSER
|
19
|
+
HELP = <<HELP
|
20
|
+
Usage:
|
21
|
+
mycmdapp [options] command
|
22
|
+
Options:
|
23
|
+
-h --help Show this help and exit
|
24
|
+
-v --version Show the version and exit
|
25
|
+
-q --quiet Be quiet
|
26
|
+
-V --verbose Say more about whats going on
|
27
|
+
HELP
|
28
|
+
begin
|
29
|
+
OPTIONS = HelpParser.new('1.0.0', HELP)
|
30
|
+
puts "Tracing..." unless OPTIONS[:quiet]
|
31
|
+
puts "More tracing..." if OPTIONS[:V] # verbose
|
32
|
+
# mycmdapp code continues...
|
33
|
+
# ...
|
34
|
+
rescue UsageException
|
35
|
+
puts $!.message # User just wants help or version
|
36
|
+
exit 0 # Not an error
|
37
|
+
rescue UsageError
|
38
|
+
STDERR.puts '*** '+$!.message+' ***'
|
39
|
+
STDERR.puts $!.obj.help
|
40
|
+
exit 64 # Usage error exit code
|
41
|
+
end
|
42
|
+
|
43
|
+
See `examples/command_suite` for an example of how to build a command suite with _HelpParser_.
|
44
|
+
|
45
|
+
== INSTALL:
|
46
|
+
|
47
|
+
$ sudo gem install help_parser
|
48
|
+
|
49
|
+
== LICENSE:
|
50
|
+
|
51
|
+
(The MIT License)
|
52
|
+
|
53
|
+
Copyright (c) 2013 CarlosJHR64
|
54
|
+
|
55
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
56
|
+
a copy of this software and associated documentation files (the
|
57
|
+
'Software'), to deal in the Software without restriction, including
|
58
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
59
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
60
|
+
permit persons to whom the Software is furnished to do so, subject to
|
61
|
+
the following conditions:
|
62
|
+
|
63
|
+
The above copyright notice and this permission notice shall be
|
64
|
+
included in all copies or substantial portions of the Software.
|
65
|
+
|
66
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
67
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
68
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
69
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
70
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
71
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
72
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'find'
|
2
|
+
|
3
|
+
module RakingTasks
|
4
|
+
def RakingTasks.git_tag_list
|
5
|
+
`git tag --list`.strip.split("\n")
|
6
|
+
end
|
7
|
+
|
8
|
+
def RakingTasks.git_status_porcelain
|
9
|
+
`git status --porcelain`.strip
|
10
|
+
end
|
11
|
+
|
12
|
+
def RakingTasks.gemspec_files
|
13
|
+
`ls ./*.gemspec 2> /dev/null`.strip.split("\n")
|
14
|
+
end
|
15
|
+
|
16
|
+
def RakingTasks.uniq_gemspec_file
|
17
|
+
gemspecs = RakingTasks.gemspec_files
|
18
|
+
raise "Gemspec file not found" if gemspecs.length == 0
|
19
|
+
raise "Mutiple gemspec files found" if gemspecs.length > 1
|
20
|
+
gemspecs.first
|
21
|
+
end
|
22
|
+
|
23
|
+
def RakingTasks.pkg_gem_files
|
24
|
+
`ls ./pkg/*.gem 2> /dev/null`.strip.split("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
def RakingTasks.gem_files
|
28
|
+
`ls ./*.gem 2> /dev/null`.strip.split("\n")
|
29
|
+
end
|
30
|
+
|
31
|
+
def RakingTasks.uniq_gem_file
|
32
|
+
gems = RakingTasks.gem_files
|
33
|
+
raise "Gem file not found" if gems.length == 0
|
34
|
+
raise "Multiple gem files found" if gems.length > 1
|
35
|
+
gems.first
|
36
|
+
end
|
37
|
+
|
38
|
+
def RakingTasks.gem_build
|
39
|
+
raise "Git status not clear" unless RakingTasks.git_status_porcelain.length == 0
|
40
|
+
gems = RakingTasks.gem_files
|
41
|
+
raise "Found gem files: #{gems.join(',')}" unless gems.length == 0
|
42
|
+
gemspec = RakingTasks.uniq_gemspec_file
|
43
|
+
system("gem build #{gemspec}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def RakingTasks.load_existing_tasks
|
47
|
+
if Dir.exist?('./tasks')
|
48
|
+
Find.find('./tasks') do |fn|
|
49
|
+
require fn if fn=~/\.rb/
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
RakingTasks.load_existing_tasks
|
55
|
+
|
56
|
+
desc 'By default, run git status'
|
57
|
+
task :default do
|
58
|
+
system('git status')
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Cucumbers tests'
|
62
|
+
task :cucumber do
|
63
|
+
system('cucumber -f progress')
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Runs all tests'
|
67
|
+
task :'test-all' => [:syntaxer, :'unit-tests', :cucumber]
|
68
|
+
|
69
|
+
desc 'Builds pre-release gem for testing, not published.'
|
70
|
+
task :gem_build do
|
71
|
+
RakingTasks.gem_build
|
72
|
+
end
|
73
|
+
|
74
|
+
desc 'pushes to rubygems and git'
|
75
|
+
task :publish, :version do |t, args|
|
76
|
+
version = args[:version]
|
77
|
+
tags = RakingTasks.git_tag_list
|
78
|
+
raise "#{version} in git tag list" if tags.include?(version)
|
79
|
+
RakingTasks.gem_build
|
80
|
+
gem = File.basename RakingTasks.uniq_gem_file
|
81
|
+
raise "#{version} did not match gem file" unless gem.include?(version)
|
82
|
+
pkgem = File.join('pkg', gem)
|
83
|
+
raise "pkg/#{gem} exists!?" if File.exist? pkgem
|
84
|
+
if system("gem push #{gem}")
|
85
|
+
File.rename(gem, pkgem)
|
86
|
+
File.open('./History.txt', 'a'){|fh| fh.puts "=== #{version} / #{Time.now}"}
|
87
|
+
File.open('./History.txt', 'a'){|fh| fh.puts `md5sum #{pkgem}`}
|
88
|
+
system("git commit -m '#{version}' ./History.txt")
|
89
|
+
system("git tag '#{version}'")
|
90
|
+
system('git push')
|
91
|
+
else
|
92
|
+
File.unlink(gem)
|
93
|
+
end
|
94
|
+
end
|
data/TODO.txt
ADDED
File without changes
|
@@ -0,0 +1,130 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'json'
|
3
|
+
require 'rainbow'
|
4
|
+
|
5
|
+
############################################################
|
6
|
+
# Here I show how one might structure a command suite
|
7
|
+
# using HelpParser. The output is for cucumber testing.
|
8
|
+
############################################################
|
9
|
+
|
10
|
+
VERSION = '10.02.3'
|
11
|
+
NAME = File.basename($PROGRAM_NAME) # command_suite
|
12
|
+
GLOBAL_HELP = <<GLOBAL_HELP
|
13
|
+
This is a test script for HelpParser.
|
14
|
+
|
15
|
+
Usage:
|
16
|
+
|
17
|
+
#{NAME} [global options] command [command options] [arguments...]
|
18
|
+
|
19
|
+
Global Options:
|
20
|
+
|
21
|
+
-h --help Show this help and exit
|
22
|
+
-v --version Show the version and exit
|
23
|
+
-q --quiet Be quiet
|
24
|
+
-V --verbose Say more about whats going on
|
25
|
+
|
26
|
+
--string=STRING A String value
|
27
|
+
--float=FLOAT A Float value
|
28
|
+
--integer=INTEGER An Integer value
|
29
|
+
--array=val1,val2... An Array of values
|
30
|
+
|
31
|
+
Commands:
|
32
|
+
|
33
|
+
first The first command
|
34
|
+
second The second command
|
35
|
+
|
36
|
+
This script is being runned by Ruby #{RUBY_VERSION}.
|
37
|
+
GLOBAL_HELP
|
38
|
+
|
39
|
+
FIRST_HELP = <<FIRST
|
40
|
+
#{NAME}-first is the first command of the test script for HelpParser.
|
41
|
+
Usage: #{NAME} [global options] first [options]
|
42
|
+
Options:
|
43
|
+
-h --help Show this help and exit
|
44
|
+
--string=STRING A String value
|
45
|
+
--float=FLOAT A Float value
|
46
|
+
--integer=INTEGER An Integer value
|
47
|
+
--array=val1,val2... An Array of values
|
48
|
+
FIRST
|
49
|
+
|
50
|
+
SECOND_HELP = <<SECOND
|
51
|
+
#{NAME}-second is the second command of the test script for HelpParser.
|
52
|
+
Usage: #{NAME} [global options] second [options]
|
53
|
+
Options:
|
54
|
+
-h --help Show this help and exit
|
55
|
+
--string=STRING A String value
|
56
|
+
--float=FLOAT A Float value
|
57
|
+
--integer=INTEGER An Integer value
|
58
|
+
--array=val1,val2... An Array of values
|
59
|
+
SECOND
|
60
|
+
|
61
|
+
begin
|
62
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
63
|
+
require 'help_parser'
|
64
|
+
include HELP_PARSER
|
65
|
+
|
66
|
+
GLOBAL_OPTIONS = HelpParser.new(VERSION, GLOBAL_HELP)
|
67
|
+
|
68
|
+
STRING = GLOBAL_OPTIONS[:string, 'string1', 'string2']
|
69
|
+
FLOAT = GLOBAL_OPTIONS[:float, 1.0, 2.0]
|
70
|
+
INTEGER = GLOBAL_OPTIONS[:integer, 1, 2]
|
71
|
+
ARRAY = GLOBAL_OPTIONS[:array, [], [1,'two',3.0]]
|
72
|
+
|
73
|
+
GLOBAL_OPTIONS.usage_error "Need command." if ARGV.empty?
|
74
|
+
COMMAND = ARGV.shift
|
75
|
+
# I see suites this way:
|
76
|
+
# system("command #{ARGV.join(' ')}")
|
77
|
+
# but...
|
78
|
+
case COMMAND
|
79
|
+
when 'first'
|
80
|
+
# Just going to reuse VERSION, although
|
81
|
+
# the comand could have it's own version number.
|
82
|
+
OPTIONS = HelpParser.new(VERSION, FIRST_HELP)
|
83
|
+
# CONST = OPTIONS[key, missing_key, missing_value]
|
84
|
+
CSTRING = OPTIONS[:string, 'string1', 'string2']
|
85
|
+
CFLOAT = OPTIONS[:float, 1.0, 2.0]
|
86
|
+
CINTEGER = OPTIONS[:integer, 1, 2]
|
87
|
+
CARRAY = OPTIONS[:array, [], [1,'two',3.0]]
|
88
|
+
when 'second'
|
89
|
+
OPTIONS = HelpParser.new(VERSION, SECOND_HELP)
|
90
|
+
CSTRING = OPTIONS[:string, 'string1', 'string2']
|
91
|
+
CFLOAT = OPTIONS[:float, 1.0, 2.0]
|
92
|
+
CINTEGER = OPTIONS[:integer, 1, 2]
|
93
|
+
CARRAY = OPTIONS[:array, [], [1,'two',3.0]]
|
94
|
+
else
|
95
|
+
GLOBAL_OPTIONS.usage_error "Unrecognized command #{COMMAND}."
|
96
|
+
end
|
97
|
+
|
98
|
+
OUTPUT = {
|
99
|
+
:exception => '',
|
100
|
+
:message=> '',
|
101
|
+
:command => COMMAND,
|
102
|
+
:options => OPTIONS,
|
103
|
+
:global_options => GLOBAL_OPTIONS,
|
104
|
+
# Constants from Global
|
105
|
+
:string => STRING,
|
106
|
+
:float => FLOAT,
|
107
|
+
:integer => INTEGER,
|
108
|
+
:array => ARRAY,
|
109
|
+
# Constants from Command
|
110
|
+
:cstring => CSTRING,
|
111
|
+
:cfloat => CFLOAT,
|
112
|
+
:cinteger => CINTEGER,
|
113
|
+
:carray => CARRAY,
|
114
|
+
}
|
115
|
+
|
116
|
+
rescue Exception
|
117
|
+
STDERR.puts $!.message.chomp.color(:blue)
|
118
|
+
STDERR.puts $!.backtrace.join("\n").color(:red)
|
119
|
+
OUTPUT = {
|
120
|
+
:exception => $!.class.to_s,
|
121
|
+
:message=> $!.message,
|
122
|
+
# These below might be undefined:
|
123
|
+
:command => (COMMAND||=''),
|
124
|
+
:global_options => (GLOBAL_OPTIONS||={}),
|
125
|
+
:command_options => (OPTIONS||={}),
|
126
|
+
:options => ($!.respond_to?(:obj))? $!.obj : {},
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
puts JSON.pretty_generate OUTPUT
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This is used by cucumber for testing.
|
3
|
+
# It has a bad help text.
|
4
|
+
require 'json'
|
5
|
+
require 'rainbow'
|
6
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
7
|
+
require 'help_parser'
|
8
|
+
include HELP_PARSER
|
9
|
+
HELP = <<HELP
|
10
|
+
Usage:
|
11
|
+
simple [options] arguments...
|
12
|
+
Options:
|
13
|
+
-z --zebra
|
14
|
+
-a --zebra
|
15
|
+
HELP
|
16
|
+
output = {
|
17
|
+
:exception => '',
|
18
|
+
:message => '',
|
19
|
+
}
|
20
|
+
begin
|
21
|
+
OPTIONS = HelpParser.new('1.2.3', HELP)
|
22
|
+
rescue Exception
|
23
|
+
STDERR.puts $!.obj.help.chomp.color(:blue)
|
24
|
+
STDERR.puts $!.message.chomp.color(:red)
|
25
|
+
output = {
|
26
|
+
:exception => $!.class.to_s,
|
27
|
+
:message => $!.message,
|
28
|
+
}
|
29
|
+
end
|
30
|
+
puts JSON.pretty_generate output
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This is used by cucumber for testing.
|
3
|
+
# It has a bad help text.
|
4
|
+
require 'json'
|
5
|
+
require 'rainbow'
|
6
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
7
|
+
require 'help_parser'
|
8
|
+
include HELP_PARSER
|
9
|
+
HELP = <<HELP
|
10
|
+
Usage:
|
11
|
+
simple [options] arguments...
|
12
|
+
Options:
|
13
|
+
-z --help
|
14
|
+
HELP
|
15
|
+
output = {
|
16
|
+
:exception => '',
|
17
|
+
:message => '',
|
18
|
+
}
|
19
|
+
begin
|
20
|
+
OPTIONS = HelpParser.new('1.2.3', HELP)
|
21
|
+
rescue Exception
|
22
|
+
STDERR.puts $!.obj.help.chop.color(:blue)
|
23
|
+
STDERR.puts $!.message.chomp.color(:red)
|
24
|
+
output = {
|
25
|
+
:exception => $!.class.to_s,
|
26
|
+
:message => $!.message,
|
27
|
+
}
|
28
|
+
end
|
29
|
+
puts JSON.pretty_generate output
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This is used by cucumber for testing.
|
3
|
+
# It has a bad help text.
|
4
|
+
require 'json'
|
5
|
+
require 'rainbow'
|
6
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
7
|
+
require 'help_parser'
|
8
|
+
include HELP_PARSER
|
9
|
+
HELP = <<HELP
|
10
|
+
Usage:
|
11
|
+
simple [options] arguments...
|
12
|
+
Options:
|
13
|
+
-a --aaa
|
14
|
+
-b --a
|
15
|
+
HELP
|
16
|
+
output = {
|
17
|
+
:exception => '',
|
18
|
+
:message => '',
|
19
|
+
}
|
20
|
+
begin
|
21
|
+
OPTIONS = HelpParser.new('1.2.3', HELP)
|
22
|
+
rescue Exception
|
23
|
+
STDERR.puts $!.obj.help.chomp.color(:blue)
|
24
|
+
STDERR.puts $!.message.chomp.color(:red)
|
25
|
+
output = {
|
26
|
+
:exception => $!.class.to_s,
|
27
|
+
:message => $!.message,
|
28
|
+
}
|
29
|
+
end
|
30
|
+
puts JSON.pretty_generate output
|
data/examples/simple
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This is used by cucumber for testing.
|
3
|
+
require 'json'
|
4
|
+
require 'rainbow'
|
5
|
+
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
|
6
|
+
require 'help_parser'
|
7
|
+
include HELP_PARSER
|
8
|
+
HELP = <<HELP
|
9
|
+
Usage:
|
10
|
+
simple [options] arguments...
|
11
|
+
Options:
|
12
|
+
-h --help Help already in dictionary
|
13
|
+
Missing version in dictionary
|
14
|
+
--nocomment
|
15
|
+
-s --long=VALUE Full template
|
16
|
+
--V Single characters can have long format
|
17
|
+
-k Short option does not need a long option
|
18
|
+
HELP
|
19
|
+
begin
|
20
|
+
OPTIONS = HelpParser.new('1.2.3', HELP)
|
21
|
+
data = { :exception => '', :message => '', :options => OPTIONS }
|
22
|
+
puts JSON.pretty_generate data
|
23
|
+
rescue Exception
|
24
|
+
STDERR.puts $!.message.chomp.color(:blue)
|
25
|
+
STDERR.puts $!.backtrace.join("\n").color(:red)
|
26
|
+
data = { :exception => $!.class, :message => $!.message, :options => $!.obj }
|
27
|
+
data[:options] = ($!.respond_to?(:obj))? $!.obj : {}
|
28
|
+
puts JSON.pretty_generate data
|
29
|
+
end
|