This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexample.py
executable file
·74 lines (63 loc) · 2 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
#
# Copyright 2013 TellApart, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =============================================================================
"""Example usage of commandr."""
from commandr import command, Run, CommandrUsageError, wraps
@command('greet')
def SayGreeting(name, title='Mr.', times=1, comma=False, caps_lock=False):
"""Greet someone.
Arguments:
name - Name to greet.
title - Title of the person to greet.
times - Number of time to say the greeting.
comma - Whether to add a comma after the greeting.
caps_lock - Whether to output in ALL CAPS.
"""
message = 'Hi%s %s %s!' % (',' if comma else '', title, name)
if caps_lock:
message = message.upper()
for _ in xrange(times):
print message
@command
def simple_greet(name):
"""An example of @command without arguments and printing Usage.
Arguments:
name - Name to greet.
"""
if name == 'John':
raise CommandrUsageError("John is not a valid name.")
print 'Hi %s!' % name
@command()
def another_simple_greet(name):
"""An example of @command() without arguments.
Arguments:
name - Name to greet.
"""
print 'Aloha %s!' % name
def some_decorator(fn):
@wraps(fn)
def _wrapper(*args, **kwargs):
print 'Wrapper Here!'
return fn(*args, **kwargs)
return _wrapper
@command('test_decorated')
@some_decorator
def DecoratedFunction(arg1, arg2=1):
"""An example usage of stacked decorators."""
print arg1, arg2
if __name__ == '__main__':
Run(hyphenate=True)