optplus 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/Bugs.rdoc +5 -0
- data/Gemfile +3 -0
- data/History.txt +39 -0
- data/Intro.txt +3 -0
- data/LICENCE.rdoc +159 -0
- data/README.md +296 -0
- data/bin/optplus +78 -0
- data/bin/optplus-installer +32 -0
- data/lib/optplus.rb +416 -0
- data/lib/optplus/errors.rb +31 -0
- data/lib/optplus/nested.rb +159 -0
- data/lib/optplus/version.rb +13 -0
- data/spec/optplus_spec.rb +41 -0
- data/spec/spec_helper.rb +12 -0
- data/test/conf.d/optplus.rb +1 -0
- data/test/my_nested_parser.rb +130 -0
- data/test/my_parser.rb +90 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MDU0ODg5OTM1ZjZlNjQ3ZWM4NzA0NGNmYTRhOWY4NWYxYzI0NzJhZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDMzYjM4Y2EyZmQ3MDU4N2RmNjA1MDQ0YTkyM2M4MjdiYTRkMTIxZg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjRmMGU3Zjg3ZjJmOGI3MDE1MDJlODBlM2U5NWQ1NjA3ZjQyYzBmODg3OWM5
|
10
|
+
MzA5YTE1ODRlMDNmMTdmYmNjYjhjNmQzNmRmZmRhMDM3YmZkMzJhOWQ5MGFj
|
11
|
+
ZGM0Y2FkNmJjOTRlYjkwN2ZmOGE2MDY5MzkyYzMwZGUzZTY1YzA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YWZkNDdlMzBmNzI5OTMwOTc4MGFlNDU5Njg4NGViNDE0Njk0ZDFhOTcwMmU3
|
14
|
+
NDQ4ZDI1NTFmMjQ3ODQzODQ5MmM2NzJlZmYyZTZmMjVmOGZlOWFlMTc5YTUz
|
15
|
+
MDUyYWFkYzI2MTI1MzUwOWQ0MzRiNGE4MzY0ZDZiOWM2ZmE5NWI=
|
data/Bugs.rdoc
ADDED
data/Gemfile
ADDED
data/History.txt
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# @markup rdoc
|
2
|
+
# @title Change History
|
3
|
+
|
4
|
+
== History
|
5
|
+
[optplus-0.0.8 13-Sep-2013]
|
6
|
+
|
7
|
+
Release Candidate with nesting etc.
|
8
|
+
|
9
|
+
[optplus-0.0.7 11-Sep-2013]
|
10
|
+
|
11
|
+
Add flag to actions that have their own help
|
12
|
+
|
13
|
+
[optplus-0.0.6 11-Sep-2013]
|
14
|
+
|
15
|
+
Fix typo in hasty release
|
16
|
+
|
17
|
+
[optplus-0.0.5 11-Sep-2013]
|
18
|
+
|
19
|
+
Add a general error handler to the run! method to make it easier to handle argument errors
|
20
|
+
|
21
|
+
[optplus-0.0.4 11-Sep-2013]
|
22
|
+
|
23
|
+
Add a couple of next_argument variants for defaults and exceptions
|
24
|
+
|
25
|
+
[optplus-0.0.3 03-Sep-2013]
|
26
|
+
|
27
|
+
Fix Gemfile to remove unwanted and cyclic dependencies
|
28
|
+
|
29
|
+
[optplus-0.0.2 03-Sep-2013]
|
30
|
+
|
31
|
+
Include Nested Parser
|
32
|
+
|
33
|
+
[optplus-0.0.1 01-Sep-2013]
|
34
|
+
|
35
|
+
Initial release
|
36
|
+
|
37
|
+
[optplus-0.0.0 30-Aug-2013]
|
38
|
+
|
39
|
+
New application generated
|
data/Intro.txt
ADDED
data/LICENCE.rdoc
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
= LICENCE
|
2
|
+
|
3
|
+
This software is licensed for use under the Open Software Licence v. 3.0
|
4
|
+
The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php
|
5
|
+
and below. Under the terms of this licence, all derivative works
|
6
|
+
must themselves be licensed under the Open Software Licence v. 3.0
|
7
|
+
|
8
|
+
Open Source Initiative OSI - The Open Software Licence 3.0:Licensing
|
9
|
+
[OSI Approved Licence]
|
10
|
+
Open Software Licence ("OSL") v. 3.0
|
11
|
+
|
12
|
+
This Open Software Licence (the "Licence") applies to any original work of authorship
|
13
|
+
(the "Original Work") whose owner (the "Licensor") has placed the following licensing
|
14
|
+
notice adjacent to the copyright notice for the Original Work:
|
15
|
+
|
16
|
+
Licensed under the Open Software Licence version 3.0
|
17
|
+
|
18
|
+
1) Grant of Copyright Licence. Licensor grants You a worldwide, royalty-free, non-exclusive,
|
19
|
+
sublicensable license, for the duration of the copyright, to do the following:
|
20
|
+
|
21
|
+
a) to reproduce the Original Work in copies, either alone or as part of a collective work;
|
22
|
+
|
23
|
+
b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby
|
24
|
+
creating derivative works ("Derivative Works") based upon the Original Work;
|
25
|
+
|
26
|
+
c) to distribute or communicate copies of the Original Work and Derivative Works to
|
27
|
+
the public, with the proviso that copies of Original Work or Derivative Works that
|
28
|
+
You distribute or communicate shall be licensed under this Open Software Licence;
|
29
|
+
|
30
|
+
d) to perform the Original Work publicly; and
|
31
|
+
|
32
|
+
e) to display the Original Work publicly.
|
33
|
+
|
34
|
+
2) Grant of Patent Licence. Licensor grants You a worldwide, royalty-free, non-exclusive,
|
35
|
+
sublicensable license, under patent claims owned or controlled by the Licensor that are
|
36
|
+
embodied in the Original Work as furnished by the Licensor, for the duration of the
|
37
|
+
patents, to make, use, sell, offer for sale, have made, and import the Original Work
|
38
|
+
and Derivative Works.
|
39
|
+
|
40
|
+
3) Grant of Source Code Licence. The term "Source Code" means the preferred form of
|
41
|
+
the Original Work for making modifications to it and all available documentation
|
42
|
+
describing how to modify the Original Work. Licensor agrees to provide a machine-readable
|
43
|
+
copy of the Source Code of the Original Work along with each copy of the Original Work
|
44
|
+
that Licensor distributes. Licensor reserves the right to satisfy this obligation by
|
45
|
+
placing a machine-readable copy of the Source Code in an information repository reasonably
|
46
|
+
calculated to permit inexpensive and convenient access by You for as long as Licensor
|
47
|
+
continues to distribute the Original Work.
|
48
|
+
|
49
|
+
4) Exclusions From Licence Grant. Neither the names of Licensor, nor the names of
|
50
|
+
any contributors to the Original Work, nor any of their trademarks or service marks,
|
51
|
+
may be used to endorse or promote products derived from this Original Work without
|
52
|
+
express prior permission of the Licensor. Except as expressly stated herein, nothing
|
53
|
+
in this Licence grants any license to Licensor's trademarks, copyrights, patents,
|
54
|
+
trade secrets or any other intellectual property. No patent license is granted to
|
55
|
+
make, use, sell, offer for sale, have made, or import embodiments of any patent claims
|
56
|
+
other than the licensed claims defined in Section 2. No license is granted to the
|
57
|
+
trademarks of Licensor even if such marks are included in the Original Work. Nothing
|
58
|
+
in this Licence shall be interpreted to prohibit Licensor from licensing under terms
|
59
|
+
different from this Licence any Original Work that Licensor otherwise would have a
|
60
|
+
right to license.
|
61
|
+
|
62
|
+
5) External Deployment. The term "External Deployment" means the use, distribution,
|
63
|
+
or communication of the Original Work or Derivative Works in any way such that the
|
64
|
+
Original Work or Derivative Works may be used by anyone other than You, whether those
|
65
|
+
works are distributed or communicated to those persons or made available as an application
|
66
|
+
intended for use over a network. As an express condition for the grants of license hereunder,
|
67
|
+
You must treat any External Deployment by You of the Original Work or a Derivative Work
|
68
|
+
as a distribution under section 1(c).
|
69
|
+
|
70
|
+
6) Attribution Rights. You must retain, in the Source Code of any Derivative Works
|
71
|
+
that You create, all copyright, patent, or trademark notices from the Source Code of
|
72
|
+
the Original Work, as well as any notices of licensing and any descriptive text identified
|
73
|
+
therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works
|
74
|
+
that You create to carry a prominent Attribution Notice reasonably calculated to inform
|
75
|
+
recipients that You have modified the Original Work.
|
76
|
+
|
77
|
+
7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright
|
78
|
+
in and to the Original Work and the patent rights granted herein by Licensor are owned by
|
79
|
+
the Licensor or are sublicensed to You under the terms of this Licence with the permission
|
80
|
+
of the contributor(s) of those copyrights and patent rights. Except as expressly stated in
|
81
|
+
the immediately preceding sentence, the Original Work is provided under this Licence on an
|
82
|
+
"AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation,
|
83
|
+
the warranties of non-infringement, merchantability or fitness for a particular purpose.
|
84
|
+
THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY
|
85
|
+
constitutes an essential part of this Licence. No license to the Original Work is granted
|
86
|
+
by this Licence except under this disclaimer.
|
87
|
+
|
88
|
+
8) Limitation of Liability. Under no circumstances and under no legal theory, whether
|
89
|
+
in tort (including negligence), contract, or otherwise, shall the Licensor be liable
|
90
|
+
to anyone for any indirect, special, incidental, or consequential damages of any character
|
91
|
+
arising as a result of this Licence or the use of the Original Work including, without
|
92
|
+
limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction,
|
93
|
+
or any and all other commercial damages or losses. This limitation of liability shall not
|
94
|
+
apply to the extent applicable law prohibits such limitation.
|
95
|
+
|
96
|
+
9) Acceptance and Termination. If, at any time, You expressly assented to this Licence,
|
97
|
+
that assent indicates your clear and irrevocable acceptance of this Licence and all of its
|
98
|
+
terms and conditions. If You distribute or communicate copies of the Original Work or a
|
99
|
+
Derivative Work, You must make a reasonable effort under the circumstances to obtain the
|
100
|
+
express assent of recipients to the terms of this Licence. This Licence conditions your
|
101
|
+
rights to undertake the activities listed in Section 1, including your right to create
|
102
|
+
Derivative Works based upon the Original Work, and doing so without honoring these
|
103
|
+
terms and conditions is prohibited by copyright law and international treaty. Nothing
|
104
|
+
in this Licence is intended to affect copyright exceptions and limitations (including
|
105
|
+
"fair use" or "fair dealing"). This Licence shall terminate immediately and You may no
|
106
|
+
longer exercise any of the rights granted to You by this Licence upon your failure to
|
107
|
+
honor the conditions in Section 1(c).
|
108
|
+
|
109
|
+
10) Termination for Patent Action. This Licence shall terminate automatically and
|
110
|
+
You may no longer exercise any of the rights granted to You by this Licence as of
|
111
|
+
the date You commence an action, including a cross-claim or counterclaim, against
|
112
|
+
Licensor or any licensee alleging that the Original Work infringes a patent. This
|
113
|
+
termination provision shall not apply for an action alleging patent infringement
|
114
|
+
by combinations of the Original Work with other software or hardware.
|
115
|
+
|
116
|
+
11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this Licence
|
117
|
+
may be brought only in the courts of a jurisdiction wherein the Licensor resides or
|
118
|
+
in which Licensor conducts its primary business, and under the laws of that jurisdiction
|
119
|
+
excluding its conflict-of-law provisions. The application of the United Nations Convention
|
120
|
+
on Contracts for the International Sale of Goods is expressly excluded. Any use of
|
121
|
+
the Original Work outside the scope of this Licence or after its termination shall be
|
122
|
+
subject to the requirements and penalties of copyright or patent law in the appropriate
|
123
|
+
jurisdiction. This section shall survive the termination of this Licence.
|
124
|
+
|
125
|
+
12) Attorneys' Fees. In any action to enforce the terms of this Licence or seeking
|
126
|
+
damages relating thereto, the prevailing party shall be entitled to recover its costs
|
127
|
+
and expenses, including, without limitation, reasonable attorneys' fees and costs
|
128
|
+
incurred in connection with such action, including any appeal of such action. This
|
129
|
+
section shall survive the termination of this Licence.
|
130
|
+
|
131
|
+
13) Miscellaneous. If any provision of this Licence is held to be unenforceable,
|
132
|
+
such provision shall be reformed only to the extent necessary to make it enforceable.
|
133
|
+
|
134
|
+
14) Definition of "You" in This Licence. "You" throughout this Licence, whether in
|
135
|
+
upper or lower case, means an individual or a legal entity exercising rights under,
|
136
|
+
and complying with all of the terms of, this Licence. For legal entities, "You" includes
|
137
|
+
any entity that controls, is controlled by, or is under common control with you. For
|
138
|
+
purposes of this definition, "control" means (i) the power, direct or indirect, to cause
|
139
|
+
the direction or management of such entity, whether by contract or otherwise, or (ii)
|
140
|
+
ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial
|
141
|
+
ownership of such entity.
|
142
|
+
|
143
|
+
15) Right to Use. You may use the Original Work in all ways not otherwise restricted or
|
144
|
+
conditioned by this Licence or by law, and Licensor promises not to interfere with or be
|
145
|
+
responsible for such uses by You.
|
146
|
+
|
147
|
+
16) Modification of This Licence. This Licence is Copyright © 2005 Lawrence Rosen.
|
148
|
+
Permission is granted to copy, distribute, or communicate this Licence without modification.
|
149
|
+
Nothing in this Licence permits You to modify this Licence as applied to the Original Work
|
150
|
+
or to Derivative Works. However, You may modify the text of this Licence and copy, distribute
|
151
|
+
or communicate your modified version (the "Modified Licence") and apply it to other original
|
152
|
+
works of authorship subject to the following conditions: (i) You may not indicate in any way
|
153
|
+
that your Modified Licence is the "Open Software Licence" or "OSL" and you may not use those
|
154
|
+
names in the name of your Modified Licence; (ii) You must replace the notice specified in the
|
155
|
+
first paragraph above with the notice "Licensed under <insert your license name here>" or
|
156
|
+
with a notice of your own that is not confusingly similar to the notice in this Licence;
|
157
|
+
and (iii) You may not claim that your original works are open source software unless your
|
158
|
+
Modified Licence has been approved by Open Source Initiative (OSI) and You comply with
|
159
|
+
its license review and certification process.
|
data/README.md
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
# OPTPLUS
|
2
|
+
|
3
|
+
Optplus is a wrapper around optparse aimed at making optparse-based scripts easier to write and
|
4
|
+
offering enhanced functions for little extra effort. It has a Thor-like structure but is
|
5
|
+
strictly optparse based with none of the complications that thor presents.
|
6
|
+
|
7
|
+
**GitHub:** [https://github.com/osburn-sharp/optplus](https://github.com/osburn-sharp/optplus)
|
8
|
+
|
9
|
+
**RubyDoc:** [http://rubydoc.info/github/osburn-sharp/optplus/frames](http://rubydoc.info/github/osburn-sharp/optplus/frames)
|
10
|
+
|
11
|
+
**RubyGems:** [https://rubygems.org/gems/optplus](https://rubygems.org/gems/optplus)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Optplus is available as a gem. To install:
|
16
|
+
|
17
|
+
# gem install optplus
|
18
|
+
|
19
|
+
To use optplus, just require it. See below for examples.
|
20
|
+
|
21
|
+
## Getting Started
|
22
|
+
|
23
|
+
To use Optplus, first define a class that inherits from {Optplus::Parser}, add an options
|
24
|
+
method to include your own options (Optplus defines a few for you) and then define some
|
25
|
+
action methods to do the work. After the class definition use the {Optplus::Parser.run!}
|
26
|
+
class method to run the parser.
|
27
|
+
|
28
|
+
### Simple Example
|
29
|
+
|
30
|
+
Here is a simple example:
|
31
|
+
|
32
|
+
require 'optplus'
|
33
|
+
|
34
|
+
class Mycli < Optplus::Parser
|
35
|
+
|
36
|
+
usage "actions [params] [options]"
|
37
|
+
|
38
|
+
description "a brief description",
|
39
|
+
"that can extend over multiple lines"
|
40
|
+
|
41
|
+
def options(opts)
|
42
|
+
|
43
|
+
# define your own options just like with optparse
|
44
|
+
opts.on('-a', '--all', 'show all the items') do
|
45
|
+
# optplus provides a convenient way to set and get options
|
46
|
+
set_option :all
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe :show, 'show all of the items that match the string given'
|
52
|
+
def show
|
53
|
+
matches = next_argument_or_error('You must provide a string to match')
|
54
|
+
|
55
|
+
# do the stuff
|
56
|
+
|
57
|
+
end
|
58
|
+
help :show, "A more details description of the show action",
|
59
|
+
"that can again spread over multiple lines",
|
60
|
+
"and is displayed when the user types mycli show -h"
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
Mycli.run! # don't forget!
|
65
|
+
|
66
|
+
Optplus allows minimal unambiguous abbreviations for actions using the Array#abbrev
|
67
|
+
method. Single letters will not be recognised but two or more are generally enough.
|
68
|
+
It is worth planning your action names to enable convenient abbreviations to be used.
|
69
|
+
|
70
|
+
## General Description
|
71
|
+
|
72
|
+
Optplus is nothing more than a convenient wrapper around Optparse aimed at accelerating
|
73
|
+
the writing of useful scripts that work in the style of actions, parameters and options.
|
74
|
+
It uses Optparse#permute to evaluate the command line so that options can be scattered
|
75
|
+
anywhere on the command line and will be evaluated all in the same level. It also
|
76
|
+
provides the ability to nest a parser so that actions can have sub-actions - see below
|
77
|
+
for further details.
|
78
|
+
|
79
|
+
Using Optplus involves the following:
|
80
|
+
|
81
|
+
* define a class that inherits from {Optplus::Parser}
|
82
|
+
|
83
|
+
* specify some preamble help information using the following meta-methods
|
84
|
+
|
85
|
+
> **usage** -- define the order of any actions, parameters or options to display in
|
86
|
+
the usage line. Does not need "Usage etc" cos this is provided automatically
|
87
|
+
|
88
|
+
> **description** -- multiline description displayed at the top of the help text. If
|
89
|
+
you want more details, use the readme action.
|
90
|
+
|
91
|
+
* define a **#before_all** method if you need something to be done before any optparse
|
92
|
+
methods or anything else. It expects no parameters.
|
93
|
+
|
94
|
+
* define an **#options** method to add your own option switches in the same manner as Optparse.
|
95
|
+
You must provide a parameter to pass in the optparse object (e.g. opts).
|
96
|
+
There are some convenience methods that can also be called with the optparse object
|
97
|
+
to add some common option switches. See below for more details.
|
98
|
+
|
99
|
+
* define a **#before_actions** method -- this method is called after the optplus parser has
|
100
|
+
been evaluated but before any actions are executed. Use this to set up common objects
|
101
|
+
for these actions. It expects no parameters.
|
102
|
+
|
103
|
+
* For each action you need to define the following
|
104
|
+
|
105
|
+
> **describe** -- links an action (as a symbol) to a one line description.
|
106
|
+
|
107
|
+
> **action_ method** -- for the action itself, having the same name as the symbol above
|
108
|
+
|
109
|
+
> **help** - links the action to multi-line help text that will be displayed when
|
110
|
+
the --help option is used with the action
|
111
|
+
|
112
|
+
* And finally, if you need to tidy up afterwards you can define an after_actions
|
113
|
+
method.
|
114
|
+
|
115
|
+
Within an action method you can obtain parameters using the {Optplus::Parser#next_argument}
|
116
|
+
method. This will return nil if there is no next argument, but to make life a little easier
|
117
|
+
you can use {Optplus::Parser#next_argument_or} to define a default if there is no argument, or
|
118
|
+
{Optplus::Parser#next_argument_or_error} to specify an error message if there is no argument.
|
119
|
+
The latter will raise an exception that is trapped by {Optplus::Parser.run!} to display
|
120
|
+
as a sensible error message.
|
121
|
+
|
122
|
+
Note that Optparser may raise a number of exceptions, for example where a type is defined
|
123
|
+
but the wrong type entered, and Optplus will trap these and display appropriate error
|
124
|
+
messages when this occurs so you don't have to worry about them.
|
125
|
+
|
126
|
+
There are a couple of example scripts that can be used to see what Optplus does.
|
127
|
+
See Tests below.
|
128
|
+
|
129
|
+
### Default and Extra Option Switches
|
130
|
+
|
131
|
+
Optplus provides a -h or --help option switch by default. Two other option switches can be added
|
132
|
+
simply by calling helper methods:
|
133
|
+
|
134
|
+
debug_option(opts) # add a debug switch (-D, --debug)
|
135
|
+
|
136
|
+
puts "In debug mode" if get_option(:debug)
|
137
|
+
|
138
|
+
verbose_option(opts) # add a verbose switch (-V, --verbose)
|
139
|
+
|
140
|
+
puts "In verbose mode" if get_option(:verbose)
|
141
|
+
|
142
|
+
You can change the short-form switch by adding a different one:
|
143
|
+
|
144
|
+
debug_option(opts, '-d')
|
145
|
+
|
146
|
+
### Options helpers
|
147
|
+
|
148
|
+
To make things a little easier there are a couple of methods to set and read local
|
149
|
+
options:
|
150
|
+
|
151
|
+
**{Optplus::Parser#set_option}** that can be used to set an option with the given :key
|
152
|
+
to either true (if no object is passed as the second parameter) or to the given object.
|
153
|
+
|
154
|
+
**{Optplus::Parser#get_option}** returns the value of the set option for the given :key
|
155
|
+
|
156
|
+
**{Optplus::Parser#option?}** returns true if the given option has been defined
|
157
|
+
|
158
|
+
For example:
|
159
|
+
|
160
|
+
set_option :all
|
161
|
+
set_option :config, path
|
162
|
+
set_option :environment, env
|
163
|
+
|
164
|
+
if option? :all then
|
165
|
+
# do something
|
166
|
+
params = get_config(get_option(:config))
|
167
|
+
end
|
168
|
+
|
169
|
+
You can always define instance variables if you prefer.
|
170
|
+
|
171
|
+
### Handling Errors
|
172
|
+
|
173
|
+
Optplus should take care of parsing errors and the like without you having to get involved.
|
174
|
+
However, there may be circumstances when you need to exit the script in a hurry and
|
175
|
+
you may need to tidy up if you have already set something up (e.g. in a before_actions method).
|
176
|
+
The {Optplus::Parser#exit_on_error} method is provided to help. Calling the method
|
177
|
+
will raise an exception that will be trapped to ensure that, if necessary, the
|
178
|
+
{Optplus::Parser#after_actions} method is called and the script exits with code 1. It will
|
179
|
+
also display the message if provided.
|
180
|
+
|
181
|
+
## Nesting Actions
|
182
|
+
|
183
|
+
Optplus allows you to define sub-actions or sub-commands for an action if you want to.
|
184
|
+
The general syntax would be: action sub [param] [options].
|
185
|
+
|
186
|
+
First you need to define a nested parser class, inheriting from {Optplus::NestedParser}
|
187
|
+
instead of {Optplus::Parser}. This is similar to the main parser but should only contain
|
188
|
+
a usage and description call, action methods, descriptions and help text.
|
189
|
+
|
190
|
+
Once this nested class is defined you must define the action to which it will belong
|
191
|
+
using the {Optplus::Parser.nest_parser} method. Here is a simple example:
|
192
|
+
|
193
|
+
class MyParser < Optplus::Parser
|
194
|
+
|
195
|
+
usage "action [param] [options]"
|
196
|
+
|
197
|
+
description "simple parser"
|
198
|
+
|
199
|
+
def options(opts)
|
200
|
+
# etc
|
201
|
+
end
|
202
|
+
|
203
|
+
class SubParser < Optplus::NestedParser
|
204
|
+
|
205
|
+
usage "action subaction [params] [options]"
|
206
|
+
|
207
|
+
description "do things relating to action"
|
208
|
+
|
209
|
+
describe :list, 'list actions'
|
210
|
+
def list
|
211
|
+
# do the listing
|
212
|
+
end
|
213
|
+
help :list 'describe what list does'
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
nest_parser :action, SubParser, 'do things with actions'
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
MyParser.run!
|
222
|
+
|
223
|
+
The methods for getting arguments and options etc that are available in MyParser are
|
224
|
+
also available in SubParser but you cannot set_option in SubParser and you cannot add
|
225
|
+
option switches that are specific to the action. If you want to get into that you may
|
226
|
+
be better trying Thor.
|
227
|
+
|
228
|
+
Help for nested commands works like this:
|
229
|
+
|
230
|
+
* the help/usage text for the script should contain the name of the action and the
|
231
|
+
description provided with {Optplus::Parser#nest_parser} just like all of the other
|
232
|
+
actions.
|
233
|
+
|
234
|
+
* applying the -h switch with the action will display the usage, description and sub-actions
|
235
|
+
just as for the main help but will not repeat the option switches. There is a note
|
236
|
+
to remind you how to see them though (plain -h)
|
237
|
+
|
238
|
+
* applying the -h switch to an action and sub-action that has defined its own help text will
|
239
|
+
do exactly the same as it would if it was just an action
|
240
|
+
|
241
|
+
And finally, don't forget that sub-actions can take parameters but obviously the action
|
242
|
+
to which they belong cannot (cos it will be interpreted as a sub-action)
|
243
|
+
|
244
|
+
## Code Walkthrough
|
245
|
+
|
246
|
+
The code is simple. The class methods that are called in defining a parser use class
|
247
|
+
instance variables to store the data defined (e.g. describe stores the action and the description
|
248
|
+
in a hash). The {Optplus::Parser.run!} method creates an instance of the parser class
|
249
|
+
and the constructor will call the before methods, build the Optparse object with all
|
250
|
+
the tedious separators, call the options method to add your own option switches, and
|
251
|
+
then read the first argument as an action.
|
252
|
+
|
253
|
+
It uses Array#abbrev to convert abbreviated action names into the full action and then
|
254
|
+
calls the method. Optparse exceptions and Optplus exceptions are trapped for convenient
|
255
|
+
error messaging.
|
256
|
+
|
257
|
+
## Dependencies
|
258
|
+
|
259
|
+
Ruby 1.9.3
|
260
|
+
abbrev standard library
|
261
|
+
colored gem
|
262
|
+
|
263
|
+
Check the {file:Gemfile} for dependencies.
|
264
|
+
|
265
|
+
## Documentation
|
266
|
+
|
267
|
+
Documentation is best viewed using Yard and is available online
|
268
|
+
at [RubyDoc](http://rdoc.info/github/osburn-sharp/optplus/frames)
|
269
|
+
|
270
|
+
## Testing/Modifying
|
271
|
+
|
272
|
+
There are no rspec tests provided, but there are two test scripts: {file:test/my_parser.rb}
|
273
|
+
and {file:test/my_nested_parser.rb}. They are not comprehensive, but they do test
|
274
|
+
out various things.
|
275
|
+
|
276
|
+
## Bugs
|
277
|
+
|
278
|
+
Details of any unresolved bugs and change requests are in {file:Bugs.rdoc Bugs}
|
279
|
+
|
280
|
+
## Changelog
|
281
|
+
|
282
|
+
See {file:History.txt} for a summary change history
|
283
|
+
|
284
|
+
## Copyright and Licence
|
285
|
+
|
286
|
+
Copyright (c) 2013 Robert Sharp
|
287
|
+
|
288
|
+
This software is licensed under the terms defined in {file:LICENCE.rdoc}
|
289
|
+
|
290
|
+
The author may be contacted by via [GitHub](http://github.com/osburn-sharp)
|
291
|
+
|
292
|
+
## Warranty
|
293
|
+
|
294
|
+
This software is provided "as is" and without any express or implied
|
295
|
+
warranties, including, without limitation, the implied warranties of
|
296
|
+
merchantibility and fitness for a particular purpose.
|