forked from anandology/subcommand
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support specifying default value and type.
- Loading branch information
1 parent
1324f39
commit 2e40269
Showing
3 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# subcommand: Python library for creating command line applicaitons with subcommands | ||
|
||
Subcommand is a function decorated with `subcommand.subcommand()` decorator. | ||
Help message and options are specified in docstring. | ||
|
||
Hello world example: | ||
|
||
import subcommand | ||
|
||
@subcommand.subcommand() | ||
def hello(options, name='world'): | ||
"""Display hello message. | ||
|
||
Usage: hello [options] [name] | ||
|
||
Options: | ||
|
||
-p [--prefix] prefix : message prefix (default: hello) | ||
-n [--repeat] n : repeat the message n times (type: int) (default: 1) | ||
""" | ||
for i in range(options.n): | ||
print options.prefix + ",", name | ||
|
||
if __name__ == "__main__": | ||
subcommand.main() | ||
|
||
And here is the response. | ||
|
||
$ python hello.py hello | ||
hello, world | ||
$ python hello.py hello subcommand | ||
hello, subcommand | ||
$ python hello.py hello -p bye subcommand | ||
bye, subcommand | ||
$ python hello.py hello --prefix bye subcommand | ||
bye, subcommand | ||
$ python hello.py hello -p bye -n 3 subcommand | ||
bye, subcommand | ||
bye, subcommand | ||
bye, subcommand | ||
|
||
$ python hello.py | ||
usage: python hello.py <subcommand> [options] [args] | ||
|
||
Available subcommands: | ||
hello Displays hello message. | ||
help Describe usage of this program and its subcommands. | ||
|
||
anand@bodhi subcommand$ python hello.py help hello | ||
hello: Displays hello message. | ||
|
||
Usage: hello [options] [name] | ||
|
||
Valid Options: | ||
-p [--prefix] prefix : message prefix (default: hello) | ||
-n [--repeat] n : repeat the message n times (default: 1) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Subcommand example. | ||
Options: | ||
-v [--verbose] : verbose | ||
""" | ||
import subcommand | ||
|
||
@subcommand.subcommand(aliases=["co"]) | ||
def checkout(options, *args): | ||
"""Check out a working copy from a repository. | ||
Fake subcommand to demonstrate subcommand functionality. | ||
Options: | ||
-r [--revision] revision int : revision to checkout | ||
--username username : username | ||
--password password : password | ||
""" | ||
print "dummy checkout." | ||
print "options:", options | ||
print "args:", args | ||
print "global options:", subcommand.global_options | ||
|
||
if __name__ == "__main__": | ||
subcommand.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters