cmdparse 2.0.5 → 2.0.6
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.
- checksums.yaml +7 -0
- data/README.md +59 -0
- data/Rakefile +94 -124
- data/VERSION +1 -1
- data/doc/{src/about.page → about.page} +8 -12
- data/doc/{src/default.css → default.css} +2 -1
- data/doc/{src/default.template → default.template} +4 -7
- data/doc/download.page +34 -0
- data/doc/index.page +107 -0
- data/doc/{src/logo.png → logo.png} +0 -0
- data/doc/tutorial.page +191 -0
- data/doc/virtual +5 -0
- data/lib/cmdparse.rb +2 -2
- data/lib/cmdparse/wrappers/optparse.rb +1 -1
- data/net.rb +19 -19
- data/setup.rb +861 -607
- data/test/tc_commandhash.rb +96 -0
- metadata +27 -31
- data/README +0 -55
- data/TODO +0 -10
- data/doc/config.yaml +0 -2
- data/doc/plugin/extract.rb +0 -29
- data/doc/src/download.page +0 -34
- data/doc/src/features.page +0 -11
- data/doc/src/index.page +0 -80
- data/doc/src/meta.info +0 -5
- data/doc/src/tutorial.page +0 -177
@@ -0,0 +1,96 @@
|
|
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
|
metadata
CHANGED
@@ -1,71 +1,67 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cmdparse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.6
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Thomas Leitner
|
9
8
|
autorequire: cmdparse
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-04-05 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
|
-
description:
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
description: |2
|
14
|
+
cmdparse provides classes for parsing commands on the command line; command line options
|
15
|
+
are parsed using optparse or any other option parser implementation. Programs that use
|
16
|
+
such command line interfaces are, for example, subversion's 'svn' or Rubygem's 'gem' program.
|
18
17
|
email: t_leitner@gmx.at
|
19
18
|
executables: []
|
20
19
|
extensions: []
|
21
20
|
extra_rdoc_files: []
|
22
21
|
files:
|
23
22
|
- setup.rb
|
24
|
-
- TODO
|
25
23
|
- COPYING
|
26
24
|
- COPYING.LESSER
|
27
|
-
- README
|
25
|
+
- README.md
|
28
26
|
- Rakefile
|
29
27
|
- net.rb
|
30
28
|
- VERSION
|
31
|
-
- lib/cmdparse/wrappers/optparse.rb
|
32
29
|
- lib/cmdparse.rb
|
33
|
-
-
|
34
|
-
- doc/
|
35
|
-
- doc/
|
36
|
-
- doc/
|
37
|
-
- doc/
|
38
|
-
- doc/
|
39
|
-
- doc/
|
40
|
-
- doc/
|
41
|
-
- doc/
|
42
|
-
-
|
43
|
-
|
44
|
-
|
45
|
-
|
30
|
+
- lib/cmdparse/wrappers/optparse.rb
|
31
|
+
- doc/about.page
|
32
|
+
- doc/default.css
|
33
|
+
- doc/default.template
|
34
|
+
- doc/download.page
|
35
|
+
- doc/index.page
|
36
|
+
- doc/logo.png
|
37
|
+
- doc/tutorial.page
|
38
|
+
- doc/virtual
|
39
|
+
- test/tc_commandhash.rb
|
40
|
+
homepage: http://cmdparse.gettalong.org
|
41
|
+
licenses:
|
42
|
+
- LGPLv3
|
43
|
+
metadata: {}
|
46
44
|
post_install_message:
|
47
45
|
rdoc_options:
|
48
46
|
- --line-numbers
|
49
|
-
-
|
47
|
+
- --main
|
50
48
|
- CmdParse::CommandParser
|
51
49
|
require_paths:
|
52
50
|
- lib
|
53
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
52
|
requirements:
|
56
|
-
- -
|
53
|
+
- - '>='
|
57
54
|
- !ruby/object:Gem::Version
|
58
55
|
version: '0'
|
59
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
-
none: false
|
61
57
|
requirements:
|
62
|
-
- -
|
58
|
+
- - '>='
|
63
59
|
- !ruby/object:Gem::Version
|
64
60
|
version: '0'
|
65
61
|
requirements: []
|
66
|
-
rubyforge_project:
|
67
|
-
rubygems_version:
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.3
|
68
64
|
signing_key:
|
69
|
-
specification_version:
|
65
|
+
specification_version: 4
|
70
66
|
summary: Advanced command line parser supporting commands
|
71
67
|
test_files: []
|
data/README
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
cmdparse - an advanced command line parser using optparse which has support for commands
|
2
|
-
|
3
|
-
Copyright (C) 2004-2012 Thomas Leitner
|
4
|
-
|
5
|
-
= Description
|
6
|
-
|
7
|
-
Some new programs use a "command style" command line. Examples for such programs are the "svn"
|
8
|
-
program from Subversion and the "gem" program from Rubygems. The standard Ruby distribution has no
|
9
|
-
library to create programs that use such a command line interface.
|
10
|
-
|
11
|
-
This library, cmdparse, can be used to create such a command line interface. Internally it uses
|
12
|
-
optparse or any other option parser library to parse options and it provides a nice API for
|
13
|
-
specifying commands.
|
14
|
-
|
15
|
-
= License
|
16
|
-
|
17
|
-
GNU LGPLv3 - see COPYING.LESSER for the LGPL and COPYING for the GPL
|
18
|
-
|
19
|
-
= Dependencies
|
20
|
-
|
21
|
-
none
|
22
|
-
|
23
|
-
= Installation
|
24
|
-
|
25
|
-
The preferred way of installing cmdparse is via RubyGems:
|
26
|
-
$ gem install cmdparse
|
27
|
-
|
28
|
-
If you do not have RubyGems installed, but Rake, you can use the following command:
|
29
|
-
$ rake install
|
30
|
-
|
31
|
-
If you have neither RubyGems nor Rake, use these commands:
|
32
|
-
$ ruby setup.rb config
|
33
|
-
$ ruby setup.rb setup
|
34
|
-
$ ruby setup.rb install
|
35
|
-
|
36
|
-
= Documentation
|
37
|
-
|
38
|
-
You can build the documentation by invoking
|
39
|
-
$ rake doc
|
40
|
-
|
41
|
-
This builds the API and the additional documentation. The additional documentation needs webgen
|
42
|
-
>=0.3.5 (webgen.rubyforge.org) for building.
|
43
|
-
|
44
|
-
|
45
|
-
= Example
|
46
|
-
|
47
|
-
There is an example of how to use cmdparse in the +net.rb+ file.
|
48
|
-
|
49
|
-
|
50
|
-
= Contact
|
51
|
-
|
52
|
-
Author: Thomas Leitner
|
53
|
-
* Web: http://cmdparse.rubyforge.org
|
54
|
-
* e-Mail: t_leitner@gmx.at
|
55
|
-
* GPG Key-Id: 0xD942E7F6
|
data/TODO
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
* see mail from Markus Werner concerning class methods for defining methods as sub commands
|
2
|
-
|
3
|
-
---- DONE ----
|
4
|
-
|
5
|
-
* define banner in a common way (like option parser), maybe use the one from option parser
|
6
|
-
* raise NoCommandGivenError when no command was given
|
7
|
-
* ability to specify default command (used when no command was given)
|
8
|
-
* add order,order!,... methods to mimic optparse
|
9
|
-
* optionally capture error messages and provide friendlier output
|
10
|
-
* if command has subcommands, use order! method, if has arguments use permute! (=POSIXLY_CORRECT); same with getoptlong
|
data/doc/config.yaml
DELETED
data/doc/plugin/extract.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
module CmdparsePlugins
|
2
|
-
|
3
|
-
class ExtractTag < Tags::DefaultTag
|
4
|
-
|
5
|
-
summary "Extracts lines from a file"
|
6
|
-
depends_on 'Tags'
|
7
|
-
add_param 'file', nil, 'the file from which to read the lines'
|
8
|
-
add_param 'lines', nil, 'the lines which should be read (Range)'
|
9
|
-
set_mandatory 'file'
|
10
|
-
set_mandatory 'lines'
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
super
|
14
|
-
@processOutput = false
|
15
|
-
register_tag( 'extract' )
|
16
|
-
end
|
17
|
-
|
18
|
-
def process_tag( tag, node, refNode )
|
19
|
-
data = File.readlines( get_param( 'file' ) ).unshift( 'empty null line' )[get_param( 'lines' )]
|
20
|
-
out = '<table class="cmdparse-example"><tr>'
|
21
|
-
out << '<td><pre>' << get_param( 'lines' ).collect {|n| "#{n}<br />" }.to_s << '</pre></td>'
|
22
|
-
out << '<td><pre>' << data.collect {|line| "#{CGI::escapeHTML(line)}" }.to_s << '</pre></td>'
|
23
|
-
out << '</tr></table>'
|
24
|
-
out
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
data/doc/src/download.page
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Download & Installation
|
3
|
-
inMenu: true
|
4
|
-
orderInfo: 4
|
5
|
-
---
|
6
|
-
h2. Download
|
7
|
-
|
8
|
-
The newest version of cmdparse can be downloaded from Rubyforge.
|
9
|
-
|
10
|
-
Homepage: "cmdparse.rubyforge.org":http://cmdparse.rubyforge.org <br />
|
11
|
-
Download: "rubyforge.org/frs/?group_id=396":http://rubyforge.org/frs/?group_id=396
|
12
|
-
|
13
|
-
h2. Dependencies
|
14
|
-
|
15
|
-
none
|
16
|
-
|
17
|
-
h2. Installation
|
18
|
-
|
19
|
-
The preferred way of installing cmdparse is via RubyGems:
|
20
|
-
<pre>
|
21
|
-
$ gem install cmdparse
|
22
|
-
</pre>
|
23
|
-
|
24
|
-
If you do not have RubyGems installed, but Rake, you can use the following command:
|
25
|
-
<pre>
|
26
|
-
$ rake install
|
27
|
-
</pre>
|
28
|
-
|
29
|
-
If you have neither RubyGems nor Rake, use these commands:
|
30
|
-
<pre>
|
31
|
-
$ ruby setup.rb config
|
32
|
-
$ ruby setup.rb setup
|
33
|
-
$ ruby setup.rb install
|
34
|
-
</pre>
|
data/doc/src/features.page
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Features
|
3
|
-
inMenu: true
|
4
|
-
orderInfo: 3
|
5
|
-
---
|
6
|
-
h2. Feature list
|
7
|
-
|
8
|
-
* Commands can have subcommands
|
9
|
-
* Default commands for showing help and the version of the program
|
10
|
-
* Option parsing library of choice can be used (after writing wrapper), default is @optparse@
|
11
|
-
* Easy to use
|
data/doc/src/index.page
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: HomePage
|
3
|
-
inMenu: true
|
4
|
-
directoryName: cmdparse
|
5
|
-
menuOrder: 1
|
6
|
-
---
|
7
|
-
h2. Welcome
|
8
|
-
|
9
|
-
... to the homepage of _*cmdparse*_, an advanced command line parser supporting commands.
|
10
|
-
|
11
|
-
Have a look around the site!
|
12
|
-
|
13
|
-
h2. News
|
14
|
-
|
15
|
-
<b>2006-06-17 cmdparse 2.0.2 released!!!</b>
|
16
|
-
|
17
|
-
Changes:
|
18
|
-
|
19
|
-
* Included two patches from Assaph Mehr:
|
20
|
-
* partial command matching can now be used (see <a href="{relocatable: tutorial.page}">tutorial page</a>)
|
21
|
-
* now a banner for the help and version commands can be specified
|
22
|
-
|
23
|
-
<b>2006-04-05 cmdparse 2.0.1 released!!!</b>
|
24
|
-
|
25
|
-
Changes:
|
26
|
-
|
27
|
-
* Just a bug fix release for those using cmdparse with Rubygems. By issuing the command
|
28
|
-
@require_gem 'cmdparse'@ the cmdparse library gets automagically loaded.
|
29
|
-
* Minor documentation updates
|
30
|
-
|
31
|
-
<b>2005-08-16 cmdparse 2.0.0 released!!!</b>
|
32
|
-
|
33
|
-
This version is not compatible to previous versions of cmdparse as there have been major changes in
|
34
|
-
the API. However, updating your code to use the new API is very easy!
|
35
|
-
|
36
|
-
Changes:
|
37
|
-
|
38
|
-
* Commands can now have subcommands which can have subcommands which can have subcommands...
|
39
|
-
* No need to implement a whole new class for simple commands anymore
|
40
|
-
* Default option parser library is @optparse@, however, any option parser library can be used after writing a small wrapper
|
41
|
-
|
42
|
-
<b>2005-07-05 cmdparse 1.0.5 released!!!</b>
|
43
|
-
|
44
|
-
Changes:
|
45
|
-
|
46
|
-
* added possibility to parse global options, command and local options separately
|
47
|
-
|
48
|
-
|
49
|
-
<b>2005-06-16 cmdparse 1.0.4 released!!!</b>
|
50
|
-
|
51
|
-
Changes:
|
52
|
-
|
53
|
-
* fix for older ruby versions
|
54
|
-
* fixed bug where exception was not caught
|
55
|
-
|
56
|
-
|
57
|
-
<b>2005-06-09 cmdparse 1.0.3 released!!!</b>
|
58
|
-
|
59
|
-
Changes:
|
60
|
-
|
61
|
-
* added optional graceful exception handling
|
62
|
-
|
63
|
-
|
64
|
-
<b>2005-04-21 cmdparse 1.0.2 released!!!</b>
|
65
|
-
|
66
|
-
Changes:
|
67
|
-
|
68
|
-
* splitted parsing of the arguments and executing the command into two methods
|
69
|
-
|
70
|
-
<b>2005-04-13 cmdparse 1.0.1 released!!!</b>
|
71
|
-
|
72
|
-
Changes:
|
73
|
-
|
74
|
-
* Improved HelpCommand: the global options -h and --help take an optional command name now
|
75
|
-
* Possibility to define a default command which is used when no command was specified
|
76
|
-
* A @NoCommandGivenError@ is thrown when no command on the CLI and no default command was specified
|
77
|
-
|
78
|
-
h2. And so
|
79
|
-
|
80
|
-
... have fun!
|
data/doc/src/meta.info
DELETED
data/doc/src/tutorial.page
DELETED
@@ -1,177 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title: Tutorial
|
3
|
-
inMenu: true
|
4
|
-
orderInfo: 6
|
5
|
-
---
|
6
|
-
h2. Quickstart
|
7
|
-
|
8
|
-
Here are some short code fragments which show how to use cmdparse. A complete example app can be
|
9
|
-
found later in the "tutorial section":#tutorial.
|
10
|
-
|
11
|
-
Defining commands using classes:
|
12
|
-
<pre>
|
13
|
-
class TestCmd < CmdParse::Command
|
14
|
-
def initialize
|
15
|
-
super('test', true)
|
16
|
-
self.add_command(TestSubCmd.new)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class TestSubCmd < CmdParse::Command
|
21
|
-
def initialize
|
22
|
-
super('sub',false)
|
23
|
-
end
|
24
|
-
|
25
|
-
def execute (args)
|
26
|
-
puts "Hallo #{args}"
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
cmd = CmdParse::CommandParser.new( true )
|
31
|
-
cmd.add_command(TestCmd.new)
|
32
|
-
</pre>
|
33
|
-
|
34
|
-
Defining command using the basic @CmdParse::Command@ class:
|
35
|
-
<pre>
|
36
|
-
cmd = CmdParse::CommandParser.new( true )
|
37
|
-
testcmd = CmdParse::Command.new( 'test', true )
|
38
|
-
testcmd.short_desc = "Short desc"
|
39
|
-
cmd.add_command( testcmd )
|
40
|
-
|
41
|
-
sub = CmdParse::Command.new( 'sub', false )
|
42
|
-
sub.short_desc = "Add an IP address"
|
43
|
-
sub.set_execution_block do |args|
|
44
|
-
puts "Hallo #{args}"
|
45
|
-
end
|
46
|
-
testcmd.add_command( sub )
|
47
|
-
</pre>
|
48
|
-
|
49
|
-
h2(#tutorial). Tutorial
|
50
|
-
|
51
|
-
<b>The complete code for this example can be found in the file @net.rb@ of the @cmdparse@
|
52
|
-
package!</b>
|
53
|
-
|
54
|
-
This tutorial produces a small @net@ application which can add, delete and list IP adresses and show
|
55
|
-
'network statistics'. The shown code fragments do not include the whole program. So, instead of
|
56
|
-
writing all the code yourself, just look at the code fragments first and then use the include
|
57
|
-
@net.rb@ file for running the program.
|
58
|
-
|
59
|
-
h3. Require statements
|
60
|
-
|
61
|
-
Create a new new file and write the necessary @require@ statements.
|
62
|
-
|
63
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 5..5}}</notextile>
|
64
|
-
|
65
|
-
h3. The @CommandParser@ class
|
66
|
-
|
67
|
-
Next we will define our basic @CommandParser@ by defining the name of the program, its version and
|
68
|
-
the global options. The first boolean argument to the constructor of the @CommandParser@ class
|
69
|
-
defines whether exceptions should be handled gracefully, i.e. by showing an appropriate error
|
70
|
-
message and the help screen. The second boolean argument defines whether the top level commands
|
71
|
-
should use partial command matching instead of full command matching. If partial command matching is
|
72
|
-
used, then the shortest unambiguous part of a command name can be used instead of always specifing
|
73
|
-
the full command name.
|
74
|
-
|
75
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 30..36}}</notextile>
|
76
|
-
|
77
|
-
The options are defined using an option parser wrapper. Currently, the only option parser library
|
78
|
-
supported is @optparse@ from the Ruby Standard Library. If you want to use another option parser
|
79
|
-
library, you need to write a wrapper for it so that @cmdparse@ can use it.
|
80
|
-
|
81
|
-
Now we only have to tell the program to use our newly defined class to process the command line
|
82
|
-
arguments.
|
83
|
-
|
84
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 86..86}}</notextile>
|
85
|
-
|
86
|
-
The @parse@ method of our @CommandParser@ instance parses the given array of options (or @ARGV@ if
|
87
|
-
no argument is specified). All the command line options are parsed and the given command executed.
|
88
|
-
|
89
|
-
The program can be executed now but won't be useful as we did not specify any commands.
|
90
|
-
|
91
|
-
h3. Defining commands
|
92
|
-
|
93
|
-
So, as we have defined our @CommandParser@ object, we need to add some commands to it. First, we
|
94
|
-
will add two predefined commands, namely the @help@ and the @version@ command.
|
95
|
-
|
96
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 37..38}}</notextile>
|
97
|
-
|
98
|
-
That was easy! Now you can execute the program and specify the commands @help@ or @version@.
|
99
|
-
However, we want the program to do something "useful". Therefore we define a new command.
|
100
|
-
|
101
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 41..44}}</notextile>
|
102
|
-
|
103
|
-
This command is defined by using the default @Command@ class. First an instance is created assigning
|
104
|
-
a name to the command and defining whether this command takes subcommands and uses partial command
|
105
|
-
matching. Next we add a short description so that the @help@ command can produce something useful.
|
106
|
-
And at last, we add this command to our @CommandParser@ instance.
|
107
|
-
|
108
|
-
We specified that our @ipaddr@ command takes subcommands. So we have to define them, too:
|
109
|
-
|
110
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 46..78}}</notextile>
|
111
|
-
|
112
|
-
We add three subcommands to the @ipaddr@ command: @add@, @del@ and @list@.
|
113
|
-
|
114
|
-
The @add@ command is similar to the @ipaddr@ command. However, as the @add@ command does not take
|
115
|
-
other commands, we have to define an execution block.
|
116
|
-
|
117
|
-
The @del@ command is similar to the @add@ command. As we want to be able to delete all IP addresses
|
118
|
-
by issuing only one command, we add an option for this.
|
119
|
-
|
120
|
-
By providing @true@ as second argument when we add the @list@ command to the @ipaddr@ command, we
|
121
|
-
specifiy that this command should be the default command which gets invoked when no command name is
|
122
|
-
specified on the command line. Only one command can be specified as default command!
|
123
|
-
|
124
|
-
Till now we only used the basic @Command@ class to specify commands. However, new commands can also
|
125
|
-
be created by subclassing the @Command@ class, as shown with this last command:
|
126
|
-
|
127
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 9..28}}</notextile>
|
128
|
-
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 39..39}}</notextile>
|
129
|
-
|
130
|
-
h3. Running the program
|
131
|
-
|
132
|
-
That's all! You can run the program now and have a look at the output which differs depending on
|
133
|
-
which arguments you choose.
|
134
|
-
|
135
|
-
So, a typical invocation of this program looks like this:
|
136
|
-
|
137
|
-
<pre>
|
138
|
-
$ ruby net.rb --verbose ipaddr add 192.168.0.1 193.150.0.1
|
139
|
-
</pre>
|
140
|
-
|
141
|
-
* <notextile><tt>--verbose</tt></notextile> is a global option
|
142
|
-
* @ipaddr@ is the first command name (which has no options)
|
143
|
-
* @add@ is the second command name (which also has no options)
|
144
|
-
* <tt>192.168.0.1 193.150.0.1</tt> are additional arguments
|
145
|
-
|
146
|
-
You should notice that if you type
|
147
|
-
<pre>
|
148
|
-
$ ruby net.rb
|
149
|
-
</pre>
|
150
|
-
|
151
|
-
you get an error because you did not specify any command.
|
152
|
-
|
153
|
-
However, when you type
|
154
|
-
<pre>
|
155
|
-
$ ruby net.rb ipaddr
|
156
|
-
</pre>
|
157
|
-
|
158
|
-
you do not get an error!
|
159
|
-
|
160
|
-
Why? As the @ipaddr@ command takes subcommands there should be an additional command name (e.g.
|
161
|
-
@list@) on the command line. However, as the @list@ command is the default command for @ipaddr@ you
|
162
|
-
do not need to type it.
|
163
|
-
|
164
|
-
*By the way:* You get the same output if you type
|
165
|
-
<pre>
|
166
|
-
$ ruby net.rb ip
|
167
|
-
</pre>
|
168
|
-
|
169
|
-
Why? As partial command matching is used for the top level commands, the shortest unambiguous name
|
170
|
-
for a command can be used. As there is no other command starting with @ip@ (or even with the letter
|
171
|
-
@i@), it is sufficient to write the above to select the @ipaddr@ command.
|
172
|
-
|
173
|
-
*Notice:* The options of a command which does not take subcommands do not need to be at the front;
|
174
|
-
they can be anywhere, like this
|
175
|
-
<pre>
|
176
|
-
$ ruby test.rb --verbose mycommand file1 file2 --recursive file3
|
177
|
-
</pre>
|