cmdparse 2.0.6 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -1,63 +0,0 @@
1
- #
2
- #--
3
- # cmdparse: advanced command line parser supporting commands
4
- # Copyright (C) 2004-2014 Thomas Leitner
5
- #
6
- # This file is part of cmdparse.
7
- #
8
- # cmdparse is free software: you can redistribute it and/or modify it under the terms of the GNU
9
- # Lesser General Public License as published by the Free Software Foundation, either version 3 of
10
- # the License, or (at your option) any later version.
11
- #
12
- # cmdparse is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
13
- # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
14
- # General Public License for more details.
15
- #
16
- # You should have received a copy of the GNU Lesser General Public License along with cmdparse. If
17
- # not, see <http://www.gnu.org/licenses/>.
18
- #
19
- #++
20
- #
21
-
22
- require 'optparse'
23
-
24
- # Some extension to the standard option parser class
25
- class OptionParser
26
-
27
- if const_defined?('Officious')
28
- Officious.delete('version')
29
- Officious.delete('help')
30
- else
31
- DefaultList.long.delete('version')
32
- DefaultList.long.delete('help')
33
- end
34
-
35
- end
36
-
37
- module CmdParse
38
-
39
- # Parser wrapper for OptionParser (included in Ruby Standard Library).
40
- class OptionParserWrapper < ParserWrapper
41
-
42
- # Initializes the wrapper with a default OptionParser instance or the +parser+ parameter and
43
- # yields this instance.
44
- def initialize(parser = OptionParser.new, &block)
45
- @instance = parser
46
- self.instance(&block)
47
- end
48
-
49
- def order(args)
50
- @instance.order(args)
51
- end
52
-
53
- def permute(args)
54
- @instance.permute(args)
55
- end
56
-
57
- def summarize
58
- @instance.summarize
59
- end
60
-
61
- end
62
-
63
- end
data/net.rb DELETED
@@ -1,88 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # if something is changed here -> change line numbers in doc/src/documentation.page
3
-
4
- $:.unshift "lib"
5
- require 'cmdparse'
6
- require 'ostruct'
7
- require 'yaml'
8
-
9
- class NetStatCommand < CmdParse::Command
10
-
11
- def initialize
12
- super('stat', false)
13
- self.short_desc = "Show network statistics"
14
- self.description = "This command shows very useful network statistics - eye catching!!!"
15
- end
16
-
17
- def execute(args)
18
- puts "Showing network statistics" if $verbose
19
- puts
20
- puts "Yeah, I will do something now..."
21
- puts
22
- 1.upto(10) do |row|
23
- puts " "*(20-row) + "#"*(row*2 - 1)
24
- end
25
- puts
26
- end
27
-
28
- end
29
-
30
- cmd = CmdParse::CommandParser.new(true, true)
31
- cmd.program_name = "net"
32
- cmd.program_version = [0, 1, 1]
33
- cmd.options = CmdParse::OptionParserWrapper.new do |opt|
34
- opt.separator "Global options:"
35
- opt.on("--verbose", "Be verbose when outputting info") {|t| $verbose = true }
36
- end
37
- cmd.add_command(CmdParse::HelpCommand.new)
38
- cmd.add_command(CmdParse::VersionCommand.new)
39
- cmd.add_command(NetStatCommand.new)
40
-
41
- # ipaddr
42
- ipaddr = CmdParse::Command.new('ipaddr', true, true)
43
- ipaddr.short_desc = "Manage IP addresses"
44
- cmd.add_command(ipaddr)
45
-
46
- # ipaddr add
47
- add = CmdParse::Command.new('add', false)
48
- add.short_desc = "Add an IP address"
49
- add.set_execution_block do |args|
50
- puts "Adding ip addresses: #{args.join(', ')}" if $verbose
51
- $ipaddrs += args
52
- end
53
- ipaddr.add_command(add)
54
-
55
- # ipaddr del
56
- del = CmdParse::Command.new('del', false)
57
- del.short_desc = "Delete an IP address"
58
- del.options = CmdParse::OptionParserWrapper.new do |opt|
59
- opt.on('-a', '--all', 'Delete all IP addresses') { $deleteAll = true }
60
- end
61
- del.set_execution_block do |args|
62
- if $deleteAll
63
- $ipaddrs = []
64
- else
65
- puts "Deleting ip addresses: #{args.join(', ')}" if $verbose
66
- args.each {|ip| $ipaddrs.delete(ip) }
67
- end
68
- end
69
- ipaddr.add_command(del)
70
-
71
- # ipaddr list
72
- list = CmdParse::Command.new('list', false)
73
- list.short_desc = "Lists all IP addresses"
74
- list.set_execution_block do |args|
75
- puts "Listing ip addresses:" if $verbose
76
- puts $ipaddrs.to_yaml
77
- end
78
- ipaddr.add_command(list, true)
79
-
80
- if File.exists?('dumpnet')
81
- $ipaddrs = Marshal.load(File.read('dumpnet'))
82
- else
83
- $ipaddrs = []
84
- end
85
-
86
- cmd.parse
87
-
88
- File.open('dumpnet', 'w+') {|f| f.write(Marshal.dump($ipaddrs)) }
@@ -1,96 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'cmdparse'
3
-
4
- class CommandHashTest < Minitest::Test
5
- def setup
6
- @cmd = CmdParse::CommandHash.new
7
- end
8
-
9
- def test_basic
10
- assert_equal 0, @cmd.size
11
- assert_nil @cmd['import']
12
- @cmd['import'] = 1
13
- assert_equal 1, @cmd['import']
14
- assert_equal 1, @cmd['i']
15
- assert_equal 1, @cmd['im']
16
- assert_equal 1, @cmd['imp']
17
- assert_equal 1, @cmd['impo']
18
- assert_equal 1, @cmd['impor']
19
- assert_nil @cmd['importer']
20
-
21
- @cmd['implode'] = 2
22
- assert_equal 2, @cmd.size
23
-
24
- assert_equal 1, @cmd['import']
25
- assert_equal 2, @cmd['implode']
26
- assert_nil @cmd['impart']
27
-
28
- assert_nil @cmd['i']
29
- assert_nil @cmd['im']
30
- assert_nil @cmd['imp']
31
- assert_equal 1, @cmd['impo']
32
- assert_equal 1, @cmd['impor']
33
- assert_equal 1, @cmd['import']
34
- assert_equal 2, @cmd['implo']
35
- assert_equal 2, @cmd['implod']
36
- assert_equal 2, @cmd['implode']
37
- end
38
-
39
- def test_edge_cases
40
- @cmd['import'] = 1
41
- @cmd['important'] = 2
42
-
43
- assert_equal 1, @cmd['import']
44
- assert_equal 2, @cmd['important']
45
- assert_nil @cmd['i']
46
- assert_nil @cmd['im']
47
- assert_nil @cmd['imp']
48
- assert_nil @cmd['impo']
49
- assert_nil @cmd['impor']
50
- assert_equal 2, @cmd['importa']
51
- assert_equal 2, @cmd['importan']
52
-
53
- assert_nil @cmd['impart']
54
- end
55
-
56
- def test_integration
57
- # define and setup the commands
58
- cmd = CmdParse::CommandParser.new(handle_exceptions = false)
59
- cmd.main_command.use_partial_commands( true )
60
- Object.const_set(:ImportCommand, Class.new(CmdParse::Command) do
61
- def initialize() super('import', false) end
62
- def execute(args) raise 'import' end
63
- def show_help() raise 'import' end
64
- end)
65
- Object.const_set(:ImpolodeCommand, Class.new(CmdParse::Command) do
66
- def initialize() super('implode', false) end
67
- def execute(args) raise 'implode' end
68
- def show_help() raise 'implode' end
69
- end)
70
- cmd.add_command( ImportCommand.new )
71
- cmd.add_command( ImpolodeCommand.new )
72
-
73
- # simulate running the program
74
- assert_raises(RuntimeError, 'import') {cmd.parse(['import'])}
75
- assert_raises(RuntimeError, 'implode') {cmd.parse(['implode'])}
76
- assert_raises(CmdParse::InvalidCommandError) {cmd.parse(['impart'])}
77
-
78
- assert_raises(CmdParse::InvalidCommandError) {cmd.parse(['i'])}
79
- assert_raises(CmdParse::InvalidCommandError) {cmd.parse(['im'])}
80
- assert_raises(CmdParse::InvalidCommandError) {cmd.parse(['imp'])}
81
- assert_raises(RuntimeError, 'import') {cmd.parse(['impo'])}
82
- assert_raises(RuntimeError, 'import') {cmd.parse(['impor'])}
83
- assert_raises(RuntimeError, 'implode') {cmd.parse(['impl'])}
84
- assert_raises(RuntimeError, 'implode') {cmd.parse(['implo'])}
85
- assert_raises(RuntimeError, 'implode') {cmd.parse(['implod'])}
86
-
87
- # simulate the help command
88
- cmd.add_command( CmdParse::HelpCommand.new )
89
- assert_raises(RuntimeError, 'import') {cmd.parse(['help', 'import'])}
90
- assert_raises(RuntimeError, 'implode') {cmd.parse(['help', 'implode'])}
91
-
92
- cmd.main_command.use_partial_commands( false )
93
- assert_raises(CmdParse::InvalidCommandError, 'import') {cmd.parse(['impo'])}
94
- assert_raises(CmdParse::InvalidCommandError, 'implode') {cmd.parse(['impl'])}
95
- end
96
- end