Skip to content

Commit 20c1749

Browse files
author
Kevin Ballard
committed
Merge branch 'dashes' of git://github.com/kevinballard/commandr into kevinballard-dashes
Conflicts: README.txt
2 parents dc3062d + d9290f9 commit 20c1749

File tree

5 files changed

+408
-477
lines changed

5 files changed

+408
-477
lines changed

README.md

+58-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ commandr
22
========
33

44
commandr is a simple tool for making Python functions accessible from the
5-
command line. Essentially you add a command decorator and you are off to
5+
command line. Essentially you add a command decorator and you are off to
66
the races.
77

88
Example
99
-------
1010
In example.py:
1111
```python
1212
@command('greet')
13-
def SayGreeting(name, title='Mr.', times=1, comma=False, capslock=False):
13+
def SayGreeting(name, title='Mr.', times=1, comma=False, caps_lock=False):
1414
"""Greet someone.
1515
Arguments:
1616
name - Name to greet.
1717
title - Title of the person to greet.
1818
times - Number of time to say the greeting.
1919
comma - Whether to add a comma after the greeting.
20-
capslock - Whether to output in ALL CAPS.
20+
caps_lock - Whether to output in ALL CAPS.
2121
"""
2222
message = 'Hi%s %s %s!' % (',' if comma else '', title, name)
23-
if capslock:
23+
if caps_lock:
2424
message = message.upper()
2525

2626
for _ in xrange(times):
@@ -45,9 +45,9 @@ The command can them be invoked on the command line with:
4545

4646
# Combined explicit and positional arguments. In this case, 'Julie' will
4747
# match the first unspecified argument 'name'
48-
# 'capslock' doesn't have a short name because 'comma' came first.
48+
# 'caps-lock' doesn't have a short name because 'comma' came first.
4949
# Equal signs are also optional.
50-
$ python example.py greet --title Engineer -c --capslock Julie
50+
$ python example.py greet --title Engineer -c --caps-lock Julie
5151
HI, ENGINEER JULIE!
5252
```
5353

@@ -106,7 +106,7 @@ The following script will be used to show examples of the features:
106106
...
107107

108108
@command('version')
109-
def GetVersion(host, dev=False):
109+
def GetVersion(host_name, dev=False):
110110
...
111111

112112
if __name__ == '__main__':
@@ -128,6 +128,19 @@ $ python features.py put somekey somevalue -t 5
128128
```
129129
Note that the '=' signs are optional.
130130

131+
### Underscores
132+
133+
Underscores ('_') in parameter names can be automatically converted to dashes
134+
('-'). When enabled, both form of the argument are allowed. For example, both
135+
of the following are correct:
136+
```bash
137+
$ python features.py version --host-name localhost
138+
$ python features.py version --host_name localhost
139+
```
140+
141+
This feature is enabled by default. See the Options section below for details
142+
on how to adjust this behavior.
143+
131144
### Defaults and Types
132145

133146
Keyword argument defaults are respected, and are used to infer types for those
@@ -153,7 +166,7 @@ $ python features.py version --dev
153166
When a boolean parameter default is True, the generated switch is the parameter
154167
name with "no_" prefixed. For example, to set 'cache' to False for 'get':
155168
```bash
156-
$ python features.py get somekey --no_cache
169+
$ python features.py get somekey --no-cache
157170
```
158171

159172
### Documentation Generation
@@ -198,7 +211,7 @@ $ python features.py get -h
198211
> -k KEY, --key=KEY
199212
> -t TIMEOUT, --timeout=TIMEOUT
200213
> [default: 10]
201-
> -n, --no_cache
214+
> -n, --no-cache
202215
```
203216

204217
If a command is invoked with incomplete arguments, or invalid values, the error
@@ -237,6 +250,42 @@ $ python features.py put somekey1 --transaction
237250
> --transaction
238251
```
239252

253+
### Options
254+
255+
There are several options that can be set to modify the behavior of the parser
256+
generation. These options can be set by calling the SeeOption() function, or
257+
as parameters to the Run() function. Values set by Run() take precedence.
258+
259+
The available parameters are:
260+
261+
##### hyphenate:
262+
If True (default), then any argument containing an underscore ('_') will be
263+
parsable with dashes ('-') in their place (both variants will be allowed).
264+
If False, then only the original argument name will be accepted.
265+
266+
Note that if hyphenate is enabled, partial arguments will result in a
267+
conflict error. For example, if --caps is supplied for --caps-lock and
268+
--caps_lock, a conflict error will occur.
269+
270+
#####show_all_help_variants:
271+
If False (default), then only one argument name variant will be displayed
272+
in the help text (all forms will remain accepted as arguments).
273+
Specifically, when hyphenate is True, only the hyphenated variant will be
274+
displayed in the help text.
275+
276+
* * *
277+
278+
For example, disabling hyphenation:
279+
```python
280+
from commandr import command, Run, SetOptions
281+
282+
...
283+
284+
if __name__ == '__main__':
285+
SetOptions(hyphenate=False)
286+
Run()
287+
```
288+
240289
Authors
241290
-------
242291
commandr was developed at TellApart by [Kevin Ballard](https://github.com/kevinballard) and [Chris Huegle](https://github.com/chuegle).

README.txt

-224
This file was deleted.

commandr/__init__.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__all__ = ['command', 'Run']
15+
__all__ = [
16+
'command',
17+
'Run',
18+
'SetOptions',]
1619

17-
from commandr import Run, command
20+
from commandr import Commandr
1821

22+
_COMMANDR = Commandr()
23+
24+
# Export the global Commandr object methods.
25+
command = _COMMANDR.command
26+
Run = _COMMANDR.Run
27+
SetOptions = _COMMANDR.SetOptions

0 commit comments

Comments
 (0)