cmdparse 1.0.5 → 2.0.0

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.
@@ -0,0 +1,65 @@
1
+ #
2
+ #--
3
+ #
4
+ # $Id: optparse.rb 329 2005-08-14 15:39:05Z thomas $
5
+ #
6
+ # cmdparse: advanced command line parser supporting commands
7
+ # Copyright (C) 2004 Thomas Leitner
8
+ #
9
+ # This program is free software; you can redistribute it and/or modify it under the terms of the GNU
10
+ # General Public License as published by the Free Software Foundation; either version 2 of the
11
+ # License, or (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14
+ # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License along with this program; if not,
18
+ # write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+ #
22
+ #
23
+
24
+ require 'optparse'
25
+
26
+ # Some extension to the standard option parser class
27
+ class OptionParser
28
+
29
+ if const_defined?( 'Officious' )
30
+ Officious.delete( 'version' )
31
+ Officious.delete( 'help' )
32
+ else
33
+ DefaultList.long.delete( 'version' )
34
+ DefaultList.long.delete( 'help' )
35
+ end
36
+
37
+ end
38
+
39
+ module CmdParse
40
+
41
+ # Parser wrapper for OptionParser (included in Ruby Standard Library).
42
+ class OptionParserWrapper < ParserWrapper
43
+
44
+ # Initializes the wrapper with a default OptionParser instance or the +parser+ parameter and
45
+ # yields this instance.
46
+ def initialize( parser = OptionParser.new, &block )
47
+ @instance = parser
48
+ self.instance( &block )
49
+ end
50
+
51
+ def order( args )
52
+ @instance.order( args )
53
+ end
54
+
55
+ def permute( args )
56
+ @instance.permute( args )
57
+ end
58
+
59
+ def summarize
60
+ @instance.summarize
61
+ end
62
+
63
+ end
64
+
65
+ end
data/net.rb ADDED
@@ -0,0 +1,88 @@
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 )
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 )
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 ) ) }
metadata CHANGED
@@ -3,17 +3,18 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: cmdparse
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.5
7
- date: 2005-07-05
8
- summary: An advanced command line parser using optparse which supports commands
6
+ version: 2.0.0
7
+ date: 2005-08-16
8
+ summary: Advanced command line parser supporting commands
9
9
  require_paths:
10
10
  - lib
11
11
  email: t_leitner@gmx.at
12
12
  homepage: http://cmdparse.rubyforge.org
13
13
  rubyforge_project: cmdparse
14
- description: "cmdparse extends the default option parser 'optparse' by adding support for
15
- commands. Programs that use such command line interfaces are, for example,
16
- subversion's 'svn' or Rubygem's 'gem' program."
14
+ description: "cmdparse provides classes for parsing commands on the command line; command line
15
+ options are parsed using optparse or any other option parser implementation.
16
+ Programs that use such command line interfaces are, for example, subversion's
17
+ 'svn' or Rubygem's 'gem' program."
17
18
  autorequire:
18
19
  default_executable:
19
20
  bindir: bin
@@ -35,10 +36,12 @@ files:
35
36
  - README
36
37
  - Rakefile
37
38
  - ChangeLog
38
- - test.rb
39
+ - net.rb
39
40
  - VERSION
40
41
  - lib/cmdparse.rb
42
+ - lib/cmdparse/wrappers/optparse.rb
41
43
  - doc/src
44
+ - doc/config.yaml
42
45
  - doc/plugin
43
46
  - doc/src/default.css
44
47
  - doc/src/features.page
@@ -54,9 +57,8 @@ test_files: []
54
57
  rdoc_options:
55
58
  - "--line-numbers"
56
59
  - "-m"
57
- - README
58
- extra_rdoc_files:
59
- - README
60
+ - CmdParse::CommandParser
61
+ extra_rdoc_files: []
60
62
  executables: []
61
63
  extensions: []
62
64
  requirements: []
data/test.rb DELETED
@@ -1,42 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # if something is changed here -> change line numbers in doc/src/documentation.page
3
- $:.unshift "lib"
4
- require 'cmdparse'
5
- require 'ostruct'
6
-
7
-
8
- class TestCommand < CommandParser::Command
9
- def initialize
10
- super('test')
11
- @internal = OpenStruct.new
12
- @internal.function = nil
13
- @internal.audible = false
14
- options.separator "Options:"
15
- options.on("-t", "--test FUNCTION", "Test only FUNCTION") do |func|
16
- @internal.function = func
17
- end
18
- options.on("-a", "--[no-]audible", "Run audible") { |@internal.audible| }
19
- end
20
- def description
21
- "Executes various tests"
22
- end
23
- def execute( commandParser, args )
24
- puts "Additional arguments: "+ args.inspect
25
- puts "Internal values: " + @internal.inspect
26
- end
27
- end
28
-
29
- cmd = CommandParser.new(true)
30
- cmd.options do |opt|
31
- opt.program_name = "test.rb"
32
- opt.version = [0, 1, 0]
33
- opt.release = "1.0"
34
- opt.separator "Global options:"
35
- opt.on("-r", "--require TEST", "Require the TEST") {|t| puts "required: #{t}"}
36
- opt.on("--delay N", Integer, "Delay test for N seconds before executing") {|d| puts "delay: #{d}"}
37
- end
38
- cmd.add_command TestCommand.new, true
39
- cmd.add_command CommandParser::HelpCommand.new
40
- cmd.add_command CommandParser::VersionCommand.new
41
- cmd.parse!( ARGV, false )
42
- cmd.execute