@@ -2,25 +2,25 @@ commandr
2
2
========
3
3
4
4
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
6
6
the races.
7
7
8
8
Example
9
9
-------
10
10
In example.py:
11
11
``` python
12
12
@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 ):
14
14
""" Greet someone.
15
15
Arguments:
16
16
name - Name to greet.
17
17
title - Title of the person to greet.
18
18
times - Number of time to say the greeting.
19
19
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.
21
21
"""
22
22
message = ' Hi%s %s %s !' % (' ,' if comma else ' ' , title, name)
23
- if capslock :
23
+ if caps_lock :
24
24
message = message.upper()
25
25
26
26
for _ in xrange (times):
@@ -45,9 +45,9 @@ The command can them be invoked on the command line with:
45
45
46
46
# Combined explicit and positional arguments. In this case, 'Julie' will
47
47
# 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.
49
49
# 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
51
51
HI , ENGINEER JULIE !
52
52
```
53
53
@@ -106,7 +106,7 @@ The following script will be used to show examples of the features:
106
106
...
107
107
108
108
@command (' version' )
109
- def GetVersion (host , dev = False ):
109
+ def GetVersion (host_name , dev = False ):
110
110
...
111
111
112
112
if __name__ == ' __main__' :
@@ -128,6 +128,19 @@ $ python features.py put somekey somevalue -t 5
128
128
```
129
129
Note that the '=' signs are optional.
130
130
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
+
131
144
### Defaults and Types
132
145
133
146
Keyword argument defaults are respected, and are used to infer types for those
@@ -153,7 +166,7 @@ $ python features.py version --dev
153
166
When a boolean parameter default is True, the generated switch is the parameter
154
167
name with "no_ " prefixed. For example, to set 'cache' to False for 'get':
155
168
``` bash
156
- $ python features.py get somekey --no_cache
169
+ $ python features.py get somekey --no-cache
157
170
```
158
171
159
172
### Documentation Generation
@@ -198,7 +211,7 @@ $ python features.py get -h
198
211
> -k KEY, --key=KEY
199
212
> -t TIMEOUT, --timeout=TIMEOUT
200
213
> [default: 10]
201
- > -n, --no_cache
214
+ > -n, --no-cache
202
215
```
203
216
204
217
If a command is invoked with incomplete arguments, or invalid values, the error
@@ -237,6 +250,42 @@ $ python features.py put somekey1 --transaction
237
250
> --transaction
238
251
```
239
252
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
+
240
289
Authors
241
290
-------
242
291
commandr was developed at TellApart by [ Kevin Ballard] ( https://github.com/kevinballard ) and [ Chris Huegle] ( https://github.com/chuegle ) .
0 commit comments