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.
- data/ChangeLog +40 -0
- data/README +12 -12
- data/Rakefile +11 -11
- data/TODO +1 -0
- data/VERSION +1 -1
- data/doc/config.yaml +2 -0
- data/doc/plugin/extract.rb +1 -1
- data/doc/src/about.page +9 -1
- data/doc/src/default.template +4 -4
- data/doc/src/documentation.page +72 -36
- data/doc/src/download.page +10 -10
- data/doc/src/features.page +4 -4
- data/doc/src/index.page +12 -1
- data/lib/cmdparse.rb +298 -270
- data/lib/cmdparse/wrappers/optparse.rb +65 -0
- data/net.rb +88 -0
- metadata +12 -10
- data/test.rb +0 -42
data/ChangeLog
CHANGED
@@ -1,4 +1,44 @@
|
|
1
1
|
------------------------------------------------------------------------
|
2
|
+
r332 | thomas | 2005-08-16 10:54:01 +0200 (Tue, 16 Aug 2005) | 1 line
|
3
|
+
Changed paths:
|
4
|
+
M /trunk/cmdparse/doc/src/index.page
|
5
|
+
|
6
|
+
* updated index.page
|
7
|
+
------------------------------------------------------------------------
|
8
|
+
r331 | thomas | 2005-08-16 10:51:46 +0200 (Tue, 16 Aug 2005) | 1 line
|
9
|
+
Changed paths:
|
10
|
+
M /trunk/cmdparse/Rakefile
|
11
|
+
|
12
|
+
* corrected Rakefile
|
13
|
+
------------------------------------------------------------------------
|
14
|
+
r329 | thomas | 2005-08-14 17:39:05 +0200 (Sun, 14 Aug 2005) | 7 lines
|
15
|
+
Changed paths:
|
16
|
+
M /trunk/cmdparse/README
|
17
|
+
M /trunk/cmdparse/Rakefile
|
18
|
+
M /trunk/cmdparse/TODO
|
19
|
+
A /trunk/cmdparse/doc/config.yaml
|
20
|
+
M /trunk/cmdparse/doc/plugin/extract.rb
|
21
|
+
M /trunk/cmdparse/doc/src/about.page
|
22
|
+
M /trunk/cmdparse/doc/src/default.template
|
23
|
+
M /trunk/cmdparse/doc/src/documentation.page
|
24
|
+
M /trunk/cmdparse/doc/src/download.page
|
25
|
+
M /trunk/cmdparse/doc/src/features.page
|
26
|
+
M /trunk/cmdparse/doc/src/index.page
|
27
|
+
A /trunk/cmdparse/lib/cmdparse
|
28
|
+
A /trunk/cmdparse/lib/cmdparse/wrappers
|
29
|
+
A /trunk/cmdparse/lib/cmdparse/wrappers/optparse.rb
|
30
|
+
M /trunk/cmdparse/lib/cmdparse.rb
|
31
|
+
A /trunk/cmdparse/net.rb (from /trunk/cmdparse/test.rb:284)
|
32
|
+
D /trunk/cmdparse/test.rb
|
33
|
+
|
34
|
+
* updated Rakefile
|
35
|
+
* added config.yaml for documentation sothat html files get validated
|
36
|
+
* updated documentation
|
37
|
+
* bumped version number to 2.0.0
|
38
|
+
* updated cmdparse.rb
|
39
|
+
* added optparse wrapper
|
40
|
+
* updated README file
|
41
|
+
------------------------------------------------------------------------
|
2
42
|
r328 | thomas | 2005-07-05 16:10:02 +0200 (Tue, 05 Jul 2005) | 1 line
|
3
43
|
Changed paths:
|
4
44
|
M /trunk/cmdparse/Rakefile
|
data/README
CHANGED
@@ -9,26 +9,26 @@ program from Subversion and the "gem" program from Rubygems. The standard Ruby d
|
|
9
9
|
library to create programs that use such a command line interface.
|
10
10
|
|
11
11
|
This library, cmdparse, can be used to create such a command line interface. Internally it uses
|
12
|
-
optparse to parse options and it provides a nice API for
|
12
|
+
optparse or any other option parser library to parse options and it provides a nice API for
|
13
|
+
specifying commands.
|
13
14
|
|
14
15
|
|
15
16
|
= Dependencies
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
none
|
19
19
|
|
20
20
|
= Installation
|
21
21
|
|
22
|
-
|
23
|
-
$
|
24
|
-
$ ruby setup.rb install
|
22
|
+
The preferred way of installing cmdparse is via RubyGems:
|
23
|
+
$ gem install cmdparse
|
25
24
|
|
26
|
-
|
25
|
+
If you do not have RubyGems installed, but Rake, you can use the following command:
|
27
26
|
$ rake install
|
28
27
|
|
29
|
-
|
30
|
-
$
|
31
|
-
|
28
|
+
If you have neither RubyGems nor Rake, use these commands:
|
29
|
+
$ ruby setup.rb config
|
30
|
+
$ ruby setup.rb setup
|
31
|
+
$ ruby setup.rb install
|
32
32
|
|
33
33
|
= Documentation
|
34
34
|
|
@@ -36,12 +36,12 @@ You can build the documentation by invoking
|
|
36
36
|
$ rake doc
|
37
37
|
|
38
38
|
This builds the API and the additional documentation. The additional documentation needs webgen
|
39
|
-
>=0.3.
|
39
|
+
>=0.3.5 (webgen.rubyforge.org) for building.
|
40
40
|
|
41
41
|
|
42
42
|
= Example
|
43
43
|
|
44
|
-
There is an example of how to use cmdparse in the +
|
44
|
+
There is an example of how to use cmdparse in the +net.rb+ file.
|
45
45
|
|
46
46
|
|
47
47
|
= Contact
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
#
|
3
3
|
# $Id$
|
4
4
|
#
|
5
|
-
# cmdparse:
|
5
|
+
# cmdparse: advanced command line parser supporting commands
|
6
6
|
# Copyright (C) 2004 Thomas Leitner
|
7
7
|
#
|
8
8
|
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
|
@@ -32,10 +32,11 @@ require 'rake/testtask'
|
|
32
32
|
|
33
33
|
# General actions ##############################################################
|
34
34
|
|
35
|
-
|
35
|
+
$:.unshift 'lib'
|
36
|
+
require 'cmdparse'
|
36
37
|
|
37
38
|
PKG_NAME = "cmdparse"
|
38
|
-
PKG_VERSION =
|
39
|
+
PKG_VERSION = CmdParse::VERSION.join( '.' )
|
39
40
|
PKG_FULLNAME = PKG_NAME + "-" + PKG_VERSION
|
40
41
|
|
41
42
|
SRC_RB = FileList['lib/**/*.rb']
|
@@ -78,8 +79,7 @@ end
|
|
78
79
|
rd = Rake::RDocTask.new do |rdoc|
|
79
80
|
rdoc.rdoc_dir = 'doc/output/rdoc'
|
80
81
|
rdoc.title = PKG_NAME
|
81
|
-
rdoc.options << '--line-numbers' << '--inline-source' << '-m
|
82
|
-
rdoc.rdoc_files.include( 'README' )
|
82
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '-m CmdParse::CommandParser'
|
83
83
|
rdoc.rdoc_files.include( 'lib/**/*.rb' )
|
84
84
|
end
|
85
85
|
|
@@ -94,7 +94,7 @@ PKG_FILES = FileList.new( [
|
|
94
94
|
'README',
|
95
95
|
'Rakefile',
|
96
96
|
'ChangeLog',
|
97
|
-
'
|
97
|
+
'net.rb',
|
98
98
|
'VERSION',
|
99
99
|
'lib/**/*.rb',
|
100
100
|
'doc/**/*'
|
@@ -112,11 +112,11 @@ else
|
|
112
112
|
|
113
113
|
s.name = PKG_NAME
|
114
114
|
s.version = PKG_VERSION
|
115
|
-
s.summary = "
|
115
|
+
s.summary = "Advanced command line parser supporting commands"
|
116
116
|
s.description = <<-EOF
|
117
|
-
cmdparse
|
118
|
-
|
119
|
-
are, for example, subversion's 'svn' or Rubygem's 'gem' program.
|
117
|
+
cmdparse provides classes for parsing commands on the command line; command line options
|
118
|
+
are parsed using optparse or any other option parser implementation. Programs that use
|
119
|
+
such command line interfaces are, for example, subversion's 'svn' or Rubygem's 'gem' program.
|
120
120
|
EOF
|
121
121
|
|
122
122
|
#### Dependencies, requirements and files
|
@@ -130,7 +130,7 @@ else
|
|
130
130
|
|
131
131
|
s.has_rdoc = true
|
132
132
|
s.extra_rdoc_files = rd.rdoc_files.reject do |fn| fn =~ /\.rb$/ end.to_a
|
133
|
-
s.rdoc_options = ['--line-numbers', '-m', '
|
133
|
+
s.rdoc_options = ['--line-numbers', '-m', 'CmdParse::CommandParser']
|
134
134
|
|
135
135
|
#### Author and project details
|
136
136
|
|
data/TODO
CHANGED
@@ -6,3 +6,4 @@
|
|
6
6
|
* ability to specify default command (used when no command was given)
|
7
7
|
* add order,order!,... methods to mimic optparse
|
8
8
|
* optionally capture error messages and provide friendlier output
|
9
|
+
* if command has subcommands, use order! method, if has arguments use permute! (=POSIXLY_CORRECT); same with getoptlong
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/doc/config.yaml
ADDED
data/doc/plugin/extract.rb
CHANGED
@@ -19,7 +19,7 @@ module CmdparsePlugins
|
|
19
19
|
data = File.readlines( get_param( 'file' ) ).unshift( 'empty null line' )[get_param( 'lines' )]
|
20
20
|
out = '<table class="cmdparse-example"><tr>'
|
21
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)}
|
22
|
+
out << '<td><pre>' << data.collect {|line| "#{CGI::escapeHTML(line)}" }.to_s << '</pre></td>'
|
23
23
|
out << '</tr></table>'
|
24
24
|
out
|
25
25
|
end
|
data/doc/src/about.page
CHANGED
@@ -10,7 +10,15 @@ program from Subversion and the "gem" program from Rubygems. The standard Ruby d
|
|
10
10
|
library to create programs that use such a command line interface.
|
11
11
|
|
12
12
|
This library, cmdparse, can be used to create such a command line interface. Internally it uses
|
13
|
-
optparse to parse options and it provides a nice API for
|
13
|
+
optparse or any other option parser library to parse options and it provides a nice API for
|
14
|
+
specifying commands and subcommands.
|
15
|
+
|
16
|
+
A typical command line for a program which uses commands looks like this:
|
17
|
+
|
18
|
+
<pre>
|
19
|
+
$ net --verbose ipaddr add 192.168.0.1 193.150.0.1
|
20
|
+
</pre>
|
21
|
+
|
14
22
|
|
15
23
|
h2. Author
|
16
24
|
|
data/doc/src/default.template
CHANGED
@@ -13,7 +13,7 @@
|
|
13
13
|
<table>
|
14
14
|
<tr>
|
15
15
|
<td><img src="{relocatable: logo.png}" alt="Logo" title="Logo" /></td>
|
16
|
-
<td><h1>cmdparse<span style="font-size: 60%"> - advanced command line parser
|
16
|
+
<td><h1>cmdparse<span style="font-size: 60%"> - advanced command line parser supporting commands</span></h1>
|
17
17
|
</td>
|
18
18
|
</tr>
|
19
19
|
</table>
|
@@ -34,9 +34,9 @@
|
|
34
34
|
</div>
|
35
35
|
|
36
36
|
<div id="footer" class="bar">
|
37
|
-
<a href="http://validator.w3.org/check?uri=referer"><img src="
|
38
|
-
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="
|
39
|
-
<a href="http://webgen.rubyforge.org"><img src="
|
37
|
+
<a href="http://validator.w3.org/check?uri=referer"><img src="{resource: w3c-valid-xhtml11}" alt="Valid XHTML 1.1!" height="31" width="88" /></a>
|
38
|
+
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="{resource: w3c-valid-css}" alt="Valid CSS!" height="31" width="88" /></a>
|
39
|
+
<a href="http://webgen.rubyforge.org"><img src="{resource: webgen-generated}" alt="Generated by webgen" height="31" width="88"/></a> on <b>{date: }</b>
|
40
40
|
</div>
|
41
41
|
</div>
|
42
42
|
</body>
|
data/doc/src/documentation.page
CHANGED
@@ -10,82 +10,118 @@ The <a href="{relocatable: /rdoc/index.html}">API reference</a> provides informa
|
|
10
10
|
|
11
11
|
h2. Tutorial
|
12
12
|
|
13
|
-
<b>The complete code for this example can be found in the file @
|
13
|
+
<b>The complete code for this example can be found in the file @net.rb@ of the @cmdparse@
|
14
14
|
package!</b>
|
15
15
|
|
16
|
+
This tutorial produces a small @net@ application which can add, delete and list IP adresses and show
|
17
|
+
'network statistics'. The shown code fragments do not include the whole program. So, instead of
|
18
|
+
writing all the code yourself, just look at the code fragments first and then use the include
|
19
|
+
@net.rb@ file for running the program.
|
20
|
+
|
16
21
|
h3. Require statements
|
17
22
|
|
18
23
|
Create a new new file and write the necessary @require@ statements.
|
19
24
|
|
20
|
-
<notextile>{extract: {file: ../
|
25
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 5..5}}</notextile>
|
21
26
|
|
22
27
|
h3. The @CommandParser@ class
|
23
28
|
|
24
29
|
Next we will define our basic @CommandParser@ by defining the name of the program, its version and
|
25
|
-
|
26
|
-
|
27
|
-
the
|
28
|
-
|
30
|
+
the global options. The boolean argument to the constructor of the @CommandParser@ class defines
|
31
|
+
whether exceptions should be handled gracefully, i.e. by showing an appropriate error message and
|
32
|
+
the help screen.
|
33
|
+
|
34
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 30..36}}</notextile>
|
29
35
|
|
30
|
-
|
36
|
+
The options are defined using an option parser wrapper. Currently, the only option parser library
|
37
|
+
supported is @optparse@ from the Ruby Standard Library. If you want to use another option parser
|
38
|
+
library, you need to write a wrapper for it so that @cmdparse@ can use it.
|
31
39
|
|
32
40
|
Now we only have to tell the program to use our newly defined class to process the command line
|
33
41
|
arguments.
|
34
42
|
|
35
|
-
<notextile>{extract: {file: ../
|
43
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 86..86}}</notextile>
|
36
44
|
|
37
|
-
The
|
38
|
-
argument
|
39
|
-
parsed). The @#execute@ method then executes the command.
|
45
|
+
The @parse@ method of our @CommandParser@ instance parses the given array of options (or @ARGV@ if
|
46
|
+
no argument is specified). All the command line options are parsed and the given command executed.
|
40
47
|
|
41
|
-
The program can be executed now but won't be useful as we
|
42
|
-
always produce error messages.
|
48
|
+
The program can be executed now but won't be useful as we did not specify any commands.
|
43
49
|
|
44
50
|
h3. Defining commands
|
45
51
|
|
46
52
|
So, as we have defined our @CommandParser@ object, we need to add some commands to it. First, we
|
47
53
|
will add two predefined commands, namely the @help@ and the @version@ command.
|
48
54
|
|
49
|
-
<notextile>{extract: {file: ../
|
55
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 37..38}}</notextile>
|
50
56
|
|
51
57
|
That was easy! Now you can execute the program and specify the commands @help@ or @version@.
|
52
58
|
However, we want the program to do something "useful". Therefore we define a new command.
|
53
59
|
|
54
|
-
<notextile>{extract: {file: ../
|
60
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 41..44}}</notextile>
|
61
|
+
|
62
|
+
This command is defined by using the default @Command@ class. First an instance is created
|
63
|
+
assigning a name to the command and defining whether this command takes subcommands. Next we add a
|
64
|
+
short description so that the @help@ command can produce something useful. And at last, we add this
|
65
|
+
command to our @CommandParser@ instance.
|
66
|
+
|
67
|
+
We specified that our @ipaddr@ command takes subcommands. So we have to define them, too:
|
68
|
+
|
69
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 46..78}}</notextile>
|
55
70
|
|
56
|
-
|
57
|
-
our @CommandParser@ object if, and only if, the command is specified on the command line. The
|
58
|
-
@#description@ method describes the command and the @#execute@ is called when the command needs to
|
59
|
-
be executed.
|
71
|
+
We add three subcommands to the @ipaddr@ command: @add@, @del@ and @list@.
|
60
72
|
|
61
|
-
|
73
|
+
The @add@ command is similar to the @ipaddr@ command. However, as the @add@ command does not take
|
74
|
+
other commands, we have to define an execution block.
|
62
75
|
|
63
|
-
|
76
|
+
The @del@ command is similar to the @add@ command. As we want to be able to delete all IP addresses
|
77
|
+
by issuing only one command, we add an option for this.
|
64
78
|
|
65
|
-
By providing @true@ as second argument
|
66
|
-
|
67
|
-
specified as default command!
|
79
|
+
By providing @true@ as second argument when we add the @list@ command to the @ipaddr@ command, we
|
80
|
+
specifiy that this command should be the default command which gets invoked when no command name is
|
81
|
+
specified on the command line. Only one command can be specified as default command!
|
82
|
+
|
83
|
+
Till now we only used the basic @Command@ class to specify commands. However, new commands can also
|
84
|
+
be created by subclassing the @Command@ class, as shown with this last command:
|
85
|
+
|
86
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 9..28}}</notextile>
|
87
|
+
<notextile>{extract: {file: ../net.rb, lines: !ruby/range 39..39}}</notextile>
|
68
88
|
|
69
89
|
h3. Running the program
|
70
90
|
|
71
91
|
That's all! You can run the program now and have a look at the output which differs depending on
|
72
92
|
which arguments you choose.
|
73
93
|
|
74
|
-
|
75
|
-
command line:
|
94
|
+
So, a typical invocation of this program looks like this:
|
76
95
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
# [optional] Additional arguments
|
96
|
+
<pre>
|
97
|
+
$ ruby net.rb --verbose ipaddr add 192.168.0.1 193.150.0.1
|
98
|
+
</pre>
|
81
99
|
|
82
|
-
|
100
|
+
* <notextile><tt>--verbose</tt></notextile> is a global option
|
101
|
+
* @ipaddr@ is the first command name (which has no options)
|
102
|
+
* @add@ is the second command name (which also has no options)
|
103
|
+
* <tt>192.168.0.1 193.150.0.1</tt> are additional arguments
|
83
104
|
|
105
|
+
You should notice that if you type
|
84
106
|
<pre>
|
85
|
-
$ ruby
|
107
|
+
$ ruby net.rb
|
86
108
|
</pre>
|
87
109
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
110
|
+
you get an error because you did not specify any command.
|
111
|
+
|
112
|
+
However, when you type
|
113
|
+
<pre>
|
114
|
+
$ ruby net.rb ipaddr
|
115
|
+
</pre>
|
116
|
+
|
117
|
+
you do not get an error!
|
118
|
+
|
119
|
+
Why? As the @ipaddr@ command takes subcommands there should be an additional command name (e.g.
|
120
|
+
@list@) on the command line. However, as the @list@ command is the default command for @ipaddr@ you
|
121
|
+
do not need to type it.
|
122
|
+
|
123
|
+
*Notice:* The options of a command which does not take subcommands do not need to be at the front;
|
124
|
+
they can be anywhere, like this
|
125
|
+
<pre>
|
126
|
+
$ ruby test.rb --verbose mycommand file1 file2 --recursive file3
|
127
|
+
</pre>
|
data/doc/src/download.page
CHANGED
@@ -7,28 +7,28 @@ h2. Download
|
|
7
7
|
|
8
8
|
The newest version of cmdparse can be downloaded from Rubyforge.
|
9
9
|
|
10
|
-
Homepage:
|
11
|
-
Download:
|
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
12
|
|
13
13
|
h2. Dependencies
|
14
14
|
|
15
|
-
|
15
|
+
none
|
16
16
|
|
17
17
|
h2. Installation
|
18
18
|
|
19
|
-
|
19
|
+
The preferred way of installing cmdparse is via RubyGems:
|
20
20
|
<pre>
|
21
|
-
$
|
22
|
-
$ ruby setup.rb setup
|
23
|
-
$ ruby setup.rb install
|
21
|
+
$ gem install cmdparse
|
24
22
|
</pre>
|
25
23
|
|
26
|
-
|
24
|
+
If you do not have RubyGems installed, but Rake, you can use the following command:
|
27
25
|
<pre>
|
28
26
|
$ rake install
|
29
27
|
</pre>
|
30
28
|
|
31
|
-
|
29
|
+
If you have neither RubyGems nor Rake, use these commands:
|
32
30
|
<pre>
|
33
|
-
$
|
31
|
+
$ ruby setup.rb config
|
32
|
+
$ ruby setup.rb setup
|
33
|
+
$ ruby setup.rb install
|
34
34
|
</pre>
|
data/doc/src/features.page
CHANGED
@@ -5,7 +5,7 @@ orderInfo: 3
|
|
5
5
|
---
|
6
6
|
h2. Feature list
|
7
7
|
|
8
|
-
*
|
9
|
-
* Default help and version
|
10
|
-
*
|
11
|
-
*
|
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
CHANGED
@@ -6,12 +6,23 @@ menuOrder: 1
|
|
6
6
|
---
|
7
7
|
h2. Welcome
|
8
8
|
|
9
|
-
... to the homepage of _*cmdparse*_, an advanced command line parser.
|
9
|
+
... to the homepage of _*cmdparse*_, an advanced command line parser supporting commands.
|
10
10
|
|
11
11
|
Have a look around the site!
|
12
12
|
|
13
13
|
h2. News
|
14
14
|
|
15
|
+
<b>2005-08-16 cmdparse 2.0.0 released!!!</b>
|
16
|
+
|
17
|
+
This version is not compatible to previous versions of cmdparse as there have been major changes in
|
18
|
+
the API. However, updating your code to use the new API is very easy!
|
19
|
+
|
20
|
+
Changes:
|
21
|
+
|
22
|
+
* Commands can now have subcommands which can have subcommands which can have subcommands...
|
23
|
+
* No need to implement a whole new class for simple commands anymore
|
24
|
+
* Default option parser library is @optparse@, however, any option parser library can be used after writing a small wrapper
|
25
|
+
|
15
26
|
<b>2005-07-05 cmdparse 1.0.5 released!!!</b>
|
16
27
|
|
17
28
|
Changes:
|