optiflag 0.6 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- data/ReleaseNotes.txt +35 -0
- data/doc/classes/OptiFlag.html +439 -0
- data/doc/classes/OptiFlag.src/M000001.html +49 -0
- data/doc/classes/OptiFlag.src/M000002.html +49 -0
- data/doc/classes/OptiFlag.src/M000003.html +30 -0
- data/doc/classes/OptiFlag/Flagset.html +459 -0
- data/doc/classes/OptiFlag/Flagset.src/M000003.html +45 -0
- data/doc/classes/OptiFlag/Flagset.src/M000004.html +45 -0
- data/doc/classes/OptiFlag/Flagset.src/M000005.html +18 -0
- data/doc/classes/OptiFlag/Flagset.src/M000006.html +20 -0
- data/doc/classes/OptiFlag/Flagset.src/M000007.html +35 -0
- data/doc/classes/OptiFlag/Flagset.src/M000008.html +23 -0
- data/doc/classes/OptiFlag/Flagset.src/M000009.html +24 -0
- data/doc/classes/OptiFlag/Flagset.src/M000010.html +28 -0
- data/doc/classes/OptiFlag/Flagset.src/M000011.html +20 -0
- data/doc/classes/OptiFlag/Flagset.src/M000012.html +24 -0
- data/doc/classes/OptiFlag/Flagset.src/M000013.html +24 -0
- data/doc/classes/OptiFlag/Flagset.src/M000014.html +32 -0
- data/doc/classes/OptiFlag/Flagset.src/M000015.html +25 -0
- data/doc/classes/OptiFlag/Flagset.src/M000016.html +53 -0
- data/doc/classes/OptiFlag/Flagset.src/M000017.html +53 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.html +930 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000024.html +36 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000025.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000026.html +36 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000027.html +36 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000028.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000029.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000030.html +19 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000031.html +19 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000032.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000033.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000034.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000035.html +22 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000036.html +19 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000037.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000038.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000039.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000040.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000041.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000042.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000043.html +19 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000044.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000045.html +19 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000046.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000047.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000048.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000049.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000050.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000051.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000052.html +20 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000053.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000054.html +18 -0
- data/doc/classes/OptiFlag/Flagset/EachFlag.src/M000055.html +22 -0
- data/doc/classes/OptiFlag/Flagset/Errors.html +192 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000021.html +18 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000022.html +19 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000023.html +18 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000024.html +18 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000025.html +19 -0
- data/doc/classes/OptiFlag/Flagset/Errors.src/M000026.html +36 -0
- data/doc/classes/OptiFlag/Flagset/Help.html +125 -0
- data/doc/classes/OptiFlag/Flagset/Help/Bundle.html +160 -0
- data/doc/classes/OptiFlag/Flagset/Help/Bundle.src/M000018.html +18 -0
- data/doc/classes/OptiFlag/Flagset/Help/Bundle.src/M000019.html +18 -0
- data/doc/classes/OptiFlag/Flagset/Help/StandardHelpBundle.html +118 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.html +296 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000017.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000018.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000019.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000020.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000021.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000022.html +18 -0
- data/doc/classes/OptiFlag/Flagset/NewInterface.src/M000023.html +18 -0
- data/doc/created.rid +1 -0
- data/doc/files/optiflag-help_rb.html +101 -0
- data/doc/files/optiflag-parse_rb.html +101 -0
- data/doc/files/optiflag_rb.html +167 -0
- data/doc/files/optiflag_rb.src/M000001.html +18 -0
- data/doc/fr_class_index.html +34 -0
- data/doc/fr_file_index.html +29 -0
- data/doc/fr_method_index.html +82 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/examples/example_1.rb +21 -0
- data/examples/example_1_1.rb +21 -0
- data/{doc/example → examples}/example_1_2.rb +8 -8
- data/examples/example_2.rb +31 -0
- data/{doc/example → examples}/example_2_1.rb +4 -4
- data/{doc/example → examples}/example_2_2.rb +7 -5
- data/{doc/example → examples}/example_2_3.rb +9 -9
- data/{doc/example → examples}/example_2_4.rb +4 -4
- data/{doc/example → examples}/example_2_5.rb +4 -4
- data/{doc/example → examples}/example_2_6.rb +6 -6
- data/{doc/example → examples}/example_3.rb +7 -6
- data/examples/example_6.rb +20 -0
- data/{doc/example → examples}/example_7.rb +5 -4
- data/examples/example_8.rb +26 -0
- data/optiflag-help.rb +42 -0
- data/optiflag-parse.rb +281 -0
- data/optiflag.gemspec +4 -3
- data/optiflag.rb +972 -0
- data/quick.rb +12 -0
- data/{test → testcases}/tc_advanced_usage_helping.rb +4 -0
- data/{test → testcases}/tc_basic_alternate_forms.rb +0 -0
- data/{test → testcases}/tc_basic_char_flags.rb +12 -12
- data/{test → testcases}/tc_basic_optional_flag.rb +0 -0
- data/{test → testcases}/tc_basic_parsing.rb +0 -0
- data/{test → testcases}/tc_basic_usage_helping.rb +0 -0
- data/{test → testcases}/tc_basic_value_validation.rb +0 -0
- data/{test → testcases}/tc_bug_one.rb +0 -0
- data/{test → testcases}/tc_bug_two.rb +0 -0
- data/{test → testcases}/tc_change_symbols.rb +0 -0
- data/{test → testcases}/tc_enumerated_value_validation.rb +0 -0
- data/{test → testcases}/tc_flagall.rb +4 -0
- data/{test → testcases}/tc_flagless_arg.rb +0 -0
- data/{test → testcases}/tc_keyword.rb +0 -0
- data/testcases/tc_new_basic_parsing.rb +51 -0
- data/testcases/tc_poro_tester.rb +30 -0
- data/{test → testcases}/tc_values_as_hash.rb +0 -0
- metadata +152 -52
- data/doc/example/example_1.rb +0 -30
- data/doc/example/example_1_1.rb +0 -27
- data/doc/example/example_2.rb +0 -28
- data/doc/example/example_4.rb +0 -12
- data/doc/example/example_5.rb +0 -26
- data/doc/example/example_6.rb +0 -17
- data/lib/optiflag.rb +0 -860
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'optiflag'
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
module Example extend
|
3
|
+
# Title: Using the optional switch flag, a zero argument optional flag
|
4
|
+
# Description: If your flag is optional and without arguments, it is a switch. Use optional_switch_flag to indicate.
|
5
|
+
module Example extend OptiFlagSet
|
6
6
|
flag "dir"
|
7
7
|
optional_switch_flag "clear"
|
8
8
|
|
9
|
-
|
9
|
+
and_process!
|
10
10
|
end
|
11
11
|
|
12
12
|
|
13
13
|
|
14
|
-
|
15
|
-
if
|
14
|
+
|
15
|
+
if ARGV.flags.clear?
|
16
16
|
puts "The optional switch flag -clear has been invoked"
|
17
17
|
else
|
18
18
|
puts "The optional switch flag -clear was NOT invoked"
|
@@ -1,24 +1,25 @@
|
|
1
1
|
require 'optiflag'
|
2
2
|
|
3
|
-
#
|
4
|
-
|
5
|
-
module Example extend
|
3
|
+
# Title: Adding validation rules to the value of an input flag
|
4
|
+
# Description: See the flags "mode", "run_date", and "connection" for examples of both forcing values to be in a set (value_in_set) or to match a particular regexp (value_matches).
|
5
|
+
module Example extend OptiFlagSet
|
6
6
|
flag "dir"
|
7
7
|
flag "connection" do
|
8
|
+
# this is our new clause-level modifier
|
8
9
|
value_matches ["Connection string must be of the form username/password@servicename", /^\b.+\b\/\b.+@.+$/ ]
|
9
10
|
end
|
10
11
|
optional_flag "mode" do
|
12
|
+
# this is our second new clause-level modifier
|
11
13
|
value_in_set ["read","write","execute"]
|
12
14
|
end
|
13
15
|
optional_flag "run_date" do
|
14
16
|
value_matches ["run_date must be of the form mm/DD/YY",/^[0-9]{2}\/[0-9]{2}\/[0-9]{2,4}$/]
|
15
17
|
end
|
16
|
-
usage_flag "h","help","?"
|
17
18
|
|
18
|
-
|
19
|
+
and_process!
|
19
20
|
end
|
20
21
|
|
21
|
-
flag =
|
22
|
+
flag = Example.flags
|
22
23
|
|
23
24
|
puts "Mode flag is #{ flag.mode }" if flag.mode?
|
24
25
|
puts "Run Date flag is #{ flag.run_date }" if flag.run_date?
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'optiflag'
|
2
|
+
|
3
|
+
# Title: Accessing values of the flags through a hash instead of a method.
|
4
|
+
# Description: Usually we have been accessing by doing this 'ARGV.flags.dir' but if you have a flag with a non alpha-numeric flag can't be accessed via a method name. Therefore we have to use a hash.
|
5
|
+
module HashAcess extend OptiFlagSet
|
6
|
+
flag "dir"
|
7
|
+
flag "log_level"
|
8
|
+
flag "&"
|
9
|
+
|
10
|
+
and_process!
|
11
|
+
end
|
12
|
+
|
13
|
+
puts "Dir is: #{ ARGV.flags[:dir] }"
|
14
|
+
puts "Log Level is: #{ ARGV.flags[:log_level] }"
|
15
|
+
puts "Ampersand Level is: #{ ARGV.flags[:&] }"
|
16
|
+
|
17
|
+
#h# ruby example_6.rb -dir "c:/Program Files/Apache Software Foundation/Tomcat 5/" -log_level 3 -& ETALL
|
18
|
+
|
19
|
+
|
20
|
+
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'optiflag'
|
2
2
|
|
3
|
-
#
|
4
|
-
|
3
|
+
# Title: Character flags and clustering.
|
4
|
+
# Description: character flags are simple single character flags with zero arity and which are optional. An optional symbol as a second parameter to the 'character_flag' groups the character within a cluster in the same name. This last point will probably change (see 'discussion' on the web page for a better idea).
|
5
|
+
module HashAcess extend OptiFlagSet
|
5
6
|
|
6
7
|
character_flag :l, :list_group
|
7
8
|
character_flag :s, :list_group
|
@@ -17,10 +18,10 @@ module HashAcess extend OptiFlag::Flagset
|
|
17
18
|
description "Archive File"
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
+
and_process!
|
21
22
|
end
|
22
23
|
|
23
|
-
f = ARGV.
|
24
|
+
f = ARGV.flags
|
24
25
|
puts "'l' flag set?: #{ f.l? }"
|
25
26
|
puts "'s' flag set?: #{ f.s? }"
|
26
27
|
puts "'a' flag set?: #{ f.a? }"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'optiflag'
|
2
|
+
|
3
|
+
# Title: SUPER EASY alternative method for getting command line options into your program.
|
4
|
+
# Description: Rather than use the 'module' DSL syntax, we just re-use an existing class. Any method accessor will be used verbatim as a switch. OptiFlag will crawl up the inheritance hierarchy up to (but not including) object and use all accessors as standard 'flag's.
|
5
|
+
class Person
|
6
|
+
attr_accessor :name,:ssn
|
7
|
+
end
|
8
|
+
class Employee < Person
|
9
|
+
attr_accessor :username,:password
|
10
|
+
end
|
11
|
+
|
12
|
+
daniel = Employee.new
|
13
|
+
|
14
|
+
OptiFlag.using_object(daniel)
|
15
|
+
|
16
|
+
puts <<-EOF
|
17
|
+
Name: #{ daniel.name}
|
18
|
+
Password: #{ daniel.password}
|
19
|
+
Username: #{ daniel.username}
|
20
|
+
SSN: #{ daniel.ssn}
|
21
|
+
EOF
|
22
|
+
|
23
|
+
#h# ruby example_8.rb
|
24
|
+
#h# ruby example_8.rb -username doeklund -password AHA -ssn 1234562342 -name "Daniel Eklund"
|
25
|
+
|
26
|
+
|
data/optiflag-help.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module OptiFlag
|
2
|
+
module Flagset
|
3
|
+
module Help
|
4
|
+
class Bundle
|
5
|
+
attr_accessor :help,:extended_help,:banner;
|
6
|
+
end
|
7
|
+
StandardHelpBundle = Bundle.new
|
8
|
+
StandardHelpBundle.banner = proc do |render_on|
|
9
|
+
render_on.printf("Help for commands:\n")
|
10
|
+
end
|
11
|
+
StandardHelpBundle.help = proc do |render_on,flag|
|
12
|
+
render_on.printf(" #{ flag.the_dash_symbol }%-10s (#{ flag.the_is_required ? 'Required' : 'Optional' }, takes #{flag.the_arity} argument#{ (flag.the_arity==1) ? '' : 's' })\n", flag.flag)
|
13
|
+
render_on.printf(" #{ flag.the_description}\n") if flag.the_description
|
14
|
+
end
|
15
|
+
StandardHelpBundle.extended_help = proc do |render_on,flag|
|
16
|
+
render_on.puts "----------------"
|
17
|
+
desc = ""
|
18
|
+
desc << <<-EOF if flag.the_description
|
19
|
+
Description: #{ flag.the_description }
|
20
|
+
EOF
|
21
|
+
desc << <<-EOF
|
22
|
+
Flag: #{ flag.the_dash_symbol }#{ flag.flag } (#{ flag.the_is_required ? 'Required' : 'Optional' }, takes #{flag.the_arity} argument#{ (flag.the_arity==1) ? '' : 's' })
|
23
|
+
EOF
|
24
|
+
desc << <<-EOF if flag.the_long_form
|
25
|
+
Long Form: #{ flag.the_long_dash_symbol }#{ flag.the_long_form }
|
26
|
+
EOF
|
27
|
+
desc << <<-EOF if flag.the_alternate_forms.length > 0
|
28
|
+
Alternate Flags: #{ flag.the_alternate_forms.collect{|x| "#{ flag.the_dash_symbol}#{ x}" }.join(', ') }
|
29
|
+
EOF
|
30
|
+
render_on.puts desc
|
31
|
+
end
|
32
|
+
MAN_HELP = proc do |render_on,flag|
|
33
|
+
render_on.puts <<-EOHELP
|
34
|
+
#{ flag.the_dash_symbol }#{flag.flag }, #{ flag.the_long_dash_symbol }#{ flag.the_long_form}
|
35
|
+
#{ flag.the_description }
|
36
|
+
|
37
|
+
EOHELP
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
data/optiflag-parse.rb
ADDED
@@ -0,0 +1,281 @@
|
|
1
|
+
module OptiFlag
|
2
|
+
module Flagset
|
3
|
+
def parse(args,clone=true)
|
4
|
+
init_help_bundle()
|
5
|
+
safe_args = args.clone if clone
|
6
|
+
safe_args = args if ! clone
|
7
|
+
# the following 10 lines were changed so
|
8
|
+
# that a module could reparse a command-line
|
9
|
+
# and not have a global-state change for
|
10
|
+
# everyone... I had mulled just mandating that
|
11
|
+
# a SomeArgModule::parse(ARGV) statement
|
12
|
+
# could only occur once, but since everything else
|
13
|
+
# is getting ugly, might as well allow this
|
14
|
+
# --D.O.E 5/30/2006
|
15
|
+
new_self = self.clone
|
16
|
+
new_flags = {}
|
17
|
+
@all_flags.each_pair do |key,val|
|
18
|
+
val = val.clone
|
19
|
+
new_flags[key] = val
|
20
|
+
end
|
21
|
+
new_self.module_eval do
|
22
|
+
@all_flags = new_flags
|
23
|
+
safe_args = search_for_missing_character_switches(safe_args)
|
24
|
+
safe_args = create_api(safe_args)
|
25
|
+
safe_args = search_for_missing_flags(safe_args)
|
26
|
+
populate_values(safe_args)
|
27
|
+
now_populate_hash(@all_flags,safe_args)
|
28
|
+
end
|
29
|
+
return safe_args
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def init_help_bundle
|
34
|
+
# thank god for the ||= operator
|
35
|
+
@help_bundle ||= Help::StandardHelpBundle
|
36
|
+
end
|
37
|
+
def now_populate_hash(all_flags,safe_args)
|
38
|
+
all_flags.each_pair do |k,v|
|
39
|
+
safe_args.flag_value[k.to_sym] = v.value
|
40
|
+
v.the_alternate_forms.each do |x|
|
41
|
+
safe_args.flag_value[x.to_sym] = v.value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
def find_help_flags(safe_args)
|
46
|
+
arg_copy = safe_args.clone
|
47
|
+
|
48
|
+
#first we get rid of all non-help flags...
|
49
|
+
non_help_flags = @all_flags.values.select{|value| !value.for_help }
|
50
|
+
non_help_flags.each do |val|
|
51
|
+
flag_finder_and_stripper(val.as_the_form_that_is_actually_used,val.the_arity,arg_copy)
|
52
|
+
end
|
53
|
+
# ...because the help flag is overloaded... it can have an arity of 0 or 1
|
54
|
+
help_flag = @all_flags.values.select{|value| value.for_help }
|
55
|
+
if help_flag.length > 0
|
56
|
+
flag = help_flag[0].as_the_form_that_is_actually_used
|
57
|
+
|
58
|
+
found,discard = flag_finder_and_stripper(flag,1,arg_copy)
|
59
|
+
if found.length > 0
|
60
|
+
safe_args.help_requested = true
|
61
|
+
if found.length == 2
|
62
|
+
safe_args.help_requested_on = found[1]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
arg_copy = safe_args.clone
|
67
|
+
|
68
|
+
ext_help_flag = @all_flags.values.select{|value| value.for_extended_help }
|
69
|
+
if ext_help_flag.length > 0
|
70
|
+
flag = ext_help_flag[0].as_the_form_that_is_actually_used
|
71
|
+
|
72
|
+
found,discard = flag_finder_and_stripper(flag,0,arg_copy)
|
73
|
+
if found.length > 0
|
74
|
+
safe_args.extended_help_requested = true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
def flag_finder_and_stripper(flag,arity,args)
|
79
|
+
idx = args.index(flag)
|
80
|
+
if idx == nil
|
81
|
+
return [], args
|
82
|
+
end
|
83
|
+
the_range = idx..(idx + arity)
|
84
|
+
flag_and_values = args[the_range]
|
85
|
+
fragment = args[the_range].clone
|
86
|
+
args[the_range] = nil
|
87
|
+
return fragment, args
|
88
|
+
end
|
89
|
+
def populate_values(safe_args)
|
90
|
+
find_the_flag_that_is_actually_used(safe_args)
|
91
|
+
arg_copy = safe_args.clone
|
92
|
+
@all_flags.values.each do |flag_obj|
|
93
|
+
the_string_flag = flag_obj.as_the_form_that_is_actually_used
|
94
|
+
flag_and_values, arg_copy =
|
95
|
+
flag_finder_and_stripper(the_string_flag,flag_obj.the_arity,arg_copy)
|
96
|
+
if flag_and_values.length >2
|
97
|
+
discard,*theRest = flag_and_values
|
98
|
+
flag_obj.value = theRest
|
99
|
+
end
|
100
|
+
flag_obj.value = flag_and_values[1] if flag_and_values.length ==2
|
101
|
+
flag_obj.value = true if flag_and_values.length == 1 and flag_obj.the_arity == 0
|
102
|
+
if flag_and_values.length == 1 and flag_obj.the_arity >0
|
103
|
+
problem = "Argument(s) missing for flag #{ flag_obj.as_the_form_that_is_actually_used }"
|
104
|
+
safe_args.errors ||= Errors.new
|
105
|
+
safe_args.errors.validation_errors << problem
|
106
|
+
end
|
107
|
+
end
|
108
|
+
if arg_copy.length > 0 # is there anything left over
|
109
|
+
safe_args.warnings ||= []
|
110
|
+
safe_args.warnings <<
|
111
|
+
"There are extra arguments left over: [#{ arg_copy.join(', ') }]. "
|
112
|
+
end
|
113
|
+
validate_values(safe_args)
|
114
|
+
find_help_flags(safe_args)
|
115
|
+
|
116
|
+
return safe_args
|
117
|
+
end
|
118
|
+
def validate_values(safe_args)
|
119
|
+
run_pre_translate(safe_args)
|
120
|
+
# validate generic now handles all validations
|
121
|
+
validate_generic(safe_args)
|
122
|
+
run_post_translate(safe_args)
|
123
|
+
end
|
124
|
+
# find all flags the have validation rules assigned to them
|
125
|
+
# i.e. any flag that has values set in the array
|
126
|
+
# the_validation_rules. Each value in this array is a
|
127
|
+
# proc/lambda/block that accepts two arguments: the flag
|
128
|
+
# of class EachClass and the errors array.
|
129
|
+
def validate_generic(safe_args)
|
130
|
+
flags_requiring_validation = @all_flags.values.select do |x|
|
131
|
+
x.the_validation_rules.length > 0 && ( x.value != nil)
|
132
|
+
end
|
133
|
+
flags_requiring_validation.each do |flag_obj|
|
134
|
+
value = flag_obj.value
|
135
|
+
flag_obj.the_validation_rules.each do |proc|
|
136
|
+
errors = safe_args.errors
|
137
|
+
errors ||= Errors.new
|
138
|
+
proc.call flag_obj, errors.validation_errors
|
139
|
+
if errors.validation_errors.length > 0
|
140
|
+
safe_args.errors = errors
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
def run_pre_translate(safe_args)
|
146
|
+
# find all flags that require pre-translation, i.e.
|
147
|
+
# all flags that 1) have a value and 2) have the the_pretranslate
|
148
|
+
# attribute flagged as on
|
149
|
+
flags_requiring_pre_translating = @all_flags.values.select do |x|
|
150
|
+
x.the_pretranslate && x.value
|
151
|
+
end
|
152
|
+
# the symbol passed as the second argument is the means by which we
|
153
|
+
# fetch the translation block
|
154
|
+
standard_translating(flags_requiring_pre_translating,:the_pretranslate)
|
155
|
+
end
|
156
|
+
def run_post_translate(safe_args)
|
157
|
+
# find all flags that require post-translation, i.e.
|
158
|
+
# all flags that 1) have a value and 2) have the the_posttranslate
|
159
|
+
# attribute flagged as on
|
160
|
+
flags_requiring_post_translating = @all_flags.values.select do |x|
|
161
|
+
x.the_posttranslate && x.value
|
162
|
+
end
|
163
|
+
standard_translating(flags_requiring_post_translating,:the_posttranslate)
|
164
|
+
end
|
165
|
+
def standard_translating(arr,pre_or_post)
|
166
|
+
arr.each do |flag|
|
167
|
+
flag.send(pre_or_post).each_with_index do |translate,idx|
|
168
|
+
the_value = flag.value
|
169
|
+
the_value = [the_value] if the_value.class != Array
|
170
|
+
if translate.arity > 1
|
171
|
+
retVal = translate.call *the_value
|
172
|
+
retVal ||= []
|
173
|
+
retVal = [retVal] if retVal.class !=Array
|
174
|
+
if retVal.length != translate.arity
|
175
|
+
raise "Error: the translate block you used had #{ translate.arity } arguments, but your block returned #{ retVal.length } values. They must be equal."
|
176
|
+
end
|
177
|
+
flag.value = retVal
|
178
|
+
elsif translate.arity == 1
|
179
|
+
retVal = translate.call(the_value[idx])
|
180
|
+
flag.value[idx] = retVal
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
# private method
|
186
|
+
def find_the_flag_that_is_actually_used(safe_args)
|
187
|
+
there_might_be_errors = safe_args.errors || Errors.new
|
188
|
+
args_copy = safe_args.clone
|
189
|
+
@all_flags.values.each do |x|
|
190
|
+
shortform,longform = x.as_string_basic,x.as_string_extended
|
191
|
+
all_forms = [shortform,longform] + x.as_alternate_forms
|
192
|
+
form_found_mask = all_forms.collect do |form|
|
193
|
+
is_form_found, args_copy =
|
194
|
+
flag_finder_and_stripper(form,x.the_arity,args_copy)
|
195
|
+
[ (is_form_found.length > 0), is_form_found ]
|
196
|
+
end
|
197
|
+
any_found = form_found_mask.select{|(found,parms)| found}
|
198
|
+
if any_found.length > 1
|
199
|
+
there_might_be_errors.other_errors <<
|
200
|
+
"More than one flag form of -- is present. This is ambiguous. Choose one only."
|
201
|
+
end
|
202
|
+
if any_found.length == 1
|
203
|
+
x.the_form_that_is_actually_used = any_found[0][1][0]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
if there_might_be_errors.any_errors?
|
207
|
+
safe_args.errors = there_might_be_errors
|
208
|
+
end
|
209
|
+
return safe_args
|
210
|
+
end
|
211
|
+
def search_for_missing_character_switches(safe_args)
|
212
|
+
these_args = safe_args.clone
|
213
|
+
return safe_args if @group == nil
|
214
|
+
chars_found_for_this_group = {}
|
215
|
+
all_chars = ""
|
216
|
+
@group.each_pair do |k,val|
|
217
|
+
name_of_flag = val.collect{|x| x.name}
|
218
|
+
all_chars_alphabetical = name_of_flag.join('').unpack('c*').sort.pack('c*')
|
219
|
+
args_namespaced = these_args.select{|x| x.match("^#{ k[1] }") }
|
220
|
+
seems_to_match = []
|
221
|
+
args_namespaced.each do |flag|
|
222
|
+
flag_value = flag.match("^#{ k[1]}").post_match
|
223
|
+
potential = all_chars_alphabetical.tr(flag_value,"")
|
224
|
+
if potential.length == all_chars_alphabetical.length - flag_value.length
|
225
|
+
seems_to_match << flag_value
|
226
|
+
all_chars_alphabetical = all_chars_alphabetical.tr(flag_value,"")
|
227
|
+
these_args = these_args - [flag]
|
228
|
+
end
|
229
|
+
end
|
230
|
+
seems_to_match = seems_to_match.flatten.join('')
|
231
|
+
chars_found_for_this_group[k] = seems_to_match
|
232
|
+
all_chars << seems_to_match
|
233
|
+
end
|
234
|
+
all_chars.split(//).each do |x|
|
235
|
+
opt_flag = @all_flags[x.to_sym]
|
236
|
+
opt_flag.value = true
|
237
|
+
end
|
238
|
+
|
239
|
+
return safe_args
|
240
|
+
end
|
241
|
+
|
242
|
+
def search_for_missing_flags(safe_args)
|
243
|
+
there_might_be_errors = Errors.new
|
244
|
+
required_flags = @all_flags.values.sort do |x,y|
|
245
|
+
x.order_added <=> y.order_added
|
246
|
+
end.select{ |x| x.the_is_required }
|
247
|
+
args_copy = safe_args.clone
|
248
|
+
required_flags.each do |x|
|
249
|
+
shortform,longform = x.as_string_basic,x.as_string_extended
|
250
|
+
all_forms = [shortform,longform] + x.as_alternate_forms
|
251
|
+
|
252
|
+
form_found_mask = all_forms.collect do |form|
|
253
|
+
is_form_found, args_copy =
|
254
|
+
flag_finder_and_stripper(form,x.the_arity,args_copy)
|
255
|
+
[ (is_form_found.length > 0), is_form_found ]
|
256
|
+
end
|
257
|
+
is_first_found,is_second_found = form_found_mask
|
258
|
+
any_found = form_found_mask.select{|(found,parms)| found}
|
259
|
+
if any_found.length == 0
|
260
|
+
there_might_be_errors.missing_flags << x.as_string_basic
|
261
|
+
end
|
262
|
+
if is_second_found[0] && is_first_found[0]
|
263
|
+
there_might_be_errors.other_errors <<
|
264
|
+
"Both forms #{ x.as_string_basic } and #{x.as_string_extended } are present. This is ambiguous. Choose one only."
|
265
|
+
end
|
266
|
+
if any_found.length == 1
|
267
|
+
x.the_form_that_is_actually_used = any_found[0][1][0]
|
268
|
+
end
|
269
|
+
end
|
270
|
+
if there_might_be_errors.any_errors?
|
271
|
+
safe_args.errors = there_might_be_errors
|
272
|
+
end
|
273
|
+
return safe_args
|
274
|
+
end
|
275
|
+
def create_api(safe_args)
|
276
|
+
safe_args.extend NewInterface
|
277
|
+
safe_args.flag_value = create_new_value_class()
|
278
|
+
return safe_args
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|