Cmdline_Parser 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.txt +34 -2
- data/changeset.txt +12 -0
- data/lib/cmdline_parser.rb +10 -10
- metadata +3 -2
data/README.txt
CHANGED
@@ -1,13 +1,45 @@
|
|
1
1
|
Cmdline Parser
|
2
|
-
______________
|
3
2
|
|
4
3
|
I intended to design the command line parser to be embedded into my application.
|
5
4
|
However, since more and more project of mine include this as library, i decided to separate it out as gem so that
|
6
5
|
i can share it with all my project instead. After all, if i update in one place, all my project received the update.
|
6
|
+
Many thanks to Choice developer (http://choice.rubyforge.org/) chris[at]ozmm[dot]org for providing me the inspiration!
|
7
|
+
|
8
|
+
The reason i didn't use choice is that I couldn't figure out how to control when i want it to parse. That's the reason
|
9
|
+
the method name is parse_list, which trigger the parsing only when i need it with the data i provided, not only from ARGV.
|
7
10
|
|
8
11
|
Please sent me comment of your usage of the library! I would love to hear that!
|
9
12
|
|
10
13
|
Author : Chris Liaw
|
11
14
|
Copyright : Chris Liaw
|
12
|
-
License : GNU
|
15
|
+
License : GNU LGPL
|
16
|
+
|
17
|
+
Usage:
|
13
18
|
|
19
|
+
Create option:
|
20
|
+
require 'rubygems'
|
21
|
+
require 'cmdlineparser'
|
22
|
+
|
23
|
+
parser = CmdlineParser.new
|
24
|
+
parser.choice :name do |option|
|
25
|
+
option.short = "-f" # short flag user need to enter
|
26
|
+
option.long = "--file" #long flag user need to enter
|
27
|
+
option.desc = "Specified which file to process" #description for this flag. Printed out in help message
|
28
|
+
option.has_value = true # or false as default. Indicate that the parser should store the value followed the flag or else parser ignore it
|
29
|
+
option.mandatory = true # or false as default. Indicate the option is a must and if missing, it will throw MissingArgumentException
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
parser.parse_list(ARGV) # parameter can be any list, ARGV just for sample. See test/test_cmdline_parser.rb for sample.
|
34
|
+
rescue MissingArgumentException => ex # If any mandatory flag was not given by user, this error will be triggered.
|
35
|
+
STDERR.puts "ERROR: #{ex.message}" # If a flag requires value but value not given, this error will also be triggered.
|
36
|
+
end
|
37
|
+
|
38
|
+
if parser.name_obj.activated # check if user provide the flag. name => name_obj to differentiate user want the object instead the value
|
39
|
+
parser.name # return the value given by user
|
40
|
+
end
|
41
|
+
|
42
|
+
parser.params # return any free parameter which is not tied to any flag. It could be the parameter for the function call.
|
43
|
+
|
44
|
+
parser.show_help # print out the help message.
|
45
|
+
|
data/changeset.txt
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
V0.1.1 (19th August 2009)
|
6
|
+
[Added] Attributes of missing_mandatory to allow client to query for which flag is missing
|
7
|
+
[Added] Sample usage in the README.txt
|
8
|
+
[Edited] Change the license from GPL to LGPL
|
9
|
+
|
10
|
+
V0.1.0 (18th August 2009)
|
11
|
+
* Initial release
|
12
|
+
|
data/lib/cmdline_parser.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
|
2
2
|
|
3
3
|
class CmdlineParser
|
4
|
-
attr_accessor :choices, :params
|
4
|
+
attr_accessor :choices, :params, :missing_mandatory
|
5
5
|
|
6
|
-
VERSION = "0.1.
|
6
|
+
VERSION = "0.1.1"
|
7
7
|
|
8
8
|
def initialize(opt = {})
|
9
9
|
@choices = []
|
10
10
|
@short_map = {}
|
11
11
|
@long_map = {}
|
12
12
|
@params = []
|
13
|
-
@
|
13
|
+
@missing_mandatory = {}
|
14
14
|
@search = {}
|
15
15
|
@error_as_message = opt[:error_as_message]
|
16
16
|
@error_as_message ||= false
|
@@ -54,8 +54,8 @@ class CmdlineParser
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
if @
|
58
|
-
@
|
57
|
+
if @missing_mandatory.keys.include? opt.name
|
58
|
+
@missing_mandatory.delete(opt.name)
|
59
59
|
end
|
60
60
|
|
61
61
|
elsif @long_map.keys.include? token
|
@@ -83,8 +83,8 @@ class CmdlineParser
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
if @
|
87
|
-
@
|
86
|
+
if @missing_mandatory.keys.include? opt.name
|
87
|
+
@missing_mandatory.delete(opt.name)
|
88
88
|
end
|
89
89
|
|
90
90
|
else
|
@@ -94,9 +94,9 @@ class CmdlineParser
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
-
if @
|
97
|
+
if @missing_mandatory.length > 0
|
98
98
|
msg = []
|
99
|
-
@
|
99
|
+
@missing_mandatory.values.each do |ch|
|
100
100
|
msg << "#{ch.short} or #{ch.long}"
|
101
101
|
end
|
102
102
|
|
@@ -149,7 +149,7 @@ class CmdlineParser
|
|
149
149
|
@short_map[ch.short] = ch
|
150
150
|
@long_map[ch.long] = ch
|
151
151
|
if ch.mandatory == true
|
152
|
-
@
|
152
|
+
@missing_mandatory[ch.name] = ch
|
153
153
|
end
|
154
154
|
end
|
155
155
|
end
|
metadata
CHANGED
@@ -25,6 +25,7 @@ description:
|
|
25
25
|
specification_version: 2
|
26
26
|
default_executable:
|
27
27
|
files:
|
28
|
+
- changeset.txt
|
28
29
|
- README.txt
|
29
30
|
- cmdlineparser.rb
|
30
31
|
- lib/cmdline_parser.rb
|
@@ -43,12 +44,12 @@ requirements: []
|
|
43
44
|
|
44
45
|
authors:
|
45
46
|
- Chris Liaw Man Cheon
|
46
|
-
date: 2009-08-
|
47
|
+
date: 2009-08-18 16:00:00 +00:00
|
47
48
|
platform: ruby
|
48
49
|
test_files: []
|
49
50
|
|
50
51
|
version: !ruby/object:Gem::Version
|
51
|
-
version: 0.1.
|
52
|
+
version: 0.1.1
|
52
53
|
require_paths:
|
53
54
|
- .
|
54
55
|
dependencies: []
|