JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / common / cliutil.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 def arg(*args, **kwargs):
11     """Decorator for CLI args.
12
13     Example:
14
15     >>> @arg("name", help="Name of the new entity")
16     ... def entity_create(args):
17     ...     pass
18     """
19     def _decorator(func):
20         add_arg(func, *args, **kwargs)
21         return func
22     return _decorator
23
24
25 def add_arg(func, *args, **kwargs):
26     """Bind CLI arguments to a shell.py `do_foo` function."""
27
28     if not hasattr(func, 'arguments'):
29         func.arguments = []
30
31     # NOTE(sirp): avoid dups that can occur when the module is shared across
32     # tests.
33     if (args, kwargs) not in func.arguments:
34         # Because of the semantics of decorator composition if we just append
35         # to the options list positional options will appear to be backwards.
36         func.arguments.insert(0, (args, kwargs))