Revision edb7fc1a kamaki/cli/argument.py

b/kamaki/cli/argument.py
45 45

  
46 46

  
47 47
class Argument(object):
48
    """An argument that can be parsed from command line or otherwise"""
48
    """An argument that can be parsed from command line or otherwise.
49
    This is the general Argument class. It is suggested to extent this
50
    class into more specific argument types.
51
    """
49 52

  
50 53
    def __init__(self, arity, help=None, parsed_name=None, default=None):
51
        self.arity = int(arity)
52

  
53 54
        if help is not None:
54 55
            self.help = help
55 56
        if parsed_name is not None:
......
59 60

  
60 61
    @property
61 62
    def parsed_name(self):
63
        """the string which will be recognised by the parser as an instance
64
            of this argument
65
        """
62 66
        return getattr(self, '_parsed_name', None)
63 67

  
64 68
    @parsed_name.setter
......
71 75

  
72 76
    @property
73 77
    def help(self):
78
        """a user friendly help message"""
74 79
        return getattr(self, '_help', None)
75 80

  
76 81
    @help.setter
......
79 84

  
80 85
    @property
81 86
    def arity(self):
87
        """negative for repeating, 0 for flag, 1 or more for values"""
82 88
        return getattr(self, '_arity', None)
83 89

  
84 90
    @arity.setter
......
88 94

  
89 95
    @property
90 96
    def default(self):
97
        """the value of this argument when not set"""
91 98
        if not hasattr(self, '_default'):
92 99
            self._default = False if self.arity == 0 else None
93 100
        return self._default
......
98 105

  
99 106
    @property
100 107
    def value(self):
108
        """the value of the argument"""
101 109
        return getattr(self, '_value', self.default)
102 110

  
103 111
    @value.setter
......
105 113
        self._value = newvalue
106 114

  
107 115
    def update_parser(self, parser, name):
108
        """Update an argument parser with this argument info"""
116
        """Update argument parser with self info"""
109 117
        action = 'append' if self.arity < 0\
110 118
            else 'store_true' if self.arity == 0\
111 119
            else 'store'
......
113 121
            default=self.default, help=self.help)
114 122

  
115 123
    def main(self):
116
        """Overide this method to give functionality to ur args"""
124
        """Overide this method to give functionality to your args"""
117 125
        raise NotImplementedError
118 126

  
119 127

  
120 128
class ConfigArgument(Argument):
129
    """Manage a kamaki configuration file"""
130

  
121 131
    _config_file = None
132
    """The configuration file"""
122 133

  
123 134
    @property
124 135
    def value(self):
......
145 156

  
146 157

  
147 158
class CmdLineConfigArgument(Argument):
159
    """Set a run-time setting option (not persistent)"""
160

  
148 161
    def __init__(self, config_arg, help='', parsed_name=None, default=None):
149 162
        super(self.__class__, self).__init__(1, help, parsed_name, default)
150 163
        self._config_arg = config_arg
......
176 189

  
177 190

  
178 191
class FlagArgument(Argument):
192
    """
193
    :value type: accepts no values from user
194
    :value returns: true if argument is set, false otherwise
195
    """
196

  
179 197
    def __init__(self, help='', parsed_name=None, default=None):
180 198
        super(FlagArgument, self).__init__(0, help, parsed_name, default)
181 199

  
182 200

  
183 201
class ValueArgument(Argument):
202
    """
203
    :value type: string
204
    :value returns: given value or default
205
    """
206

  
184 207
    def __init__(self, help='', parsed_name=None, default=None):
185 208
        super(ValueArgument, self).__init__(1, help, parsed_name, default)
186 209

  
187 210

  
188 211
class IntArgument(ValueArgument):
212
    """
213
    :value type: integer (type checking occurs)
214
    :value returns: an integer
215
    """
216

  
189 217
    @property
190 218
    def value(self):
191 219
        return getattr(self, '_value', self.default)
......
203 231

  
204 232

  
205 233
class VersionArgument(FlagArgument):
234
    """A flag argument with that prints current version"""
235

  
206 236
    @property
207 237
    def value(self):
208 238
        return super(self.__class__, self).value
......
219 249

  
220 250

  
221 251
class KeyValueArgument(Argument):
252
    """A Value Argument that can be repeated
253

  
254
    --<argument> key1=value1 --<argument> key2=value2 ...
255
    """
256

  
222 257
    def __init__(self, help='', parsed_name=None, default=[]):
223 258
        super(KeyValueArgument, self).__init__(-1, help, parsed_name, default)
224 259

  

Also available in: Unified diff