Revision f17d6cb5 kamaki/cli/argument/__init__.py

b/kamaki/cli/argument/__init__.py
64 64
    def __init__(self, arity, help=None, parsed_name=None, default=None):
65 65
        self.arity = int(arity)
66 66
        self.help = '%s' % help or ''
67
        self.parsed_name = parsed_name
68
        self.default = default or (None if self.arity else False)
69 67

  
70
    @property
71
    def parsed_name(self):
72
        """
73
        :returns: (str) of the form --smth or -s is recognised as a call to an
74
            argument instance
75
        """
76
        return getattr(self, '_parsed_name', [])
77

  
78
    @parsed_name.setter
79
    def parsed_name(self, newname):
80
        assert newname, 'No parsed name for argument %s' % self
81
        self._parsed_name = getattr(self, '_parsed_name', [])
82
        if isinstance(newname, list) or isinstance(newname, tuple):
83
            self._parsed_name += list(newname)
84
        else:
85
            self._parsed_name.append('%s' % newname)
68
        assert parsed_name, 'No parsed name for argument %s' % self
69
        self.parsed_name = list(parsed_name) if isinstance(
70
            parsed_name, list) or isinstance(parsed_name, tuple) else (
71
                '%s' % parsed_name).split()
72
        for name in self.parsed_name:
73
            assert name.count(' ') == 0, '%s: Invalid parse name "%s"' % (
74
                self, name)
75
            msg = '%s: Invalid parse name "%s" should start with a "-"' % (
76
                    self, name)
77
            assert name.startswith('-'), msg
78

  
79
        self.default = default or (None if self.arity else False)
86 80

  
87 81
    @property
88 82
    def value(self):
89
        """the value of the argument"""
90 83
        return getattr(self, '_value', self.default)
91 84

  
92 85
    @value.setter
......
95 88

  
96 89
    def update_parser(self, parser, name):
97 90
        """Update argument parser with self info"""
98
        action = 'append' if self.arity < 0\
99
            else 'store_true' if self.arity == 0\
100
            else 'store'
91
        action = 'append' if self.arity < 0 else (
92
            'store' if self.arity else 'store_true')
101 93
        parser.add_argument(
102 94
            *self.parsed_name,
103
            dest=name,
104
            action=action,
105
            default=self.default,
106
            help=self.help)
107

  
108
    def main(self):
109
        """Overide this method to give functionality to your args"""
110
        raise NotImplementedError
95
            dest=name, action=action, default=self.default, help=self.help)
111 96

  
112 97

  
113 98
class ConfigArgument(Argument):
......
153 138
    def get_cloud(self, cloud, option):
154 139
        return self.value.get_cloud(cloud, option)
155 140

  
141

  
156 142
_config_arg = ConfigArgument(
157 143
    1, 'Path to configuration file', ('-c', '--config'))
158 144

  
......
305 291
    @value.setter
306 292
    def value(self, newvalue):
307 293
        self._value = newvalue
308
        self.main()
309

  
310
    def main(self):
311
        """Print current version"""
312
        if self.value:
294
        if newvalue:
313 295
            import kamaki
314 296
            print('kamaki %s' % kamaki.__version__)
315 297

  

Also available in: Unified diff