optiflag 0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/ReleaseNotes.txt +110 -0
- data/doc/example/example_1.rb +30 -0
- data/doc/example/example_1_1.rb +27 -0
- data/doc/example/example_1_2.rb +28 -0
- data/doc/example/example_2.rb +28 -0
- data/doc/example/example_2_1.rb +23 -0
- data/doc/example/example_2_2.rb +31 -0
- data/doc/example/example_2_3.rb +37 -0
- data/doc/example/example_2_4.rb +37 -0
- data/doc/example/example_2_5.rb +28 -0
- data/doc/example/example_2_6.rb +22 -0
- data/doc/example/example_3.rb +33 -0
- data/doc/example/example_4.rb +12 -0
- data/doc/example/example_5.rb +26 -0
- data/doc/example/example_6.rb +17 -0
- data/doc/example/example_7.rb +39 -0
- data/lib/optiflag.rb +860 -0
- data/optiflag.gemspec +13 -0
- data/test/tc_advanced_usage_helping.rb +42 -0
- data/test/tc_basic_alternate_forms.rb +30 -0
- data/test/tc_basic_char_flags.rb +103 -0
- data/test/tc_basic_optional_flag.rb +74 -0
- data/test/tc_basic_parsing.rb +51 -0
- data/test/tc_basic_usage_helping.rb +40 -0
- data/test/tc_basic_value_validation.rb +40 -0
- data/test/tc_bug_one.rb +25 -0
- data/test/tc_bug_two.rb +33 -0
- data/test/tc_change_symbols.rb +61 -0
- data/test/tc_enumerated_value_validation.rb +35 -0
- data/test/tc_flagall.rb +17 -0
- data/test/tc_flagless_arg.rb +37 -0
- data/test/tc_keyword.rb +37 -0
- data/test/tc_values_as_hash.rb +46 -0
- metadata +95 -0
data/optiflag.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rake'
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "optiflag"
|
4
|
+
spec.version = "0.6"
|
5
|
+
spec.summary = "Optiflag is yet-another library for handling command-line options in an arbitrary Ruby program."
|
6
|
+
spec.author = "Daniel Eklund"
|
7
|
+
spec.email = "doeklund@gmail.com"
|
8
|
+
spec.homepage = "http://rubyforge.org/projects/optiflag/"
|
9
|
+
spec.files = FileList['**/*'].to_a
|
10
|
+
spec.test_files = FileList['test/tc*'].to_a - ["test/tc_flagall.rb"]
|
11
|
+
spec.has_rdoc = true
|
12
|
+
spec.rubyforge_project = 'optiflag'
|
13
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module HelpArgs extend OptiFlag::Flagset
|
5
|
+
usage_flag "h","?","help"
|
6
|
+
flag "dir"
|
7
|
+
end
|
8
|
+
|
9
|
+
class TC_AdvancedHelpArgs < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_help_requested
|
12
|
+
command_lines =
|
13
|
+
["-dir thedire -? dir",
|
14
|
+
"-dir thedire -h dir",
|
15
|
+
"-dir thedire -help dir"]
|
16
|
+
command_lines.each do |cl|
|
17
|
+
argv = cl.split
|
18
|
+
args = HelpArgs::parse(argv)
|
19
|
+
assert(args.help_requested?,
|
20
|
+
"A help flag was added to the command-line. Please register its existence.")
|
21
|
+
assert_equal("dir",args.help_requested_on,
|
22
|
+
"Advanced help is supposed to be requested on 'dir'")
|
23
|
+
assert_equal("thedire",args.flag_value.dir,
|
24
|
+
"thedire is the proper value of the dir flag")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
def test_no_help_requested
|
28
|
+
command_lines =
|
29
|
+
["-dir thedire",
|
30
|
+
"-dir thedire -he",
|
31
|
+
"-dir thedire -hElp"]
|
32
|
+
command_lines.each do |cl|
|
33
|
+
argv = cl.split
|
34
|
+
args = HelpArgs::parse(argv)
|
35
|
+
assert_equal("thedire",args.flag_value.dir,
|
36
|
+
"thedire is the proper value of the dir flag")
|
37
|
+
|
38
|
+
assert(! args.help_requested?,
|
39
|
+
"There are no help flags on the command line")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module AlternateArgs extend OptiFlag::Flagset
|
5
|
+
flag "directory" do
|
6
|
+
alternate_forms %w{d dir D DIR DIRECTORY}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
class TC_AlternateArgs < Test::Unit::TestCase
|
13
|
+
|
14
|
+
|
15
|
+
def test_alternate_forms
|
16
|
+
command_lines =
|
17
|
+
["-d FLOTSAM","-dir FLOTSAM",
|
18
|
+
"--directory FLOTSAM",
|
19
|
+
"-DIR FLOTSAM","-DIRECTORY FLOTSAM",
|
20
|
+
"-directory FLOTSAM","-D FLOTSAM"]
|
21
|
+
command_lines.each do |command_line|
|
22
|
+
args = AlternateArgs::parse(command_line.split)
|
23
|
+
assert_equal("FLOTSAM",args.flag_value.directory,
|
24
|
+
"Directory not properly set")
|
25
|
+
assert(! args.errors?,
|
26
|
+
"No errors should have occurred")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
|
5
|
+
module CharArgs extend OptiFlag::Flagset
|
6
|
+
character_flag :l, :p_group
|
7
|
+
character_flag :s, :p_group
|
8
|
+
character_flag :a, :p_group
|
9
|
+
optional_switch_flag "clear"
|
10
|
+
character_flag :x do
|
11
|
+
description "Extract"
|
12
|
+
end
|
13
|
+
character_flag :v
|
14
|
+
character_flag :f
|
15
|
+
flag "dir"
|
16
|
+
keyword "bobby"
|
17
|
+
end
|
18
|
+
|
19
|
+
class TC_CharArgs < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def test_xvf
|
22
|
+
command_lines = ["-x bobby -v -f -dir sdsdsd",
|
23
|
+
"-xvf",
|
24
|
+
"-vxf",
|
25
|
+
"-fvx bobby",
|
26
|
+
"-f -dir kosd -xv",
|
27
|
+
"-dir sdsd -x -fv",
|
28
|
+
"-x -vf",
|
29
|
+
"-f -vx",
|
30
|
+
"-v -f -x -vo sd"]
|
31
|
+
command_lines.each do |x|
|
32
|
+
argv = x.split
|
33
|
+
args = CharArgs::parse(argv)
|
34
|
+
assert_equal(true,args.flag_value.f?,
|
35
|
+
"The 'f' flag should be set")
|
36
|
+
assert_equal(true,args.flag_value.v?,
|
37
|
+
"The 'v' flag should be set")
|
38
|
+
assert_equal(true,args.flag_value.x?,
|
39
|
+
"The 'x' flag should be set")
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_lsa
|
45
|
+
command_lines = ["-x bobby -v -f -dir sdsdsd",
|
46
|
+
"-xvf",
|
47
|
+
"-vxf",
|
48
|
+
"-fvx bobby",
|
49
|
+
"-f -dir kosd -xv",
|
50
|
+
"-dir sdsd -x -fv",
|
51
|
+
"-x -vf",
|
52
|
+
"-f -vx",
|
53
|
+
"-v -f -x -vo sd"]
|
54
|
+
command_lines.each do |x|
|
55
|
+
argv = x.split
|
56
|
+
args = CharArgs::parse(argv)
|
57
|
+
assert_equal(false,args.flag_value.l?)
|
58
|
+
assert_equal(false,args.flag_value.s?)
|
59
|
+
assert_equal(false,args.flag_value.a?)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
def test_lsa_not_there
|
63
|
+
command_lines = ["-lsa",
|
64
|
+
"-las",
|
65
|
+
"-als",
|
66
|
+
"-asl",
|
67
|
+
"-sal",
|
68
|
+
"-sla",
|
69
|
+
"-s -la",
|
70
|
+
"-s -al",
|
71
|
+
"-a -sl",
|
72
|
+
"-a -ls ",
|
73
|
+
"-l -s -a",
|
74
|
+
"--l --s --a",
|
75
|
+
"-lsa bobby -dir sdsd",
|
76
|
+
"bobby -dir sdsd -las",
|
77
|
+
"-als bobby -dir sdsd",
|
78
|
+
"bobby -asl",
|
79
|
+
"-sal -dir sdsd",
|
80
|
+
"-sla",
|
81
|
+
"-s bobby -la",
|
82
|
+
"-s -al",
|
83
|
+
"-a bobby -sl",
|
84
|
+
"-a -ls",
|
85
|
+
"-l bobby -dir sdsd -s -a"]
|
86
|
+
|
87
|
+
command_lines.each do |x|
|
88
|
+
argv = x.split
|
89
|
+
args = CharArgs::parse(argv)
|
90
|
+
assert_equal(true,args.flag_value.l?)
|
91
|
+
assert_equal(true,args.flag_value.s?)
|
92
|
+
assert_equal(true,args.flag_value.a?)
|
93
|
+
assert_equal(false,args.flag_value.x?)
|
94
|
+
assert_equal(false,args.flag_value.v?)
|
95
|
+
assert_equal(false,args.flag_value.f?)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
end
|
103
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module OptionalArgs extend OptiFlag::Flagset
|
5
|
+
flag "dir"
|
6
|
+
optional_flag "log"
|
7
|
+
optional_flag "verbose_level" do
|
8
|
+
value_in_set [1,2,3,4,0]
|
9
|
+
end
|
10
|
+
optional_switch_flag "force"
|
11
|
+
end
|
12
|
+
|
13
|
+
class TC_OptionalArgs < Test::Unit::TestCase
|
14
|
+
|
15
|
+
def test_none_there
|
16
|
+
cl = %w{ -dir c:/my_stuff }
|
17
|
+
result = OptionalArgs::parse(cl)
|
18
|
+
assert( ! result.errors?)
|
19
|
+
assert_equal("c:/my_stuff",result.flag_value.dir)
|
20
|
+
assert(!result.flag_value.log?)
|
21
|
+
assert(!result.flag_value.verbose_level?)
|
22
|
+
assert(!result.flag_value.force?)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_all_there_and_no_errors
|
26
|
+
cl = %w{ -dir c:/my_stuff -log c:/log -force -verbose_level 3 }
|
27
|
+
result = OptionalArgs::parse(cl)
|
28
|
+
assert( ! result.errors?)
|
29
|
+
assert_equal("c:/log",result.flag_value.log)
|
30
|
+
assert_equal("3",result.flag_value.verbose_level)
|
31
|
+
assert(result.flag_value.force?)
|
32
|
+
end
|
33
|
+
def test_some_there_and_no_errors
|
34
|
+
cl = %w{ -dir c:/my_stuff -force -verbose_level 3 }
|
35
|
+
result = OptionalArgs::parse(cl)
|
36
|
+
assert( ! result.errors?)
|
37
|
+
assert(!result.flag_value.log?)
|
38
|
+
assert_equal(nil,result.flag_value.log)
|
39
|
+
assert_equal("3",result.flag_value.verbose_level)
|
40
|
+
assert(result.flag_value.force?)
|
41
|
+
end
|
42
|
+
def test_some_there_and_no_errors_rearranged_1
|
43
|
+
cl = %w{ -dir c:/my_stuff -verbose_level 3 -force}
|
44
|
+
result = OptionalArgs::parse(cl)
|
45
|
+
assert( ! result.errors?)
|
46
|
+
assert(!result.flag_value.log?)
|
47
|
+
assert_equal(nil,result.flag_value.log)
|
48
|
+
assert_equal("3",result.flag_value.verbose_level)
|
49
|
+
assert(result.flag_value.force?)
|
50
|
+
end
|
51
|
+
def test_some_there_and_no_errors_rearranged_2
|
52
|
+
cl = %w{ -force -dir c:/my_stuff -verbose_level 3 }
|
53
|
+
result = OptionalArgs::parse(cl)
|
54
|
+
assert( ! result.errors?)
|
55
|
+
assert(!result.flag_value.log?)
|
56
|
+
assert_equal(nil,result.flag_value.log)
|
57
|
+
assert_equal("3",result.flag_value.verbose_level)
|
58
|
+
assert(result.flag_value.force?)
|
59
|
+
end
|
60
|
+
def test_some_there_but_has_validation_error
|
61
|
+
cl = %w{ -dir c:/my_stuff -verbose_level three }
|
62
|
+
result = OptionalArgs::parse(cl)
|
63
|
+
assert( result.errors?)
|
64
|
+
assert(!result.flag_value.log?)
|
65
|
+
assert_equal(nil,result.flag_value.log)
|
66
|
+
assert(!result.flag_value.force?)
|
67
|
+
assert_equal(0,result.errors.missing_flags.length)
|
68
|
+
assert_equal(1,result.errors.validation_errors.length)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module DansArgs extend OptiFlag::Flagset
|
5
|
+
flag ["directory"]
|
6
|
+
flag "script"
|
7
|
+
flag ["control_db"] do
|
8
|
+
description "The control database credentials"
|
9
|
+
end
|
10
|
+
flag ["target_db"]
|
11
|
+
end
|
12
|
+
|
13
|
+
class TC_FlagAll < Test::Unit::TestCase
|
14
|
+
|
15
|
+
def setup
|
16
|
+
@regular_dans_args = %w{-directory my_directory -script my_script
|
17
|
+
-control_db control -target_db target}
|
18
|
+
@missing_dans_args = %w{-directory my_directory -script my_script}
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_regular_dans_arg_parses
|
22
|
+
result = DansArgs::parse(@regular_dans_args)
|
23
|
+
assert_equal("my_directory",result.flag_value.directory,
|
24
|
+
"Flag should be equal to my_directory")
|
25
|
+
assert(result.flag_value.directory?,
|
26
|
+
"Directory Flag should have been passed")
|
27
|
+
assert_equal("my_script",result.flag_value.script,
|
28
|
+
"Flag should be equal to my_script")
|
29
|
+
assert_equal("control",result.flag_value.control_db,
|
30
|
+
"Flag should be equal to control")
|
31
|
+
assert(result.errors? == false,"There should be no errors")
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_missing_dans_arg_parses
|
35
|
+
result = DansArgs::parse(@missing_dans_args)
|
36
|
+
assert( result.errors?,
|
37
|
+
"There should be errors, with two missing required options")
|
38
|
+
assert_equal("-control_db",result.errors.missing_flags[0],
|
39
|
+
"First Missing flag should be -control_db")
|
40
|
+
assert_equal("-target_db",result.errors.missing_flags[1],
|
41
|
+
"Second Missing flag should be -target_db")
|
42
|
+
assert_equal(2,result.errors.missing_flags.length,
|
43
|
+
"There should be two missing flags")
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module HelpArgs extend OptiFlag::Flagset
|
5
|
+
usage_flag "h","?","help"
|
6
|
+
flag "dir"
|
7
|
+
end
|
8
|
+
|
9
|
+
class TC_HelpArgs < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_help_requested
|
12
|
+
command_lines =
|
13
|
+
["-dir thedire -?",
|
14
|
+
"-dir thedire -h",
|
15
|
+
"-dir thedire -help"]
|
16
|
+
command_lines.each do |cl|
|
17
|
+
argv = cl.split
|
18
|
+
args = HelpArgs::parse(argv)
|
19
|
+
assert(args.help_requested?,
|
20
|
+
"A help flag was added to the command-line. Please register its existence.")
|
21
|
+
assert_equal("thedire",args.flag_value.dir,
|
22
|
+
"thedire is the proper value of the dir flag")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
def test_no_help_requested
|
26
|
+
command_lines =
|
27
|
+
["-dir thedire",
|
28
|
+
"-dir thedire -he",
|
29
|
+
"-dir thedire -hElp"]
|
30
|
+
command_lines.each do |cl|
|
31
|
+
argv = cl.split
|
32
|
+
args = HelpArgs::parse(argv)
|
33
|
+
assert_equal("thedire",args.flag_value.dir,
|
34
|
+
"thedire is the proper value of the dir flag")
|
35
|
+
|
36
|
+
assert(! args.help_requested?,
|
37
|
+
"There are no help flags on the command line")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module ValidArgs extend OptiFlag::Flagset
|
5
|
+
flag "directory"
|
6
|
+
flag "script", :description => "The Script directory"
|
7
|
+
flag "mode" do
|
8
|
+
value_matches /[0-9]/
|
9
|
+
end
|
10
|
+
flag ["date"] do
|
11
|
+
value_matches ["Must be of form mm\/dd\/yy",
|
12
|
+
/^[0-9]{2}\/[0-9]{2}\/[0-9]{2,4}$/]
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class TC_ValidArgs < Test::Unit::TestCase
|
19
|
+
|
20
|
+
|
21
|
+
def test_validation
|
22
|
+
command_line = %w{-directory c:/eu -script RunMe -mode 3 -date 12/23/2006}
|
23
|
+
args = ValidArgs::parse(command_line)
|
24
|
+
assert_equal("c:/eu",args.flag_value.directory,
|
25
|
+
"Directory not properly set")
|
26
|
+
assert(! args.errors?,
|
27
|
+
"No errors should have occurred")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_validation_with_errors
|
31
|
+
command_line = %w{-directory c:/eu -script RunMe -mode wrx -date monday}
|
32
|
+
args = ValidArgs::parse(command_line)
|
33
|
+
assert(args.errors?,
|
34
|
+
"An error should have occurred")
|
35
|
+
assert_equal(2,args.errors.validation_errors.length,
|
36
|
+
"There should be 2 problems")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
data/test/tc_bug_one.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module BUG_ONE extend OptiFlag::Flagset
|
5
|
+
flag "dir" do
|
6
|
+
alternate_forms "D","d"
|
7
|
+
end
|
8
|
+
flag "log"
|
9
|
+
flag "db", :description => "The database"
|
10
|
+
optional_flag "verbose"
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
class TC_BUG_ONE < Test::Unit::TestCase
|
16
|
+
|
17
|
+
def test_bug
|
18
|
+
command_line = "-log LOG -dir LOG -db DB"
|
19
|
+
args = BUG_ONE::parse(command_line.split)
|
20
|
+
assert(! args.errors?,
|
21
|
+
"No errors should have occurred")
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/test/tc_bug_two.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'optiflag.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
module BugTwoArgs extend OptiFlag::Flagset
|
5
|
+
flag "dir"
|
6
|
+
optional_flag "log"
|
7
|
+
optional_flag "verbose_level" do
|
8
|
+
value_in_set [1,2,3,4,0]
|
9
|
+
end
|
10
|
+
optional_switch_flag "force"
|
11
|
+
usage_flag "h"
|
12
|
+
end
|
13
|
+
|
14
|
+
class TC_BugTwo < Test::Unit::TestCase
|
15
|
+
|
16
|
+
|
17
|
+
def setup
|
18
|
+
# this thing is changing global state
|
19
|
+
cl = %w{ -dir c:/my_stuff -log c:/log -force -verbose_level 3 -h }
|
20
|
+
result = BugTwoArgs::parse(cl)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_a
|
24
|
+
cl = %w{ -dir c:/my_stuff }
|
25
|
+
result = BugTwoArgs::parse(cl)
|
26
|
+
assert(!result.help_requested?,"Help was not requested")
|
27
|
+
assert(!result.flag_value.log?)
|
28
|
+
|
29
|
+
assert(!result.flag_value.verbose_level?)
|
30
|
+
assert(!result.flag_value.force?)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|